|
1 | 1 | import { once } from 'node:events';
|
2 | 2 |
|
3 | 3 | 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'; |
6 | 12 | import { clearFailPoint, configureFailPoint, sleep } from '../../tools/utils';
|
7 | 13 |
|
8 | 14 | describe('Connection Pool', function () {
|
@@ -150,4 +156,47 @@ describe('Connection Pool', function () {
|
150 | 156 | });
|
151 | 157 | });
|
152 | 158 | });
|
| 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 | + ); |
153 | 202 | });
|
0 commit comments