@@ -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,69 @@ describe('Connection Pool', function () {
64
71
} ) ;
65
72
} ) ;
66
73
} ) ;
74
+
75
+ describe (
76
+ 'ConnectionCheckedInEvent' ,
77
+ { requires : { mongodb : '>=4.4' , topology : '!load-balanced' } } ,
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 : 60_000
89
+ }
90
+ } ) ;
91
+
92
+ client = this . configuration . newClient ( ) ;
93
+ await client . connect ( ) ;
94
+ } ) ;
95
+
96
+ afterEach ( async function ( ) {
97
+ await clearFailPoint ( this . configuration ) ;
98
+ await client . close ( ) ;
99
+ } ) ;
100
+
101
+ describe ( 'when a MongoClient is closed' , function ( ) {
102
+ it (
103
+ 'a connection pool emits checked in events for closed connections' ,
104
+ { requires : { mongodb : '>=4.4' , topology : '!load-balanced' } } ,
105
+ async ( ) => {
106
+ const connectionCheckedOutEvents : ConnectionCheckedOutEvent [ ] = [ ] ;
107
+ client . on ( 'connectionCheckedOut' , event => connectionCheckedOutEvents . push ( event ) ) ;
108
+ const connectionCheckedInEvents : ConnectionCheckedInEvent [ ] = [ ] ;
109
+ client . on ( 'connectionCheckedIn' , event => connectionCheckedInEvents . push ( event ) ) ;
110
+
111
+ const pingPromises = Promise . allSettled ( [
112
+ client . db ( 'test' ) . collection ( 'test' ) . insertOne ( { a : 1 } ) ,
113
+ client . db ( 'test' ) . collection ( 'test' ) . insertOne ( { a : 1 } ) ,
114
+ client . db ( 'test' ) . collection ( 'test' ) . insertOne ( { a : 1 } )
115
+ ] ) ;
116
+
117
+ // wait until all pings are pending on the server
118
+ while ( connectionCheckedOutEvents . length < 3 ) await sleep ( 1 ) ;
119
+
120
+ const pingConnectionIds = connectionCheckedOutEvents . map (
121
+ ( { address, connectionId } ) => `${ address } + ${ connectionId } `
122
+ ) ;
123
+
124
+ await client . close ( ) ;
125
+
126
+ const pingCheckIns = connectionCheckedInEvents . filter ( ( { address, connectionId } ) =>
127
+ pingConnectionIds . includes ( `${ address } + ${ connectionId } ` )
128
+ ) ;
129
+
130
+ expect ( pingCheckIns ) . to . have . lengthOf ( 3 ) ;
131
+
132
+ await pingPromises ;
133
+ }
134
+ ) ;
135
+ } ) ;
136
+ }
137
+ ) ;
67
138
} ) ;
68
139
} ) ;
0 commit comments