@@ -12,6 +12,7 @@ import {
1212 type AutoEncryptionOptions ,
1313 ClientEncryption ,
1414 type Collection ,
15+ type ConnectionClosedEvent ,
1516 type Db ,
1617 FindCursor ,
1718 ListCollectionsCursor ,
@@ -400,21 +401,31 @@ describe('AbortSignal support', () => {
400401 let controller : AbortController ;
401402 let signal : AbortSignal ;
402403 let cursor : AbstractCursor < { a : number } > ;
404+ let checkedOutId ;
405+ const waitForConnectionClosed = async ( ) => {
406+ for await ( const [ ev ] of events . on ( client , 'connectionClosed' ) ) {
407+ if ( ( ev as ConnectionClosedEvent ) . connectionId === checkedOutId ) return ev ;
408+ }
409+ } ;
403410
404411 beforeEach ( async function ( ) {
412+ checkedOutId = undefined ;
405413 controller = new AbortController ( ) ;
406414 signal = controller . signal ;
407415 cursor = method ( filter , { signal } ) ;
408416 } ) ;
409417
410418 afterEach ( async function ( ) {
419+ checkedOutId = undefined ;
411420 sinon . restore ( ) ;
412421 await cursor ?. close ( ) ;
413422 } ) ;
414423
415424 it ( `rejects ${ cursorAPI . toString ( ) } ` , async ( ) => {
416425 await db . command ( { ping : 1 } , { readPreference : 'primary' } ) ; // fill the connection pool with 1 connection.
426+ const connectionClosed = waitForConnectionClosed ( ) ;
417427
428+ client . on ( 'connectionCheckedOut' , ev => ( checkedOutId = ev . connectionId ) ) ;
418429 const willBeResultBlocked = iterateUntilDocumentOrError ( cursor , cursorAPI , args ) ;
419430
420431 for ( const [ , server ] of client . topology . s . servers ) {
@@ -435,6 +446,8 @@ describe('AbortSignal support', () => {
435446 const result = await willBeResultBlocked ;
436447
437448 expect ( result ) . to . be . instanceOf ( DOMException ) ;
449+
450+ await connectionClosed ;
438451 } ) ;
439452 }
440453
@@ -461,25 +474,38 @@ describe('AbortSignal support', () => {
461474 }
462475 } ) ;
463476
477+ checkedOutId = undefined ;
464478 controller = new AbortController ( ) ;
465479 signal = controller . signal ;
466480 cursor = method ( filter , { signal } ) ;
467481 } ) ;
468482
483+ let checkedOutId ;
484+ const waitForConnectionClosed = async ( ) => {
485+ for await ( const [ ev ] of events . on ( client , 'connectionClosed' ) ) {
486+ if ( ( ev as ConnectionClosedEvent ) . connectionId === checkedOutId ) return ev ;
487+ }
488+ } ;
489+
469490 afterEach ( async function ( ) {
491+ checkedOutId = undefined ;
470492 await clearFailPoint ( this . configuration ) ;
471493 await cursor ?. close ( ) ;
472494 } ) ;
473495
474496 it ( `rejects ${ cursorAPI . toString ( ) } ` , async ( ) => {
475497 await db . command ( { ping : 1 } , { readPreference : 'primary' } ) ; // fill the connection pool with 1 connection.
498+ const connectionClosed = waitForConnectionClosed ( ) ;
476499
500+ client . on ( 'connectionCheckedOut' , ev => ( checkedOutId = ev . connectionId ) ) ;
477501 client . on ( 'commandStarted' , e => e . commandName === cursorName && controller . abort ( ) ) ;
478502 const willBeResultBlocked = iterateUntilDocumentOrError ( cursor , cursorAPI , args ) ;
479503
480504 const result = await willBeResultBlocked ;
481505
482506 expect ( result ) . to . be . instanceOf ( DOMException ) ;
507+
508+ await connectionClosed ;
483509 } ) ;
484510 }
485511
0 commit comments