Skip to content

Commit d14bdbf

Browse files
committed
Add comments and defer pool.close() in new unit tests.
1 parent a28382a commit d14bdbf

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

x/mongo/driver/topology/pool.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,12 @@ func (p *pool) checkInNoEvent(conn *connection) error {
596596
return ErrWrongPool
597597
}
598598

599+
// Bump the connection idle deadline here because we're about to make the connection "available".
600+
// The idle deadline is used to determine when a connection has reached its max idle time and
601+
// should be closed. A connection reaches its max idle time when it has been "available" in the
602+
// idle connections stack for more than the configured duration (maxIdleTimeMS). Set it before
603+
// we call connectionPerished(), which checks the idle deadline, because a newly "available"
604+
// connection should never be perished due to max idle time.
599605
conn.bumpIdleDeadline()
600606

601607
if reason, perished := connectionPerished(conn); perished {

x/mongo/driver/topology/pool_test.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,7 @@ func TestPool(t *testing.T) {
857857
}, WithDialer(func(Dialer) Dialer { return d }))
858858
err := p.ready()
859859
noerr(t, err)
860+
defer p.close(context.Background())
860861

861862
c, err := p.checkOut(context.Background())
862863
noerr(t, err)
@@ -871,8 +872,6 @@ func TestPool(t *testing.T) {
871872
assert.Equalf(t, 0, d.lenclosed(), "should have closed 0 connections")
872873
assert.Equalf(t, 1, p.availableConnectionCount(), "should have 1 idle connections in pool")
873874
assert.Equalf(t, 1, p.totalConnectionCount(), "should have 1 total connection in pool")
874-
875-
p.close(context.Background())
876875
})
877876
t.Run("sets minPoolSize connection idle deadline", func(t *testing.T) {
878877
t.Parallel()
@@ -892,14 +891,15 @@ func TestPool(t *testing.T) {
892891
}, WithDialer(func(Dialer) Dialer { return d }))
893892
err := p.ready()
894893
noerr(t, err)
894+
defer p.close(context.Background())
895895

896896
// Wait for maintain() to open 3 connections.
897897
assertConnectionsOpened(t, d, 3)
898898

899899
// Sleep for 100ms, which will exceed the 10ms connection idle timeout, then try to check
900900
// out a connection. Expect that all minPoolSize connections checked into the pool by
901-
// maintain() have passed their idle deadline, so checkOut() close all 3 connections and
902-
// try to create a new connection.
901+
// maintain() have passed their idle deadline, so checkOut() closes all 3 connections
902+
// and tries to create a new connection.
903903
time.Sleep(100 * time.Millisecond)
904904
_, err = p.checkOut(context.Background())
905905
noerr(t, err)
@@ -908,8 +908,6 @@ func TestPool(t *testing.T) {
908908
assert.Equalf(t, 4, d.lenopened(), "should have opened 4 connections")
909909
assert.Equalf(t, 0, p.availableConnectionCount(), "should have 0 idle connections in pool")
910910
assert.Equalf(t, 1, p.totalConnectionCount(), "should have 1 total connection in pool")
911-
912-
p.close(context.Background())
913911
})
914912
})
915913
t.Run("maintain", func(t *testing.T) {

0 commit comments

Comments
 (0)