Codebase list golang-github-go-kit-kit / ab2a990
sd: fix TestDefaultEndpointer flake, hopefully Peter Bourgon 6 years ago
1 changed file(s) with 25 addition(s) and 4 deletion(s). Raw diff Collapse all Expand all
2626 instancer.Update(sd.Event{Instances: []string{"a", "b"}})
2727
2828 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)
3339 }
3440
3541 instancer.Update(sd.Event{Instances: []string{}})
42
3643 select {
3744 case <-ca:
3845 t.Logf("endpoint a closed, good")
3946 case <-time.After(time.Millisecond):
4047 t.Errorf("didn't close the deleted instance in time")
4148 }
49
4250 select {
4351 case <-cb:
4452 t.Logf("endpoint b closed, good")
4553 case <-time.After(time.Millisecond):
4654 t.Errorf("didn't close the deleted instance in time")
4755 }
56
4857 if endpoints, err := endpointer.Endpoints(); err != nil {
4958 t.Errorf("unepected error %v", err)
5059 } else if want, have := 0, len(endpoints); want != have {
5261 }
5362
5463 endpointer.Close()
64
5565 instancer.Update(sd.Event{Instances: []string{"a"}})
5666 // TODO verify that on Close the endpointer fully disconnects from the instancer.
5767 // Unfortunately, because we use instance.Cache, this test cannot be in the sd package,
7787 type closer chan struct{}
7888
7989 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 }