@@ -3,7 +3,7 @@ import { EventEmitter } from 'events';
33import { clearTimeout , setTimeout } from 'timers' ;
44import { promisify } from 'util' ;
55
6- import { Connection , HostAddress , MongoClient } from '../../src' ;
6+ import { Connection , HostAddress , MongoClient , Server } from '../../src' ;
77import { ConnectionPool , ConnectionPoolOptions } from '../../src/cmap/connection_pool' ;
88import { CMAP_EVENTS } from '../../src/constants' ;
99import { makeClientMetadata , shuffle } from '../../src/utils' ;
@@ -253,11 +253,13 @@ export class ThreadContext {
253253 threads : Map < any , Thread > = new Map ( ) ;
254254 connections : Map < string , Connection > = new Map ( ) ;
255255 orphans : Set < Connection > = new Set ( ) ;
256- poolEvents = [ ] ;
256+ poolEvents : any [ ] = [ ] ;
257257 poolEventsEventEmitter = new EventEmitter ( ) ;
258258
259259 #poolOptions: Partial < ConnectionPoolOptions > ;
260260 #hostAddress: HostAddress ;
261+ #server: Server ;
262+ #originalServerPool: ConnectionPool ;
261263 #supportedOperations: ReturnType < typeof getTestOpDefinitions > ;
262264 #injectPoolStats = false ;
263265
@@ -267,12 +269,14 @@ export class ThreadContext {
267269 * @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"
268270 */
269271 constructor (
272+ server : Server ,
270273 hostAddress : HostAddress ,
271274 poolOptions : Partial < ConnectionPoolOptions > = { } ,
272275 contextOptions : { injectPoolStats : boolean }
273276 ) {
274277 this . #poolOptions = poolOptions ;
275278 this . #hostAddress = hostAddress ;
279+ this . #server = server ;
276280 this . #supportedOperations = getTestOpDefinitions ( this ) ;
277281 this . #injectPoolStats = contextOptions . injectPoolStats ;
278282 }
@@ -292,11 +296,13 @@ export class ThreadContext {
292296 }
293297
294298 createPool ( options ) {
295- this . pool = new ConnectionPool ( {
299+ this . pool = new ConnectionPool ( this . #server , {
296300 ...this . #poolOptions,
297301 ...options ,
298302 hostAddress : this . #hostAddress
299303 } ) ;
304+ this . #originalServerPool = this . #server. s . pool ;
305+ this . #server. s . pool = this . pool ;
300306 ALL_POOL_EVENTS . forEach ( eventName => {
301307 this . pool . on ( eventName , event => {
302308 if ( this . #injectPoolStats) {
@@ -312,6 +318,7 @@ export class ThreadContext {
312318 }
313319
314320 closePool ( ) {
321+ this . #server. s . pool = this . #originalServerPool;
315322 return new Promise ( resolve => {
316323 ALL_POOL_EVENTS . forEach ( ev => this . pool . removeAllListeners ( ev ) ) ;
317324 this . pool . close ( resolve ) ;
@@ -438,7 +445,10 @@ export function runCmapTestSuite(
438445) {
439446 for ( const test of tests ) {
440447 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 ;
442452
443453 beforeEach ( async function ( ) {
444454 let utilClient : MongoClient ;
@@ -479,25 +489,33 @@ export function runCmapTestSuite(
479489 }
480490
481491 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 ( ) ) ;
484494 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+
486513 threadContext = new ThreadContext (
514+ server ,
487515 hostAddress ,
488516 this . configuration . isLoadBalanced ? { loadBalanced : true } : { } ,
489517 { injectPoolStats : ! ! options ?. injectPoolStats }
490518 ) ;
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- }
501519 } finally {
502520 await utilClient . close ( ) ;
503521 }
0 commit comments