@@ -3,7 +3,7 @@ import { EventEmitter } from 'events';
3
3
import { clearTimeout , setTimeout } from 'timers' ;
4
4
import { promisify } from 'util' ;
5
5
6
- import { Connection , HostAddress , MongoClient } from '../../src' ;
6
+ import { Connection , HostAddress , MongoClient , Server } from '../../src' ;
7
7
import { ConnectionPool , ConnectionPoolOptions } from '../../src/cmap/connection_pool' ;
8
8
import { CMAP_EVENTS } from '../../src/constants' ;
9
9
import { makeClientMetadata , shuffle } from '../../src/utils' ;
@@ -253,11 +253,13 @@ export class ThreadContext {
253
253
threads : Map < any , Thread > = new Map ( ) ;
254
254
connections : Map < string , Connection > = new Map ( ) ;
255
255
orphans : Set < Connection > = new Set ( ) ;
256
- poolEvents = [ ] ;
256
+ poolEvents : any [ ] = [ ] ;
257
257
poolEventsEventEmitter = new EventEmitter ( ) ;
258
258
259
259
#poolOptions: Partial < ConnectionPoolOptions > ;
260
260
#hostAddress: HostAddress ;
261
+ #server: Server ;
262
+ #originalServerPool: ConnectionPool ;
261
263
#supportedOperations: ReturnType < typeof getTestOpDefinitions > ;
262
264
#injectPoolStats = false ;
263
265
@@ -267,12 +269,14 @@ export class ThreadContext {
267
269
* @param poolOptions - Allows the test to pass in extra options to the pool not specified by the spec test definition, such as the environment-dependent "loadBalanced"
268
270
*/
269
271
constructor (
272
+ server : Server ,
270
273
hostAddress : HostAddress ,
271
274
poolOptions : Partial < ConnectionPoolOptions > = { } ,
272
275
contextOptions : { injectPoolStats : boolean }
273
276
) {
274
277
this . #poolOptions = poolOptions ;
275
278
this . #hostAddress = hostAddress ;
279
+ this . #server = server ;
276
280
this . #supportedOperations = getTestOpDefinitions ( this ) ;
277
281
this . #injectPoolStats = contextOptions . injectPoolStats ;
278
282
}
@@ -292,11 +296,13 @@ export class ThreadContext {
292
296
}
293
297
294
298
createPool ( options ) {
295
- this . pool = new ConnectionPool ( {
299
+ this . pool = new ConnectionPool ( this . #server , {
296
300
...this . #poolOptions,
297
301
...options ,
298
302
hostAddress : this . #hostAddress
299
303
} ) ;
304
+ this . #originalServerPool = this . #server. s . pool ;
305
+ this . #server. s . pool = this . pool ;
300
306
ALL_POOL_EVENTS . forEach ( eventName => {
301
307
this . pool . on ( eventName , event => {
302
308
if ( this . #injectPoolStats) {
@@ -312,6 +318,7 @@ export class ThreadContext {
312
318
}
313
319
314
320
closePool ( ) {
321
+ this . #server. s . pool = this . #originalServerPool;
315
322
return new Promise ( resolve => {
316
323
ALL_POOL_EVENTS . forEach ( ev => this . pool . removeAllListeners ( ev ) ) ;
317
324
this . pool . close ( resolve ) ;
@@ -438,7 +445,10 @@ export function runCmapTestSuite(
438
445
) {
439
446
for ( const test of tests ) {
440
447
describe ( test . name , function ( ) {
441
- let hostAddress : HostAddress , threadContext : ThreadContext , client : MongoClient ;
448
+ let hostAddress : HostAddress ,
449
+ server : Server ,
450
+ threadContext : ThreadContext ,
451
+ client : MongoClient ;
442
452
443
453
beforeEach ( async function ( ) {
444
454
let utilClient : MongoClient ;
@@ -479,25 +489,33 @@ export function runCmapTestSuite(
479
489
}
480
490
481
491
try {
482
- const serverMap = utilClient . topology . s . description . servers ;
483
- const hosts = shuffle ( serverMap . keys ( ) ) ;
492
+ const serverDescriptionMap = utilClient . topology ? .s . description . servers ;
493
+ const hosts = shuffle ( serverDescriptionMap . keys ( ) ) ;
484
494
const selectedHostUri = hosts [ 0 ] ;
485
- hostAddress = serverMap . get ( selectedHostUri ) . hostAddress ;
495
+ hostAddress = serverDescriptionMap . get ( selectedHostUri ) . hostAddress ;
496
+
497
+ client = this . configuration . newClient (
498
+ `mongodb://${ hostAddress } /${
499
+ this . configuration . isLoadBalanced ? '?loadBalanced=true' : '?directConnection=true'
500
+ } `
501
+ ) ;
502
+ await client . connect ( ) ;
503
+ if ( test . failPoint ) {
504
+ await client . db ( 'admin' ) . command ( test . failPoint ) ;
505
+ }
506
+
507
+ const serverMap = client . topology ?. s . servers ;
508
+ server = serverMap ?. get ( selectedHostUri ) ;
509
+ if ( ! server ) {
510
+ throw new Error ( 'Failed to retrieve server for test' ) ;
511
+ }
512
+
486
513
threadContext = new ThreadContext (
514
+ server ,
487
515
hostAddress ,
488
516
this . configuration . isLoadBalanced ? { loadBalanced : true } : { } ,
489
517
{ injectPoolStats : ! ! options ?. injectPoolStats }
490
518
) ;
491
-
492
- if ( test . failPoint ) {
493
- client = this . configuration . newClient (
494
- `mongodb://${ hostAddress } /${
495
- this . configuration . isLoadBalanced ? '?loadBalanced=true' : '?directConnection=true'
496
- } `
497
- ) ;
498
- await client . connect ( ) ;
499
- await client . db ( 'admin' ) . command ( test . failPoint ) ;
500
- }
501
519
} finally {
502
520
await utilClient . close ( ) ;
503
521
}
0 commit comments