@@ -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,59 @@ describe('Connection Pool', function () {
64
71
} ) ;
65
72
} ) ;
66
73
} ) ;
74
+
75
+ describe . only ( 'ConnectionCheckedInEvent' , 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 ( 'a connection pool emits checked in events for closed connections' , async ( ) => {
98
+ const connectionCheckedOutEvents : ConnectionCheckedOutEvent [ ] = [ ] ;
99
+ client . on ( 'connectionCheckedOut' , event => connectionCheckedOutEvents . push ( event ) ) ;
100
+ const connectionCheckedInEvents : ConnectionCheckedInEvent [ ] = [ ] ;
101
+ client . on ( 'connectionCheckedIn' , event => connectionCheckedInEvents . push ( event ) ) ;
102
+
103
+ const pingPromises = Promise . allSettled ( [
104
+ client . db ( 'test' ) . collection ( 'test' ) . insertOne ( { a : 1 } ) ,
105
+ client . db ( 'test' ) . collection ( 'test' ) . insertOne ( { a : 1 } ) ,
106
+ client . db ( 'test' ) . collection ( 'test' ) . insertOne ( { a : 1 } )
107
+ ] ) ;
108
+
109
+ // wait until all pings are pending on the server
110
+ while ( connectionCheckedOutEvents . length < 3 ) await sleep ( 1 ) ;
111
+
112
+ const pingConnectionIds = connectionCheckedOutEvents . map (
113
+ ( { connectionId } ) => connectionId
114
+ ) ;
115
+
116
+ await client . close ( ) ;
117
+
118
+ const pingCheckIns = connectionCheckedInEvents . filter ( checkIn =>
119
+ pingConnectionIds . includes ( checkIn . connectionId )
120
+ ) ;
121
+
122
+ expect ( pingCheckIns ) . to . have . lengthOf ( 3 ) ;
123
+
124
+ await pingPromises ;
125
+ } ) ;
126
+ } ) ;
127
+ } ) ;
67
128
} ) ;
68
129
} ) ;
0 commit comments