Merge pull request #550 from yurishkuro/ys-fix-549
Simplify and fix unstable test
Peter Bourgon authored 5 years ago
GitHub committed 5 years ago
11 | 11 | |
12 | 12 | // The test verifies the following: |
13 | 13 | // registering causes initial notification of the current state |
14 | // notifications delivered to two receivers | |
14 | // instances are sorted | |
15 | // different update causes new notification | |
15 | 16 | // identical notifications cause no updates |
16 | // different update causes new notification | |
17 | // instances are sorted | |
18 | 17 | // no updates after de-registering |
19 | 18 | func TestCache(t *testing.T) { |
20 | 19 | e1 := sd.Event{Instances: []string{"y", "x"}} // not sorted |
21 | 20 | e2 := sd.Event{Instances: []string{"c", "a", "b"}} |
22 | 21 | |
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 { | |
25 | 24 | t.Fatalf("want %v instances, have %v", want, have) |
26 | 25 | } |
27 | 26 | |
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 { | |
30 | 29 | t.Fatalf("want %v instances, have %v", want, have) |
31 | 30 | } |
32 | 31 | |
33 | 32 | r1 := make(chan sd.Event) |
34 | go c.Register(r1) | |
33 | go cache.Register(r1) | |
35 | 34 | expectUpdate(t, r1, []string{"x", "y"}) |
36 | 35 | |
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"}) | |
40 | 38 | |
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) | |
54 | 40 | close(r1) |
55 | close(r2) | |
56 | // if deregister didn't work, Update would panic on closed channels | |
57 | c.Update(e1) | |
58 | 41 | } |
59 | 42 | |
60 | 43 | func expectUpdate(t *testing.T, r chan sd.Event, expect []string) { |
64 | 47 | t.Fatalf("want: %v, have: %v", want, have) |
65 | 48 | } |
66 | 49 | case <-time.After(time.Second): |
67 | t.Fatalf("did not receive expected update") | |
50 | t.Fatalf("did not receive expected update %v", expect) | |
68 | 51 | } |
69 | 52 | } |
70 | 53 | |
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) | |
77 | 65 | } |
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"}}) | |
78 | 76 | } |