sd: fix TestDefaultEndpointer flake, hopefully
Peter Bourgon
6 years ago
26 | 26 | instancer.Update(sd.Event{Instances: []string{"a", "b"}}) |
27 | 27 | |
28 | 28 | endpointer := sd.NewEndpointer(instancer, f, log.NewNopLogger(), sd.InvalidateOnError(time.Minute)) |
29 | if endpoints, err := endpointer.Endpoints(); err != nil { | |
30 | t.Errorf("unepected error %v", err) | |
31 | } else if want, have := 2, len(endpoints); want != have { | |
32 | t.Errorf("want %d, have %d", want, have) | |
29 | ||
30 | var ( | |
31 | endpoints []endpoint.Endpoint | |
32 | err error | |
33 | ) | |
34 | if !within(time.Second, func() bool { | |
35 | endpoints, err = endpointer.Endpoints() | |
36 | return err == nil && len(endpoints) == 2 | |
37 | }) { | |
38 | t.Errorf("wanted 2 endpoints, got %d (%v)", len(endpoints), err) | |
33 | 39 | } |
34 | 40 | |
35 | 41 | instancer.Update(sd.Event{Instances: []string{}}) |
42 | ||
36 | 43 | select { |
37 | 44 | case <-ca: |
38 | 45 | t.Logf("endpoint a closed, good") |
39 | 46 | case <-time.After(time.Millisecond): |
40 | 47 | t.Errorf("didn't close the deleted instance in time") |
41 | 48 | } |
49 | ||
42 | 50 | select { |
43 | 51 | case <-cb: |
44 | 52 | t.Logf("endpoint b closed, good") |
45 | 53 | case <-time.After(time.Millisecond): |
46 | 54 | t.Errorf("didn't close the deleted instance in time") |
47 | 55 | } |
56 | ||
48 | 57 | if endpoints, err := endpointer.Endpoints(); err != nil { |
49 | 58 | t.Errorf("unepected error %v", err) |
50 | 59 | } else if want, have := 0, len(endpoints); want != have { |
52 | 61 | } |
53 | 62 | |
54 | 63 | endpointer.Close() |
64 | ||
55 | 65 | instancer.Update(sd.Event{Instances: []string{"a"}}) |
56 | 66 | // TODO verify that on Close the endpointer fully disconnects from the instancer. |
57 | 67 | // Unfortunately, because we use instance.Cache, this test cannot be in the sd package, |
77 | 87 | type closer chan struct{} |
78 | 88 | |
79 | 89 | func (c closer) Close() error { close(c); return nil } |
90 | ||
91 | func within(d time.Duration, f func() bool) bool { | |
92 | deadline := time.Now().Add(d) | |
93 | for time.Now().Before(deadline) { | |
94 | if f() { | |
95 | return true | |
96 | } | |
97 | time.Sleep(d / 10) | |
98 | } | |
99 | return false | |
100 | } |