Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sd: fix TestDefaultEndpointer flake, hopefully #596

Merged
merged 2 commits into from
Aug 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions sd/endpointer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,42 @@ func TestDefaultEndpointer(t *testing.T) {
instancer.Update(sd.Event{Instances: []string{"a", "b"}})

endpointer := sd.NewEndpointer(instancer, f, log.NewNopLogger(), sd.InvalidateOnError(time.Minute))
if endpoints, err := endpointer.Endpoints(); err != nil {
t.Errorf("unepected error %v", err)
} else if want, have := 2, len(endpoints); want != have {
t.Errorf("want %d, have %d", want, have)

var (
endpoints []endpoint.Endpoint
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{}})

select {
case <-ca:
t.Logf("endpoint a closed, good")
case <-time.After(time.Millisecond):
t.Errorf("didn't close the deleted instance in time")
}

select {
case <-cb:
t.Logf("endpoint b closed, good")
case <-time.After(time.Millisecond):
t.Errorf("didn't close the deleted instance in time")
}

if endpoints, err := endpointer.Endpoints(); err != nil {
t.Errorf("unepected error %v", err)
} else if want, have := 0, len(endpoints); want != have {
t.Errorf("want %d, have %d", want, have)
}

endpointer.Close()

instancer.Update(sd.Event{Instances: []string{"a"}})
// 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,
Expand All @@ -78,3 +88,14 @@ func (m *mockInstancer) Deregister(ch chan<- sd.Event) {
type closer chan struct{}

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
}
2 changes: 1 addition & 1 deletion util/conn/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestManager(t *testing.T) {
// First takes should fail.
for i := 0; i < 10; i++ {
if conn = mgr.Take(); conn != nil {
t.Fatalf("want nil conn, got real conn")
t.Fatalf("iteration %d: want nil conn, got real conn", i)
}
}

Expand Down