Package list golang-github-go-kit-kit / 6ad4220
Simplify and fix unstable test Yuri Shkuro 4 years ago
1 changed file(s) with 32 addition(s) and 34 deletion(s). Raw diff Collapse all Expand all
1111
1212 // The test verifies the following:
1313 // registering causes initial notification of the current state
14 // notifications delivered to two receivers
14 // instances are sorted
15 // different update causes new notification
1516 // identical notifications cause no updates
16 // different update causes new notification
17 // instances are sorted
1817 // no updates after de-registering
1918 func TestCache(t *testing.T) {
2019 e1 := sd.Event{Instances: []string{"y", "x"}} // not sorted
2120 e2 := sd.Event{Instances: []string{"c", "a", "b"}}
2221
23 c := NewCache()
24 if want, have := 0, len(c.State().Instances); want != have {
22 cache := NewCache()
23 if want, have := 0, len(cache.State().Instances); want != have {
2524 t.Fatalf("want %v instances, have %v", want, have)
2625 }
2726
28 c.Update(e1) // sets initial state
29 if want, have := 2, len(c.State().Instances); want != have {
27 cache.Update(e1) // sets initial state
28 if want, have := 2, len(cache.State().Instances); want != have {
3029 t.Fatalf("want %v instances, have %v", want, have)
3130 }
3231
3332 r1 := make(chan sd.Event)
34 go c.Register(r1)
33 go cache.Register(r1)
3534 expectUpdate(t, r1, []string{"x", "y"})
3635
37 r2 := make(chan sd.Event)
38 go c.Register(r2)
39 expectUpdate(t, r2, []string{"x", "y"})
36 go cache.Update(e2) // different set
37 expectUpdate(t, r1, []string{"a", "b", "c"})
4038
41 // send the same instances but in different order.
42 // because it's a duplicate it should not cause new notification.
43 // if it did, this call would deadlock trying to send to channels with no readers
44 c.Update(sd.Event{Instances: []string{"x", "y"}})
45 expectNoUpdate(t, r1)
46 expectNoUpdate(t, r2)
47
48 go c.Update(e2) // different set
49 expectUpdate(t, r1, []string{"a", "b", "c"})
50 expectUpdate(t, r2, []string{"a", "b", "c"})
51
52 c.Deregister(r1)
53 c.Deregister(r2)
39 cache.Deregister(r1)
5440 close(r1)
55 close(r2)
56 // if deregister didn't work, Update would panic on closed channels
57 c.Update(e1)
5841 }
5942
6043 func expectUpdate(t *testing.T, r chan sd.Event, expect []string) {
6447 t.Fatalf("want: %v, have: %v", want, have)
6548 }
6649 case <-time.After(time.Second):
67 t.Fatalf("did not receive expected update")
50 t.Fatalf("did not receive expected update %v", expect)
6851 }
6952 }
7053
71 func expectNoUpdate(t *testing.T, r chan sd.Event) {
72 select {
73 case e := <-r:
74 t.Errorf("received unexpected update %v", e)
75 case <-time.After(time.Millisecond):
76 return // as expected
54 func TestRegistry(t *testing.T) {
55 reg := make(registry)
56 c1 := make(chan sd.Event, 1)
57 c2 := make(chan sd.Event, 1)
58 reg.register(c1)
59 reg.register(c2)
60
61 // validate that both channels receive the update
62 reg.broadcast(sd.Event{Instances: []string{"x", "y"}})
63 if want, have := []string{"x", "y"}, (<-c1).Instances; !reflect.DeepEqual(want, have) {
64 t.Fatalf("want: %v, have: %v", want, have)
7765 }
66 if want, have := []string{"x", "y"}, (<-c2).Instances; !reflect.DeepEqual(want, have) {
67 t.Fatalf("want: %v, have: %v", want, have)
68 }
69
70 reg.deregister(c1)
71 reg.deregister(c2)
72 close(c1)
73 close(c2)
74 // if deregister didn't work, broadcast would panic on closed channels
75 reg.broadcast(sd.Event{Instances: []string{"x", "y"}})
7876 }