2017-01-23 06:23:07 -05:00
|
|
|
package discovery
|
2015-11-11 19:18:06 -05:00
|
|
|
|
|
|
|
import (
|
2017-03-11 03:03:22 -05:00
|
|
|
"fmt"
|
2015-11-11 19:18:06 -05:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDiscoveryOpts(t *testing.T) {
|
|
|
|
clusterOpts := map[string]string{"discovery.heartbeat": "10", "discovery.ttl": "5"}
|
|
|
|
heartbeat, ttl, err := discoveryOpts(clusterOpts)
|
|
|
|
if err == nil {
|
2017-02-21 03:53:29 -05:00
|
|
|
t.Fatal("discovery.ttl < discovery.heartbeat must fail")
|
2015-11-11 19:18:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
clusterOpts = map[string]string{"discovery.heartbeat": "10", "discovery.ttl": "10"}
|
|
|
|
heartbeat, ttl, err = discoveryOpts(clusterOpts)
|
|
|
|
if err == nil {
|
2017-02-21 03:53:29 -05:00
|
|
|
t.Fatal("discovery.ttl == discovery.heartbeat must fail")
|
2015-11-11 19:18:06 -05:00
|
|
|
}
|
|
|
|
|
2016-08-31 13:07:05 -04:00
|
|
|
clusterOpts = map[string]string{"discovery.heartbeat": "-10", "discovery.ttl": "10"}
|
|
|
|
heartbeat, ttl, err = discoveryOpts(clusterOpts)
|
|
|
|
if err == nil {
|
2017-02-21 03:53:29 -05:00
|
|
|
t.Fatal("negative discovery.heartbeat must fail")
|
2016-08-31 13:07:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
clusterOpts = map[string]string{"discovery.heartbeat": "10", "discovery.ttl": "-10"}
|
|
|
|
heartbeat, ttl, err = discoveryOpts(clusterOpts)
|
|
|
|
if err == nil {
|
2017-02-21 03:53:29 -05:00
|
|
|
t.Fatal("negative discovery.ttl must fail")
|
2016-08-31 13:07:05 -04:00
|
|
|
}
|
|
|
|
|
2015-11-11 19:18:06 -05:00
|
|
|
clusterOpts = map[string]string{"discovery.heartbeat": "invalid"}
|
|
|
|
heartbeat, ttl, err = discoveryOpts(clusterOpts)
|
|
|
|
if err == nil {
|
2017-02-21 03:53:29 -05:00
|
|
|
t.Fatal("invalid discovery.heartbeat must fail")
|
2015-11-11 19:18:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
clusterOpts = map[string]string{"discovery.ttl": "invalid"}
|
|
|
|
heartbeat, ttl, err = discoveryOpts(clusterOpts)
|
|
|
|
if err == nil {
|
2017-02-21 03:53:29 -05:00
|
|
|
t.Fatal("invalid discovery.ttl must fail")
|
2015-11-11 19:18:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
clusterOpts = map[string]string{"discovery.heartbeat": "10", "discovery.ttl": "20"}
|
|
|
|
heartbeat, ttl, err = discoveryOpts(clusterOpts)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if heartbeat != 10*time.Second {
|
2016-08-29 06:37:14 -04:00
|
|
|
t.Fatalf("Heartbeat - Expected : %v, Actual : %v", 10*time.Second, heartbeat)
|
2015-11-11 19:18:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if ttl != 20*time.Second {
|
|
|
|
t.Fatalf("TTL - Expected : %v, Actual : %v", 20*time.Second, ttl)
|
|
|
|
}
|
|
|
|
|
|
|
|
clusterOpts = map[string]string{"discovery.heartbeat": "10"}
|
|
|
|
heartbeat, ttl, err = discoveryOpts(clusterOpts)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if heartbeat != 10*time.Second {
|
2016-08-29 06:37:14 -04:00
|
|
|
t.Fatalf("Heartbeat - Expected : %v, Actual : %v", 10*time.Second, heartbeat)
|
2015-11-11 19:18:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
expected := 10 * defaultDiscoveryTTLFactor * time.Second
|
|
|
|
if ttl != expected {
|
|
|
|
t.Fatalf("TTL - Expected : %v, Actual : %v", expected, ttl)
|
|
|
|
}
|
|
|
|
|
|
|
|
clusterOpts = map[string]string{"discovery.ttl": "30"}
|
|
|
|
heartbeat, ttl, err = discoveryOpts(clusterOpts)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ttl != 30*time.Second {
|
|
|
|
t.Fatalf("TTL - Expected : %v, Actual : %v", 30*time.Second, ttl)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected = 30 * time.Second / defaultDiscoveryTTLFactor
|
|
|
|
if heartbeat != expected {
|
2016-08-29 06:37:14 -04:00
|
|
|
t.Fatalf("Heartbeat - Expected : %v, Actual : %v", expected, heartbeat)
|
2015-11-11 19:18:06 -05:00
|
|
|
}
|
|
|
|
|
2017-05-21 19:24:07 -04:00
|
|
|
discoveryTTL := fmt.Sprintf("%d", defaultDiscoveryTTLFactor-1)
|
|
|
|
clusterOpts = map[string]string{"discovery.ttl": discoveryTTL}
|
2017-03-11 03:03:22 -05:00
|
|
|
heartbeat, ttl, err = discoveryOpts(clusterOpts)
|
|
|
|
if err == nil && heartbeat == 0 {
|
|
|
|
t.Fatal("discovery.heartbeat must be positive")
|
|
|
|
}
|
|
|
|
|
2015-11-11 19:18:06 -05:00
|
|
|
clusterOpts = map[string]string{}
|
|
|
|
heartbeat, ttl, err = discoveryOpts(clusterOpts)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if heartbeat != defaultDiscoveryHeartbeat {
|
2016-08-29 06:37:14 -04:00
|
|
|
t.Fatalf("Heartbeat - Expected : %v, Actual : %v", defaultDiscoveryHeartbeat, heartbeat)
|
2015-11-11 19:18:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
expected = defaultDiscoveryHeartbeat * defaultDiscoveryTTLFactor
|
|
|
|
if ttl != expected {
|
|
|
|
t.Fatalf("TTL - Expected : %v, Actual : %v", expected, ttl)
|
|
|
|
}
|
|
|
|
}
|