|
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, |
@@ -187,4 +191,83 @@ describe('Server Discovery and Monitoring Prose Tests', function () { |
187 | 191 | } |
188 | 192 | }); |
189 | 193 | }); |
| 194 | + |
| 195 | + context('Connection Pool Backpressure', function () { |
| 196 | + let client: MongoClient; |
| 197 | + let utilClient: MongoClient; |
| 198 | + const checkoutFailedEvents: Array<ConnectionCheckOutFailedEvent> = []; |
| 199 | + const poolClearedEvents: Array<ConnectionPoolClearedEvent> = []; |
| 200 | + |
| 201 | + const metadata: MongoDBMetadataUI = { |
| 202 | + requires: { |
| 203 | + mongodb: '>=7.0' |
| 204 | + } |
| 205 | + }; |
| 206 | + |
| 207 | + beforeEach(async function () { |
| 208 | + client = this.configuration.newClient({}, { maxConnecting: 20 }); |
| 209 | + |
| 210 | + client.on('connectionCheckOutFailed', e => checkoutFailedEvents.push(e)); |
| 211 | + client.on('connectionPoolCleared', e => poolClearedEvents.push(e)); |
| 212 | + |
| 213 | + await client.connect(); |
| 214 | + |
| 215 | + utilClient = this.configuration.newClient(); |
| 216 | + await utilClient.connect(); |
| 217 | + |
| 218 | + const admin = utilClient.db('admin').admin(); |
| 219 | + await admin.command({ |
| 220 | + setParameter: 1, |
| 221 | + ingressConnectionEstablishmentRateLimiterEnabled: true |
| 222 | + }); |
| 223 | + await admin.command({ |
| 224 | + setParameter: 1, |
| 225 | + ingressConnectionEstablishmentRatePerSec: 20 |
| 226 | + }); |
| 227 | + await admin.command({ |
| 228 | + setParameter: 1, |
| 229 | + ingressConnectionEstablishmentBurstCapacitySecs: 1 |
| 230 | + }); |
| 231 | + await admin.command({ |
| 232 | + setParameter: 1, |
| 233 | + ingressConnectionEstablishmentMaxQueueDepth: 1 |
| 234 | + }); |
| 235 | + |
| 236 | + await utilClient.db('test').collection('test').insertOne({}); |
| 237 | + }); |
| 238 | + |
| 239 | + afterEach(async function () { |
| 240 | + const admin = utilClient.db('admin').admin(); |
| 241 | + await admin.command({ |
| 242 | + setParameter: 1, |
| 243 | + ingressConnectionEstablishmentRateLimiterEnabled: false |
| 244 | + }); |
| 245 | + |
| 246 | + await utilClient.close(); |
| 247 | + await client.close(); |
| 248 | + }); |
| 249 | + |
| 250 | + it('runs', metadata, async function () { |
| 251 | + await Promise.allSettled( |
| 252 | + Array.from({ length: 10 }).map(() => |
| 253 | + client |
| 254 | + .db('test') |
| 255 | + .collection('test') |
| 256 | + .findOne({ $where: 'function() { sleep(2000); return true; }' }) |
| 257 | + ) |
| 258 | + ); |
| 259 | + |
| 260 | + await Promise.allSettled( |
| 261 | + Array.from({ length: 100 }).map(() => |
| 262 | + client |
| 263 | + .db('test') |
| 264 | + .collection('test') |
| 265 | + .findOne({ $where: 'function() { sleep(2000); return true; }' }) |
| 266 | + ) |
| 267 | + ); |
| 268 | + |
| 269 | + expect(poolClearedEvents).to.be.empty; |
| 270 | + expect(checkoutFailedEvents.length).to.be.greaterThan(10); |
| 271 | + }); |
| 272 | + }); |
190 | 273 | }); |
0 commit comments