Skip to content

Commit eb83a3b

Browse files
author
Divjot Arora
authored
GODRIVER-1613 Improve stability of flaky test (#420)
* GODRIVER-1613 Improve stability of flaky test * fix typo
1 parent 324f6a9 commit eb83a3b

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

x/mongo/driver/topology/pool_test.go

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -148,17 +148,7 @@ func TestPool(t *testing.T) {
148148
err = p.disconnect(ctx)
149149
noerr(t, err)
150150

151-
// The checked out connection may be closed in a goroutine instead of disconnect.
152-
callback := func() error {
153-
for {
154-
if d.lenclosed() >= 3 {
155-
return nil
156-
}
157-
158-
time.Sleep(100 * time.Millisecond)
159-
}
160-
}
161-
err = assert.RunWithTimeout(callback, 3*time.Second)
151+
assertConnectionsClosed(t, d, 3)
162152
assert.Nil(t, err, "error running callback: %s", err)
163153
if p.conns.totalSize != 0 {
164154
t.Errorf("Pool should have 0 total connections. got %d; want %d", p.conns.totalSize, 0)
@@ -287,9 +277,8 @@ func TestPool(t *testing.T) {
287277
defer cancel()
288278
err = p.disconnect(ctx)
289279
noerr(t, err)
290-
if d.lenclosed() != 1 {
291-
t.Errorf("Should have closed 1 connections, but didn't. got %d; want %d", d.lenclosed(), 1)
292-
}
280+
281+
assertConnectionsClosed(t, d, 1)
293282
if p.conns.totalSize != 0 {
294283
t.Errorf("Pool should have 0 total connections. got %d; want %d", p.conns.totalSize, 0)
295284
}
@@ -711,3 +700,20 @@ func (d *sleepDialer) DialContext(ctx context.Context, network, address string)
711700
time.Sleep(5 * time.Second)
712701
return d.Dialer.DialContext(ctx, network, address)
713702
}
703+
704+
func assertConnectionsClosed(t *testing.T, dialer *dialer, expectedClosedCount int) {
705+
t.Helper()
706+
707+
callback := func() error {
708+
for {
709+
if dialer.lenclosed() == expectedClosedCount {
710+
return nil
711+
}
712+
713+
time.Sleep(100 * time.Millisecond)
714+
}
715+
}
716+
717+
err := assert.RunWithTimeout(callback, 3*time.Second)
718+
assert.Nil(t, err, "timed out waiting for connection closed count to hit %d", expectedClosedCount)
719+
}

0 commit comments

Comments
 (0)