@@ -2,13 +2,7 @@ import { once } from 'node:events';
2
2
3
3
import { expect } from 'chai' ;
4
4
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' ;
12
6
import { clearFailPoint , configureFailPoint , sleep } from '../../tools/utils' ;
13
7
14
8
describe ( 'Connection Pool' , function ( ) {
@@ -104,10 +98,13 @@ describe('Connection Pool', function () {
104
98
'a connection pool emits checked in events for closed connections' ,
105
99
{ requires : { mongodb : '>=4.4' , topology : 'single' } } ,
106
100
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 ) ;
111
108
112
109
const inserts = Promise . allSettled ( [
113
110
client . db ( 'test' ) . collection ( 'test' ) . insertOne ( { a : 1 } ) ,
@@ -116,19 +113,28 @@ describe('Connection Pool', function () {
116
113
] ) ;
117
114
118
115
// 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
+ }
120
119
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 } ` ) ;
124
123
125
124
await client . close ( ) ;
126
125
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
+ ) ;
130
131
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
+ ) ;
132
138
133
139
await inserts ;
134
140
}
0 commit comments