@@ -3,10 +3,9 @@ import { once } from 'node:events';
33import { expect } from 'chai' ;
44
55import {
6- type ConnectionCheckedInEvent ,
7- type ConnectionCheckedOutEvent ,
86 type ConnectionPoolCreatedEvent ,
97 type Db ,
8+ MONGO_CLIENT_EVENTS ,
109 type MongoClient
1110} from '../../mongodb' ;
1211import { clearFailPoint , configureFailPoint , sleep } from '../../tools/utils' ;
@@ -104,10 +103,13 @@ describe('Connection Pool', function () {
104103 'a connection pool emits checked in events for closed connections' ,
105104 { requires : { mongodb : '>=4.4' , topology : 'single' } } ,
106105 async ( ) => {
107- const connectionCheckedOutEvents : ConnectionCheckedOutEvent [ ] = [ ] ;
108- client . on ( 'connectionCheckedOut' , event => connectionCheckedOutEvents . push ( event ) ) ;
109- const connectionCheckedInEvents : ConnectionCheckedInEvent [ ] = [ ] ;
110- client . on ( 'connectionCheckedIn' , event => connectionCheckedInEvents . push ( event ) ) ;
106+ const allClientEvents = [ ] ;
107+ const pushToClientEvents = e => allClientEvents . push ( e ) ;
108+
109+ client
110+ . on ( 'connectionCheckedOut' , pushToClientEvents )
111+ . on ( 'connectionCheckedIn' , pushToClientEvents )
112+ . on ( 'connectionClosed' , pushToClientEvents ) ;
111113
112114 const inserts = Promise . allSettled ( [
113115 client . db ( 'test' ) . collection ( 'test' ) . insertOne ( { a : 1 } ) ,
@@ -116,19 +118,28 @@ describe('Connection Pool', function () {
116118 ] ) ;
117119
118120 // wait until all pings are pending on the server
119- while ( connectionCheckedOutEvents . length < 3 ) await sleep ( 1 ) ;
121+ while ( allClientEvents . filter ( e => e . name === 'connectionCheckedOut' ) . length < 3 ) {
122+ await sleep ( 1 ) ;
123+ }
120124
121- const insertConnectionIds = connectionCheckedOutEvents . map (
122- ( { address , connectionId } ) => ` ${ address } + ${ connectionId } `
123- ) ;
125+ const insertConnectionIds = allClientEvents
126+ . filter ( e => e . name === 'connectionCheckedOut' )
127+ . map ( ( { address , connectionId } ) => ` ${ address } + ${ connectionId } ` ) ;
124128
125129 await client . close ( ) ;
126130
127- const insertCheckIns = connectionCheckedInEvents . filter ( ( { address, connectionId } ) =>
128- insertConnectionIds . includes ( `${ address } + ${ connectionId } ` )
129- ) ;
131+ const insertCheckInAndCloses = allClientEvents
132+ . filter ( e => e . name === 'connectionCheckedIn' || e . name === 'connectionClosed' )
133+ . filter ( ( { address, connectionId } ) =>
134+ insertConnectionIds . includes ( `${ address } + ${ connectionId } ` )
135+ ) ;
130136
131- expect ( insertCheckIns ) . to . have . lengthOf ( 3 ) ;
137+ expect ( insertCheckInAndCloses ) . to . have . lengthOf ( 6 ) ;
138+
139+ // check that each check-in is followed by a close (not proceeded by one)
140+ expect ( insertCheckInAndCloses . map ( e => e . name ) ) . to . deep . equal (
141+ Array . from ( { length : 3 } , ( ) => [ 'connectionCheckedIn' , 'connectionClosed' ] ) . flat ( 1 )
142+ ) ;
132143
133144 await inserts ;
134145 }
0 commit comments