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