@@ -148,17 +148,7 @@ func TestPool(t *testing.T) {
148
148
err = p .disconnect (ctx )
149
149
noerr (t , err )
150
150
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 )
162
152
assert .Nil (t , err , "error running callback: %s" , err )
163
153
if p .conns .totalSize != 0 {
164
154
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) {
287
277
defer cancel ()
288
278
err = p .disconnect (ctx )
289
279
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 )
293
282
if p .conns .totalSize != 0 {
294
283
t .Errorf ("Pool should have 0 total connections. got %d; want %d" , p .conns .totalSize , 0 )
295
284
}
@@ -711,3 +700,20 @@ func (d *sleepDialer) DialContext(ctx context.Context, network, address string)
711
700
time .Sleep (5 * time .Second )
712
701
return d .Dialer .DialContext (ctx , network , address )
713
702
}
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