@@ -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,63 @@ describe('Connection Pool', function () {
64
71
} ) ;
65
72
} ) ;
66
73
} ) ;
74
+
75
+ describe ( 'ConnectionCheckedInEvent' , { requires : { mongodb : '>=4.4' } } , function ( ) {
76
+ let client ;
77
+
78
+ beforeEach ( async function ( ) {
79
+ client = this . configuration . newClient ( ) ;
80
+ configureFailPoint ( this . configuration , {
81
+ configureFailPoint : 'failCommand' ,
82
+ mode : 'alwaysOn' ,
83
+ data : {
84
+ failCommands : [ 'insert' ] ,
85
+ blockConnection : true ,
86
+ blockTimeMS : 60_000
87
+ }
88
+ } ) ;
89
+ } ) ;
90
+
91
+ afterEach ( async function ( ) {
92
+ clearFailPoint ( this . configuration ) ;
93
+ await client . close ( ) ;
94
+ } ) ;
95
+
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' } } ,
100
+ async ( ) => {
101
+ const connectionCheckedOutEvents : ConnectionCheckedOutEvent [ ] = [ ] ;
102
+ client . on ( 'connectionCheckedOut' , event => connectionCheckedOutEvents . push ( event ) ) ;
103
+ const connectionCheckedInEvents : ConnectionCheckedInEvent [ ] = [ ] ;
104
+ client . on ( 'connectionCheckedIn' , event => connectionCheckedInEvents . push ( event ) ) ;
105
+
106
+ const pingPromises = Promise . allSettled ( [
107
+ client . db ( 'test' ) . collection ( 'test' ) . insertOne ( { a : 1 } ) ,
108
+ client . db ( 'test' ) . collection ( 'test' ) . insertOne ( { a : 1 } ) ,
109
+ client . db ( 'test' ) . collection ( 'test' ) . insertOne ( { a : 1 } )
110
+ ] ) ;
111
+
112
+ // wait until all pings are pending on the server
113
+ while ( connectionCheckedOutEvents . length < 3 ) await sleep ( 1 ) ;
114
+
115
+ const pingConnectionIds = connectionCheckedOutEvents . map (
116
+ ( { address, connectionId } ) => `${ address } + ${ connectionId } `
117
+ ) ;
118
+
119
+ await client . close ( ) ;
120
+
121
+ const pingCheckIns = connectionCheckedInEvents . filter ( ( { address, connectionId } ) =>
122
+ pingConnectionIds . includes ( `${ address } + ${ connectionId } ` )
123
+ ) ;
124
+
125
+ expect ( pingCheckIns ) . to . have . lengthOf ( 3 ) ;
126
+
127
+ await pingPromises ;
128
+ }
129
+ ) ;
130
+ } ) ;
131
+ } ) ;
67
132
} ) ;
68
133
} ) ;
0 commit comments