8 "github.com/go-kit/kit/sd"
11 var _ sd.Instancer = (*Cache)(nil) // API check
13 // The test verifies the following:
14 // registering causes initial notification of the current state
15 // instances are sorted
16 // different update causes new notification
17 // identical notifications cause no updates
18 // no updates after de-registering
19 func TestCache(t *testing.T) {
20 e1 := sd.Event{Instances: []string{"y", "x"}} // not sorted
21 e2 := sd.Event{Instances: []string{"c", "a", "b"}}
24 if want, have := 0, len(cache.State().Instances); want != have {
25 t.Fatalf("want %v instances, have %v", want, have)
28 cache.Update(e1) // sets initial state
29 if want, have := 2, len(cache.State().Instances); want != have {
30 t.Fatalf("want %v instances, have %v", want, have)
33 r1 := make(chan sd.Event)
35 expectUpdate(t, r1, []string{"x", "y"})
37 go cache.Update(e2) // different set
38 expectUpdate(t, r1, []string{"a", "b", "c"})
44 func expectUpdate(t *testing.T, r chan sd.Event, expect []string) {
47 if want, have := expect, e.Instances; !reflect.DeepEqual(want, have) {
48 t.Fatalf("want: %v, have: %v", want, have)
50 case <-time.After(time.Second):
51 t.Fatalf("did not receive expected update %v", expect)
55 func TestRegistry(t *testing.T) {
57 c1 := make(chan sd.Event, 1)
58 c2 := make(chan sd.Event, 1)
62 // validate that both channels receive the update
63 reg.broadcast(sd.Event{Instances: []string{"x", "y"}})
64 if want, have := []string{"x", "y"}, (<-c1).Instances; !reflect.DeepEqual(want, have) {
65 t.Fatalf("want: %v, have: %v", want, have)
67 if want, have := []string{"x", "y"}, (<-c2).Instances; !reflect.DeepEqual(want, have) {
68 t.Fatalf("want: %v, have: %v", want, have)
75 // if deregister didn't work, broadcast would panic on closed channels
76 reg.broadcast(sd.Event{Instances: []string{"x", "y"}})