1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
package eureka
import (
"testing"
"time"
"github.com/hudl/fargo"
"github.com/go-kit/kit/sd"
)
var _ sd.Instancer = (*Instancer)(nil) // API check
func TestInstancer(t *testing.T) {
connection := &testConnection{
instances: []*fargo.Instance{instanceTest1, instanceTest2},
errApplication: nil,
}
instancer := NewInstancer(connection, appNameTest, loggerTest)
defer instancer.Stop()
state := instancer.state()
if state.Err != nil {
t.Fatal(state.Err)
}
if want, have := 2, len(state.Instances); want != have {
t.Errorf("want %d, have %d", want, have)
}
}
func TestInstancerReceivesUpdates(t *testing.T) {
connection := &testConnection{
instances: []*fargo.Instance{instanceTest1},
errApplication: nil,
}
instancer := NewInstancer(connection, appNameTest, loggerTest)
defer instancer.Stop()
verifyCount := func(want int) (have int, converged bool) {
const maxPollAttempts = 5
const delayPerAttempt = 200 * time.Millisecond
for i := 1; ; i++ {
state := instancer.state()
if have := len(state.Instances); want == have {
return have, true
} else if i == maxPollAttempts {
return have, false
}
time.Sleep(delayPerAttempt)
}
}
if have, converged := verifyCount(1); !converged {
t.Fatalf("initial: want %d, have %d", 1, have)
}
if err := connection.RegisterInstance(instanceTest2); err != nil {
t.Fatalf("failed to register an instance: %v", err)
}
if have, converged := verifyCount(2); !converged {
t.Fatalf("after registration: want %d, have %d", 2, have)
}
if err := connection.DeregisterInstance(instanceTest1); err != nil {
t.Fatalf("failed to unregister an instance: %v", err)
}
if have, converged := verifyCount(1); !converged {
t.Fatalf("after deregistration: want %d, have %d", 1, have)
}
}
func TestBadInstancerScheduleUpdates(t *testing.T) {
connection := &testConnection{
instances: []*fargo.Instance{instanceTest1},
errApplication: errTest,
}
instancer := NewInstancer(connection, appNameTest, loggerTest)
defer instancer.Stop()
state := instancer.state()
if state.Err == nil {
t.Fatal("expecting error")
}
if want, have := 0, len(state.Instances); want != have {
t.Errorf("want %d, have %d", want, have)
}
}
|