mirror of
https://github.com/go-kit/kit.git
synced 2025-07-17 01:12:38 +02:00
sd: fix TestDefaultEndpointer flake, hopefully
This commit is contained in:
@ -27,25 +27,34 @@ func TestDefaultEndpointer(t *testing.T) {
|
|||||||
instancer.Update(sd.Event{Instances: []string{"a", "b"}})
|
instancer.Update(sd.Event{Instances: []string{"a", "b"}})
|
||||||
|
|
||||||
endpointer := sd.NewEndpointer(instancer, f, log.NewNopLogger(), sd.InvalidateOnError(time.Minute))
|
endpointer := sd.NewEndpointer(instancer, f, log.NewNopLogger(), sd.InvalidateOnError(time.Minute))
|
||||||
if endpoints, err := endpointer.Endpoints(); err != nil {
|
|
||||||
t.Errorf("unepected error %v", err)
|
var (
|
||||||
} else if want, have := 2, len(endpoints); want != have {
|
endpoints []endpoint.Endpoint
|
||||||
t.Errorf("want %d, have %d", want, have)
|
err error
|
||||||
|
)
|
||||||
|
if !within(time.Second, func() bool {
|
||||||
|
endpoints, err = endpointer.Endpoints()
|
||||||
|
return err == nil && len(endpoints) == 2
|
||||||
|
}) {
|
||||||
|
t.Errorf("wanted 2 endpoints, got %d (%v)", len(endpoints), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
instancer.Update(sd.Event{Instances: []string{}})
|
instancer.Update(sd.Event{Instances: []string{}})
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-ca:
|
case <-ca:
|
||||||
t.Logf("endpoint a closed, good")
|
t.Logf("endpoint a closed, good")
|
||||||
case <-time.After(time.Millisecond):
|
case <-time.After(time.Millisecond):
|
||||||
t.Errorf("didn't close the deleted instance in time")
|
t.Errorf("didn't close the deleted instance in time")
|
||||||
}
|
}
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-cb:
|
case <-cb:
|
||||||
t.Logf("endpoint b closed, good")
|
t.Logf("endpoint b closed, good")
|
||||||
case <-time.After(time.Millisecond):
|
case <-time.After(time.Millisecond):
|
||||||
t.Errorf("didn't close the deleted instance in time")
|
t.Errorf("didn't close the deleted instance in time")
|
||||||
}
|
}
|
||||||
|
|
||||||
if endpoints, err := endpointer.Endpoints(); err != nil {
|
if endpoints, err := endpointer.Endpoints(); err != nil {
|
||||||
t.Errorf("unepected error %v", err)
|
t.Errorf("unepected error %v", err)
|
||||||
} else if want, have := 0, len(endpoints); want != have {
|
} else if want, have := 0, len(endpoints); want != have {
|
||||||
@ -53,6 +62,7 @@ func TestDefaultEndpointer(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
endpointer.Close()
|
endpointer.Close()
|
||||||
|
|
||||||
instancer.Update(sd.Event{Instances: []string{"a"}})
|
instancer.Update(sd.Event{Instances: []string{"a"}})
|
||||||
// TODO verify that on Close the endpointer fully disconnects from the instancer.
|
// TODO verify that on Close the endpointer fully disconnects from the instancer.
|
||||||
// Unfortunately, because we use instance.Cache, this test cannot be in the sd package,
|
// 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) {
|
|||||||
type closer chan struct{}
|
type closer chan struct{}
|
||||||
|
|
||||||
func (c closer) Close() error { close(c); return nil }
|
func (c closer) Close() error { close(c); return nil }
|
||||||
|
|
||||||
|
func within(d time.Duration, f func() bool) bool {
|
||||||
|
deadline := time.Now().Add(d)
|
||||||
|
for time.Now().Before(deadline) {
|
||||||
|
if f() {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
time.Sleep(d / 10)
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user