@@ -2,13 +2,7 @@ import { once } from 'node:events';
22
33import { expect } from 'chai' ;
44
5- import {
6- type ConnectionCheckedInEvent ,
7- type ConnectionCheckedOutEvent ,
8- type ConnectionPoolCreatedEvent ,
9- type Db ,
10- type MongoClient
11- } from '../../mongodb' ;
5+ import { type ConnectionPoolCreatedEvent , type Db , type MongoClient } from '../../mongodb' ;
126import { clearFailPoint , configureFailPoint , sleep } from '../../tools/utils' ;
137
148describe ( 'Connection Pool' , function ( ) {
@@ -104,10 +98,13 @@ describe('Connection Pool', function () {
10498 'a connection pool emits checked in events for closed connections' ,
10599 { requires : { mongodb : '>=4.4' , topology : 'single' } } ,
106100 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 ) ) ;
101+ const allClientEvents = [ ] ;
102+ const pushToClientEvents = e => allClientEvents . push ( e ) ;
103+
104+ client
105+ . on ( 'connectionCheckedOut' , pushToClientEvents )
106+ . on ( 'connectionCheckedIn' , pushToClientEvents )
107+ . on ( 'connectionClosed' , pushToClientEvents ) ;
111108
112109 const inserts = Promise . allSettled ( [
113110 client . db ( 'test' ) . collection ( 'test' ) . insertOne ( { a : 1 } ) ,
@@ -116,19 +113,28 @@ describe('Connection Pool', function () {
116113 ] ) ;
117114
118115 // wait until all pings are pending on the server
119- while ( connectionCheckedOutEvents . length < 3 ) await sleep ( 1 ) ;
116+ while ( allClientEvents . filter ( e => e . name === 'connectionCheckedOut' ) . length < 3 ) {
117+ await sleep ( 1 ) ;
118+ }
120119
121- const insertConnectionIds = connectionCheckedOutEvents . map (
122- ( { address , connectionId } ) => ` ${ address } + ${ connectionId } `
123- ) ;
120+ const insertConnectionIds = allClientEvents
121+ . filter ( e => e . name === 'connectionCheckedOut' )
122+ . map ( ( { address , connectionId } ) => ` ${ address } + ${ connectionId } ` ) ;
124123
125124 await client . close ( ) ;
126125
127- const insertCheckIns = connectionCheckedInEvents . filter ( ( { address, connectionId } ) =>
128- insertConnectionIds . includes ( `${ address } + ${ connectionId } ` )
129- ) ;
126+ const insertCheckInAndCloses = allClientEvents
127+ . filter ( e => e . name === 'connectionCheckedIn' || e . name === 'connectionClosed' )
128+ . filter ( ( { address, connectionId } ) =>
129+ insertConnectionIds . includes ( `${ address } + ${ connectionId } ` )
130+ ) ;
130131
131- expect ( insertCheckIns ) . to . have . lengthOf ( 3 ) ;
132+ expect ( insertCheckInAndCloses ) . to . have . lengthOf ( 6 ) ;
133+
134+ // check that each check-in is followed by a close (not proceeded by one)
135+ expect ( insertCheckInAndCloses . map ( e => e . name ) ) . to . deep . equal (
136+ Array . from ( { length : 3 } , ( ) => [ 'connectionCheckedIn' , 'connectionClosed' ] ) . flat ( 1 )
137+ ) ;
132138
133139 await inserts ;
134140 }
0 commit comments