@@ -3,10 +3,9 @@ import { once } from 'node:events';
3
3
import { expect } from 'chai' ;
4
4
5
5
import {
6
- type ConnectionCheckedInEvent ,
7
- type ConnectionCheckedOutEvent ,
8
6
type ConnectionPoolCreatedEvent ,
9
7
type Db ,
8
+ MONGO_CLIENT_EVENTS ,
10
9
type MongoClient
11
10
} from '../../mongodb' ;
12
11
import { clearFailPoint , configureFailPoint , sleep } from '../../tools/utils' ;
@@ -104,10 +103,13 @@ describe('Connection Pool', function () {
104
103
'a connection pool emits checked in events for closed connections' ,
105
104
{ requires : { mongodb : '>=4.4' , topology : 'single' } } ,
106
105
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 ) ;
111
113
112
114
const inserts = Promise . allSettled ( [
113
115
client . db ( 'test' ) . collection ( 'test' ) . insertOne ( { a : 1 } ) ,
@@ -116,19 +118,28 @@ describe('Connection Pool', function () {
116
118
] ) ;
117
119
118
120
// 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
+ }
120
124
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 } ` ) ;
124
128
125
129
await client . close ( ) ;
126
130
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
+ ) ;
130
136
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
+ ) ;
132
143
133
144
await inserts ;
134
145
}
0 commit comments