Skip to content

Commit ee17a90

Browse files
Add regression test
1 parent da46aea commit ee17a90

File tree

1 file changed

+51
-2
lines changed

1 file changed

+51
-2
lines changed

test/integration/connection-monitoring-and-pooling/connection_pool.test.ts

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
import { once } from 'node:events';
22

33
import { expect } from 'chai';
4-
5-
import { type ConnectionPoolCreatedEvent, type Db, type MongoClient } from '../../mongodb';
4+
import * as sinon from 'sinon';
5+
6+
import {
7+
type ConnectionPoolCreatedEvent,
8+
type Db,
9+
type MongoClient,
10+
type Server
11+
} from '../../mongodb';
612
import { clearFailPoint, configureFailPoint, sleep } from '../../tools/utils';
713

814
describe('Connection Pool', function () {
@@ -150,4 +156,47 @@ describe('Connection Pool', function () {
150156
});
151157
});
152158
});
159+
160+
describe(
161+
'background task cleans up connections when minPoolSize=0',
162+
{ requires: { topology: 'single' } },
163+
function () {
164+
let server: Server;
165+
let ensureMinPoolSizeSpy: sinon.SinonSpy;
166+
167+
beforeEach(async function () {
168+
client = this.configuration.newClient(
169+
{},
170+
{
171+
maxConnecting: 10,
172+
minPoolSize: 0,
173+
maxIdleTimeMS: 100
174+
}
175+
);
176+
177+
await client.connect();
178+
179+
await Promise.all(
180+
Array.from({ length: 10 }).map(() => {
181+
return client.db('foo').collection('bar').insertOne({ a: 1 });
182+
})
183+
);
184+
185+
server = Array.from(client.topology.s.servers.entries())[0][1];
186+
expect(
187+
server.pool.availableConnectionCount,
188+
'pool was not filled with connections'
189+
).to.equal(10);
190+
191+
ensureMinPoolSizeSpy = sinon.spy(server.pool, 'ensureMinPoolSize');
192+
});
193+
194+
it('prunes idle connections when minPoolSize=0', async function () {
195+
await sleep(500);
196+
expect(server.pool.availableConnectionCount).to.equal(0);
197+
198+
expect(ensureMinPoolSizeSpy).to.have.been.called;
199+
});
200+
}
201+
);
153202
});

0 commit comments

Comments
 (0)