Skip to content

Commit 8e03b4c

Browse files
authored
Merge pull request go-kit#596 from go-kit/fix-test-flake
sd: fix TestDefaultEndpointer flake, hopefully
2 parents 19463ea + aa9583c commit 8e03b4c

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

sd/endpointer_test.go

+25-4
Original file line numberDiff line numberDiff line change
@@ -27,32 +27,42 @@ func TestDefaultEndpointer(t *testing.T) {
2727
instancer.Update(sd.Event{Instances: []string{"a", "b"}})
2828

2929
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)
3440
}
3541

3642
instancer.Update(sd.Event{Instances: []string{}})
43+
3744
select {
3845
case <-ca:
3946
t.Logf("endpoint a closed, good")
4047
case <-time.After(time.Millisecond):
4148
t.Errorf("didn't close the deleted instance in time")
4249
}
50+
4351
select {
4452
case <-cb:
4553
t.Logf("endpoint b closed, good")
4654
case <-time.After(time.Millisecond):
4755
t.Errorf("didn't close the deleted instance in time")
4856
}
57+
4958
if endpoints, err := endpointer.Endpoints(); err != nil {
5059
t.Errorf("unepected error %v", err)
5160
} else if want, have := 0, len(endpoints); want != have {
5261
t.Errorf("want %d, have %d", want, have)
5362
}
5463

5564
endpointer.Close()
65+
5666
instancer.Update(sd.Event{Instances: []string{"a"}})
5767
// TODO verify that on Close the endpointer fully disconnects from the instancer.
5868
// 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) {
7888
type closer chan struct{}
7989

8090
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+
}

util/conn/manager_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func TestManager(t *testing.T) {
4040
// First takes should fail.
4141
for i := 0; i < 10; i++ {
4242
if conn = mgr.Take(); conn != nil {
43-
t.Fatalf("want nil conn, got real conn")
43+
t.Fatalf("iteration %d: want nil conn, got real conn", i)
4444
}
4545
}
4646

0 commit comments

Comments
 (0)