|
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,93 @@ 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 | + // 1. Create a test client that listens to CMAP events, with maxConnecting=100. |
| 203 | + client = this.configuration.newClient({}, { maxConnecting: 100 }); |
| 204 | + |
| 205 | + client.on('connectionCheckOutFailed', e => checkoutFailedEvents.push(e)); |
| 206 | + client.on('connectionPoolCleared', e => poolClearedEvents.push(e)); |
| 207 | + |
| 208 | + await client.connect(); |
| 209 | + |
| 210 | + // 2. Run the following commands to set up the rate limiter. |
| 211 | + // ```python |
| 212 | + // client.admin.command("setParameter", 1, ingressConnectionEstablishmentRateLimiterEnabled=True) |
| 213 | + // client.admin.command("setParameter", 1, ingressConnectionEstablishmentRatePerSec=20) |
| 214 | + // client.admin.command("setParameter", 1, ingressConnectionEstablishmentBurstCapacitySecs=1) |
| 215 | + // client.admin.command("setParameter", 1, ingressConnectionEstablishmentMaxQueueDepth=1) |
| 216 | + // ``` |
| 217 | + const admin = client.db('admin').admin(); |
| 218 | + await admin.command({ |
| 219 | + setParameter: 1, |
| 220 | + ingressConnectionEstablishmentRateLimiterEnabled: true |
| 221 | + }); |
| 222 | + await admin.command({ |
| 223 | + setParameter: 1, |
| 224 | + ingressConnectionEstablishmentRatePerSec: 20 |
| 225 | + }); |
| 226 | + await admin.command({ |
| 227 | + setParameter: 1, |
| 228 | + ingressConnectionEstablishmentBurstCapacitySecs: 1 |
| 229 | + }); |
| 230 | + await admin.command({ |
| 231 | + setParameter: 1, |
| 232 | + ingressConnectionEstablishmentMaxQueueDepth: 1 |
| 233 | + }); |
| 234 | + |
| 235 | + // 3. Add a document to the test collection so that the sleep operations will actually block: |
| 236 | + // `client.test.test.insert_one({})`. |
| 237 | + await client.db('test').collection('test').insertOne({}); |
| 238 | + }); |
| 239 | + |
| 240 | + afterEach(async function () { |
| 241 | + // 7. Sleep for 1 second to clear the rate limiter. |
| 242 | + await sleep(1000); |
| 243 | + |
| 244 | + // 8. Ensure that the following command runs at test teardown even if the test fails. |
| 245 | + // `client.admin("setParameter", 1, ingressConnectionEstablishmentRateLimiterEnabled=False)`. |
| 246 | + const admin = client.db('admin').admin(); |
| 247 | + await admin.command({ |
| 248 | + setParameter: 1, |
| 249 | + ingressConnectionEstablishmentRateLimiterEnabled: false |
| 250 | + }); |
| 251 | + |
| 252 | + await client.close(); |
| 253 | + }); |
| 254 | + |
| 255 | + it( |
| 256 | + 'does not clear the pool when connections are closed due to connection storms', |
| 257 | + { |
| 258 | + requires: { |
| 259 | + // This test requires MongoDB 7.0+. |
| 260 | + mongodb: '>=7.0' // rate limiting added in 7.0 |
| 261 | + } |
| 262 | + }, |
| 263 | + async function () { |
| 264 | + // 4. Run the following find command on the collection in 100 parallel threads/coroutines. Run these commands concurrently |
| 265 | + // but block on their completion, and ignore errors raised by the command. |
| 266 | + // `client.test.test.find_one({"$where": "function() { sleep(2000); return true; }})` |
| 267 | + await Promise.allSettled( |
| 268 | + Array.from({ length: 100 }).map(() => |
| 269 | + client |
| 270 | + .db('test') |
| 271 | + .collection('test') |
| 272 | + .findOne({ $where: 'function() { sleep(2000); return true; }' }) |
| 273 | + ) |
| 274 | + ); |
| 275 | + |
| 276 | + // 5. Assert that at least 10 `ConnectionCheckOutFailedEvent` occurred. |
| 277 | + expect(checkoutFailedEvents.length).to.be.at.least(10); |
| 278 | + |
| 279 | + // 6. Assert that 0 `PoolClearedEvent` occurred. |
| 280 | + expect(poolClearedEvents).to.be.empty; |
| 281 | + } |
| 282 | + ); |
| 283 | + }); |
190 | 284 | }); |
0 commit comments