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"
|
2016-08-31 13:07:05 -04:00
|
|
|
|
2017-09-08 18:00:14 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
2015-11-11 19:18:06 -05:00
|
|
|
|
2017-09-08 18:00:14 -04:00
|
|
|
func TestDiscoveryOptsErrors(t *testing.T) {
|
|
|
|
var testcases = []struct {
|
|
|
|
doc string
|
|
|
|
opts map[string]string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
doc: "discovery.ttl < discovery.heartbeat",
|
|
|
|
opts: map[string]string{"discovery.heartbeat": "10", "discovery.ttl": "5"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "discovery.ttl == discovery.heartbeat",
|
|
|
|
opts: map[string]string{"discovery.heartbeat": "10", "discovery.ttl": "10"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "negative discovery.heartbeat",
|
|
|
|
opts: map[string]string{"discovery.heartbeat": "-10", "discovery.ttl": "10"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "negative discovery.ttl",
|
|
|
|
opts: map[string]string{"discovery.heartbeat": "10", "discovery.ttl": "-10"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "invalid discovery.heartbeat",
|
|
|
|
opts: map[string]string{"discovery.heartbeat": "invalid"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "invalid discovery.ttl",
|
|
|
|
opts: map[string]string{"discovery.ttl": "invalid"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, testcase := range testcases {
|
|
|
|
_, _, err := discoveryOpts(testcase.opts)
|
|
|
|
assert.Error(t, err, testcase.doc)
|
2015-11-11 19:18:06 -05:00
|
|
|
}
|
2017-09-08 18:00:14 -04:00
|
|
|
}
|
2015-11-11 19:18:06 -05:00
|
|
|
|
2017-09-08 18:00:14 -04:00
|
|
|
func TestDiscoveryOpts(t *testing.T) {
|
|
|
|
clusterOpts := map[string]string{"discovery.heartbeat": "10", "discovery.ttl": "20"}
|
|
|
|
heartbeat, ttl, err := discoveryOpts(clusterOpts)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, 10*time.Second, heartbeat)
|
|
|
|
assert.Equal(t, 20*time.Second, ttl)
|
2015-11-11 19:18:06 -05:00
|
|
|
|
|
|
|
clusterOpts = map[string]string{"discovery.heartbeat": "10"}
|
|
|
|
heartbeat, ttl, err = discoveryOpts(clusterOpts)
|
2017-09-08 18:00:14 -04:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, 10*time.Second, heartbeat)
|
|
|
|
assert.Equal(t, 10*defaultDiscoveryTTLFactor*time.Second, ttl)
|
2015-11-11 19:18:06 -05:00
|
|
|
|
|
|
|
clusterOpts = map[string]string{"discovery.ttl": "30"}
|
|
|
|
heartbeat, ttl, err = discoveryOpts(clusterOpts)
|
2017-09-08 18:00:14 -04:00
|
|
|
require.NoError(t, err)
|
2015-11-11 19:18:06 -05:00
|
|
|
|
|
|
|
if ttl != 30*time.Second {
|
|
|
|
t.Fatalf("TTL - Expected : %v, Actual : %v", 30*time.Second, ttl)
|
|
|
|
}
|
|
|
|
|
2017-09-08 18:00:14 -04:00
|
|
|
expected := 30 * time.Second / defaultDiscoveryTTLFactor
|
2015-11-11 19:18:06 -05:00
|
|
|
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-09-08 18:00:14 -04:00
|
|
|
heartbeat, _, err = discoveryOpts(clusterOpts)
|
2017-03-11 03:03:22 -05:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|