@@ -66,81 +66,88 @@ describe('Connection Pool', function () {
6666 } ) ;
6767 } ) ;
6868
69- describe (
70- 'ConnectionCheckedInEvent' ,
71- { requires : { mongodb : '>=4.4' , topology : 'single' } } ,
72- function ( ) {
73- let client : MongoClient ;
69+ const metadata : MongoDBMetadataUI = { requires : { mongodb : '>=4.4' , topology : 'single' } } ;
70+
71+ describe ( 'ConnectionCheckedInEvent' , metadata , function ( ) {
72+ let client : MongoClient ;
73+
74+ beforeEach ( async function ( ) {
75+ if ( ! this . configuration . filters . MongoDBVersionFilter . filter ( { metadata } ) ) {
76+ return ;
77+ }
78+ if ( ! this . configuration . filters . MongoDBTopologyFilter . filter ( { metadata } ) ) {
79+ return ;
80+ }
81+
82+ await configureFailPoint ( this . configuration , {
83+ configureFailPoint : 'failCommand' ,
84+ mode : 'alwaysOn' ,
85+ data : {
86+ failCommands : [ 'insert' ] ,
87+ blockConnection : true ,
88+ blockTimeMS : 500
89+ }
90+ } ) ;
7491
75- beforeEach ( async function ( ) {
76- await configureFailPoint ( this . configuration , {
77- configureFailPoint : 'failCommand' ,
78- mode : 'alwaysOn' ,
79- data : {
80- failCommands : [ 'insert' ] ,
81- blockConnection : true ,
82- blockTimeMS : 500
92+ client = this . configuration . newClient ( ) ;
93+ await client . connect ( ) ;
94+ await Promise . all ( Array . from ( { length : 100 } , ( ) => client . db ( ) . command ( { ping : 1 } ) ) ) ;
95+ } ) ;
96+
97+ afterEach ( async function ( ) {
98+ if ( this . configuration . filters . MongoDBVersionFilter . filter ( { metadata } ) ) {
99+ await clearFailPoint ( this . configuration ) ;
100+ }
101+ await client . close ( ) ;
102+ } ) ;
103+
104+ describe ( 'when a MongoClient is closed' , function ( ) {
105+ it (
106+ 'a connection pool emits checked in events for closed connections' ,
107+ metadata ,
108+ async ( ) => {
109+ const allClientEvents = [ ] ;
110+ const pushToClientEvents = e => allClientEvents . push ( e ) ;
111+
112+ client
113+ . on ( 'connectionCheckedOut' , pushToClientEvents )
114+ . on ( 'connectionCheckedIn' , pushToClientEvents )
115+ . on ( 'connectionClosed' , pushToClientEvents ) ;
116+
117+ const inserts = Promise . allSettled ( [
118+ client . db ( 'test' ) . collection ( 'test' ) . insertOne ( { a : 1 } ) ,
119+ client . db ( 'test' ) . collection ( 'test' ) . insertOne ( { a : 1 } ) ,
120+ client . db ( 'test' ) . collection ( 'test' ) . insertOne ( { a : 1 } )
121+ ] ) ;
122+
123+ // wait until all pings are pending on the server
124+ while ( allClientEvents . filter ( e => e . name === 'connectionCheckedOut' ) . length < 3 ) {
125+ await sleep ( 1 ) ;
83126 }
84- } ) ;
85127
86- client = this . configuration . newClient ( ) ;
87- await client . connect ( ) ;
88- await Promise . all ( Array . from ( { length : 100 } , ( ) => client . db ( ) . command ( { ping : 1 } ) ) ) ;
89- } ) ;
128+ const insertConnectionIds = allClientEvents
129+ . filter ( e => e . name === 'connectionCheckedOut' )
130+ . map ( ( { address, connectionId } ) => `${ address } + ${ connectionId } ` ) ;
90131
91- afterEach ( async function ( ) {
92- await clearFailPoint ( this . configuration ) ;
93- await client . close ( ) ;
94- } ) ;
132+ await client . close ( ) ;
95133
96- describe ( 'when a MongoClient is closed' , function ( ) {
97- it (
98- 'a connection pool emits checked in events for closed connections' ,
99- { requires : { mongodb : '>=4.4' , topology : 'single' } } ,
100- async ( ) => {
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 ) ;
108-
109- const inserts = Promise . allSettled ( [
110- client . db ( 'test' ) . collection ( 'test' ) . insertOne ( { a : 1 } ) ,
111- client . db ( 'test' ) . collection ( 'test' ) . insertOne ( { a : 1 } ) ,
112- client . db ( 'test' ) . collection ( 'test' ) . insertOne ( { a : 1 } )
113- ] ) ;
114-
115- // wait until all pings are pending on the server
116- while ( allClientEvents . filter ( e => e . name === 'connectionCheckedOut' ) . length < 3 ) {
117- await sleep ( 1 ) ;
118- }
119-
120- const insertConnectionIds = allClientEvents
121- . filter ( e => e . name === 'connectionCheckedOut' )
122- . map ( ( { address, connectionId } ) => `${ address } + ${ connectionId } ` ) ;
123-
124- await client . close ( ) ;
125-
126- const insertCheckInAndCloses = allClientEvents
127- . filter ( e => e . name === 'connectionCheckedIn' || e . name === 'connectionClosed' )
128- . filter ( ( { address, connectionId } ) =>
129- insertConnectionIds . includes ( `${ address } + ${ connectionId } ` )
130- ) ;
131-
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 )
134+ const insertCheckInAndCloses = allClientEvents
135+ . filter ( e => e . name === 'connectionCheckedIn' || e . name === 'connectionClosed' )
136+ . filter ( ( { address, connectionId } ) =>
137+ insertConnectionIds . includes ( `${ address } + ${ connectionId } ` )
137138 ) ;
138139
139- await inserts ;
140- }
141- ) ;
142- } ) ;
143- }
144- ) ;
140+ expect ( insertCheckInAndCloses ) . to . have . lengthOf ( 6 ) ;
141+
142+ // check that each check-in is followed by a close (not proceeded by one)
143+ expect ( insertCheckInAndCloses . map ( e => e . name ) ) . to . deep . equal (
144+ Array . from ( { length : 3 } , ( ) => [ 'connectionCheckedIn' , 'connectionClosed' ] ) . flat ( 1 )
145+ ) ;
146+
147+ await inserts ;
148+ }
149+ ) ;
150+ } ) ;
151+ } ) ;
145152 } ) ;
146153} ) ;
0 commit comments