|
1 | 1 | import { expect } from 'chai'; |
2 | 2 | import { once } from 'events'; |
3 | 3 |
|
4 | | -import { type MongoClient } from '../../../src'; |
| 4 | +import { |
| 5 | + type ConnectionCheckOutFailedEvent, |
| 6 | + type ConnectionPoolClearedEvent, |
| 7 | + type MongoClient |
| 8 | +} from '../../../src'; |
5 | 9 | import { |
6 | 10 | CONNECTION_POOL_CLEARED, |
7 | 11 | CONNECTION_POOL_READY, |
8 | 12 | SERVER_HEARTBEAT_FAILED, |
9 | 13 | SERVER_HEARTBEAT_SUCCEEDED |
10 | 14 | } from '../../../src/constants'; |
| 15 | +import { sleep } from '../../tools/utils'; |
11 | 16 |
|
12 | 17 | describe('Server Discovery and Monitoring Prose Tests', function () { |
13 | 18 | context('Monitors sleep at least minHeartbeatFrequencyMS between checks', function () { |
@@ -187,4 +192,74 @@ describe('Server Discovery and Monitoring Prose Tests', function () { |
187 | 192 | } |
188 | 193 | }); |
189 | 194 | }); |
| 195 | + |
| 196 | + context('Connection Pool Backpressure', function () { |
| 197 | + let client: MongoClient; |
| 198 | + const checkoutFailedEvents: Array<ConnectionCheckOutFailedEvent> = []; |
| 199 | + const poolClearedEvents: Array<ConnectionPoolClearedEvent> = []; |
| 200 | + |
| 201 | + beforeEach(async function () { |
| 202 | + client = this.configuration.newClient({}, { maxConnecting: 100 }); |
| 203 | + |
| 204 | + client.on('connectionCheckOutFailed', e => checkoutFailedEvents.push(e)); |
| 205 | + client.on('connectionPoolCleared', e => poolClearedEvents.push(e)); |
| 206 | + |
| 207 | + await client.connect(); |
| 208 | + |
| 209 | + const admin = client.db('admin').admin(); |
| 210 | + await admin.command({ |
| 211 | + setParameter: 1, |
| 212 | + ingressConnectionEstablishmentRateLimiterEnabled: true |
| 213 | + }); |
| 214 | + await admin.command({ |
| 215 | + setParameter: 1, |
| 216 | + ingressConnectionEstablishmentRatePerSec: 20 |
| 217 | + }); |
| 218 | + await admin.command({ |
| 219 | + setParameter: 1, |
| 220 | + ingressConnectionEstablishmentBurstCapacitySecs: 1 |
| 221 | + }); |
| 222 | + await admin.command({ |
| 223 | + setParameter: 1, |
| 224 | + ingressConnectionEstablishmentMaxQueueDepth: 1 |
| 225 | + }); |
| 226 | + |
| 227 | + await client.db('test').collection('test').insertOne({}); |
| 228 | + }); |
| 229 | + |
| 230 | + afterEach(async function () { |
| 231 | + // give the time to recover from the connection storm before cleaning up. |
| 232 | + await sleep(1000); |
| 233 | + |
| 234 | + const admin = client.db('admin').admin(); |
| 235 | + await admin.command({ |
| 236 | + setParameter: 1, |
| 237 | + ingressConnectionEstablishmentRateLimiterEnabled: false |
| 238 | + }); |
| 239 | + |
| 240 | + await client.close(); |
| 241 | + }); |
| 242 | + |
| 243 | + it( |
| 244 | + 'does not clear the pool when connections are closed due to connection storms', |
| 245 | + { |
| 246 | + requires: { |
| 247 | + mongodb: '>=7.0' // rate limiting added in 7.0 |
| 248 | + } |
| 249 | + }, |
| 250 | + async function () { |
| 251 | + await Promise.allSettled( |
| 252 | + Array.from({ length: 100 }).map(() => |
| 253 | + client |
| 254 | + .db('test') |
| 255 | + .collection('test') |
| 256 | + .findOne({ $where: 'function() { sleep(2000); return true; }' }) |
| 257 | + ) |
| 258 | + ); |
| 259 | + |
| 260 | + expect(poolClearedEvents).to.be.empty; |
| 261 | + expect(checkoutFailedEvents.length).to.be.greaterThan(10); |
| 262 | + } |
| 263 | + ); |
| 264 | + }); |
190 | 265 | }); |
0 commit comments