@@ -2,7 +2,14 @@ import { once } from 'node:events';
2
2
3
3
import { expect } from 'chai' ;
4
4
5
- import { type ConnectionPoolCreatedEvent , type Db , type MongoClient } from '../../mongodb' ;
5
+ import {
6
+ type ConnectionCheckedInEvent ,
7
+ type ConnectionCheckedOutEvent ,
8
+ type ConnectionPoolCreatedEvent ,
9
+ type Db ,
10
+ type MongoClient
11
+ } from '../../mongodb' ;
12
+ import { clearFailPoint , configureFailPoint , sleep } from '../../tools/utils' ;
6
13
7
14
describe ( 'Connection Pool' , function ( ) {
8
15
let client : MongoClient ;
@@ -64,5 +71,70 @@ describe('Connection Pool', function () {
64
71
} ) ;
65
72
} ) ;
66
73
} ) ;
74
+
75
+ describe (
76
+ 'ConnectionCheckedInEvent' ,
77
+ { requires : { mongodb : '>=4.4' , topology : 'single' } } ,
78
+ function ( ) {
79
+ let client : MongoClient ;
80
+
81
+ beforeEach ( async function ( ) {
82
+ await configureFailPoint ( this . configuration , {
83
+ configureFailPoint : 'failCommand' ,
84
+ mode : 'alwaysOn' ,
85
+ data : {
86
+ failCommands : [ 'insert' ] ,
87
+ blockConnection : true ,
88
+ blockTimeMS : 500
89
+ }
90
+ } ) ;
91
+
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
+ await clearFailPoint ( this . configuration ) ;
99
+ await client . close ( ) ;
100
+ } ) ;
101
+
102
+ describe ( 'when a MongoClient is closed' , function ( ) {
103
+ it (
104
+ 'a connection pool emits checked in events for closed connections' ,
105
+ { requires : { mongodb : '>=4.4' , topology : 'single' } } ,
106
+ 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 ) ) ;
111
+
112
+ const inserts = Promise . allSettled ( [
113
+ client . db ( 'test' ) . collection ( 'test' ) . insertOne ( { a : 1 } ) ,
114
+ client . db ( 'test' ) . collection ( 'test' ) . insertOne ( { a : 1 } ) ,
115
+ client . db ( 'test' ) . collection ( 'test' ) . insertOne ( { a : 1 } )
116
+ ] ) ;
117
+
118
+ // wait until all pings are pending on the server
119
+ while ( connectionCheckedOutEvents . length < 3 ) await sleep ( 1 ) ;
120
+
121
+ const insertConnectionIds = connectionCheckedOutEvents . map (
122
+ ( { address, connectionId } ) => `${ address } + ${ connectionId } `
123
+ ) ;
124
+
125
+ await client . close ( ) ;
126
+
127
+ const insertCheckIns = connectionCheckedInEvents . filter ( ( { address, connectionId } ) =>
128
+ insertConnectionIds . includes ( `${ address } + ${ connectionId } ` )
129
+ ) ;
130
+
131
+ expect ( insertCheckIns ) . to . have . lengthOf ( 3 ) ;
132
+
133
+ await inserts ;
134
+ }
135
+ ) ;
136
+ } ) ;
137
+ }
138
+ ) ;
67
139
} ) ;
68
140
} ) ;
0 commit comments