Skip to content

GODRIVER-1613 Improve stability of flaky test #420

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

Merged
merged 2 commits into from
Jun 11, 2020
Merged
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
34 changes: 20 additions & 14 deletions x/mongo/driver/topology/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,17 +148,7 @@ func TestPool(t *testing.T) {
err = p.disconnect(ctx)
noerr(t, err)

// The checked out connection may be closed in a goroutine instead of disconnect.
callback := func() error {
for {
if d.lenclosed() >= 3 {
return nil
}

time.Sleep(100 * time.Millisecond)
}
}
err = assert.RunWithTimeout(callback, 3*time.Second)
assertConnectionsClosed(t, d, 3)
assert.Nil(t, err, "error running callback: %s", err)
if p.conns.totalSize != 0 {
t.Errorf("Pool should have 0 total connections. got %d; want %d", p.conns.totalSize, 0)
Expand Down Expand Up @@ -287,9 +277,8 @@ func TestPool(t *testing.T) {
defer cancel()
err = p.disconnect(ctx)
noerr(t, err)
if d.lenclosed() != 1 {
t.Errorf("Should have closed 1 connections, but didn't. got %d; want %d", d.lenclosed(), 1)
}

assertConnectionsClosed(t, d, 1)
if p.conns.totalSize != 0 {
t.Errorf("Pool should have 0 total connections. got %d; want %d", p.conns.totalSize, 0)
}
Expand Down Expand Up @@ -711,3 +700,20 @@ func (d *sleepDialer) DialContext(ctx context.Context, network, address string)
time.Sleep(5 * time.Second)
return d.Dialer.DialContext(ctx, network, address)
}

func assertConnectionsClosed(t *testing.T, dialer *dialer, expectedClosedCount int) {
t.Helper()

callback := func() error {
for {
if dialer.lenclosed() == expectedClosedCount {
return nil
}

time.Sleep(100 * time.Millisecond)
}
}

err := assert.RunWithTimeout(callback, 3*time.Second)
assert.Nil(t, err, "timed out waiting for connection closed count to hit %d", expectedClosedCount)
}