@@ -7,18 +7,36 @@ describe('MongoClient', () => {
7
7
let client : MongoClient ;
8
8
let topologyOpenEvents ;
9
9
10
+ /** Keep track number of call to client connect to close as many as connect (otherwise leak_checker hook will failed) */
11
+ let clientConnectCounter : number ;
12
+
13
+ /**
14
+ * Wrap the connect method of the client to keep track
15
+ * of number of times connect is called
16
+ */
17
+ function clientConnect ( ) {
18
+ if ( ! client ) {
19
+ return ;
20
+ }
21
+ clientConnectCounter ++ ;
22
+ return client . connect ( ) ;
23
+ }
24
+
10
25
beforeEach ( async function ( ) {
11
26
client = this . configuration . newClient ( ) ;
12
27
topologyOpenEvents = [ ] ;
28
+ clientConnectCounter = 0 ;
13
29
client . on ( 'open' , event => topologyOpenEvents . push ( event ) ) ;
14
30
} ) ;
15
31
16
32
afterEach ( async function ( ) {
17
- await client . close ( ) ;
33
+ /** Close as many times as connect calls in the runned test (tracked by clientConnectCounter) */
34
+ const clientClosePromises = [ ...new Array ( clientConnectCounter ) ] . map ( ( ) => client . close ( ) ) ;
35
+ await Promise . all ( clientClosePromises ) ;
18
36
} ) ;
19
37
20
38
it ( 'Concurrents client connect correctly locked (only one topology created)' , async function ( ) {
21
- await Promise . all ( [ client . connect ( ) , client . connect ( ) , client . connect ( ) ] ) ;
39
+ await Promise . all ( [ clientConnect ( ) , clientConnect ( ) , clientConnect ( ) ] ) ;
22
40
23
41
expect ( topologyOpenEvents ) . to . have . lengthOf ( 1 ) ;
24
42
expect ( client . topology ?. isConnected ( ) ) . to . be . true ;
@@ -30,7 +48,7 @@ describe('MongoClient', () => {
30
48
31
49
// first call rejected to simulate a connection failure
32
50
try {
33
- await client . connect ( ) ;
51
+ await clientConnect ( ) ;
34
52
} catch ( err ) {
35
53
expect ( err ) . to . exist ;
36
54
}
@@ -39,7 +57,7 @@ describe('MongoClient', () => {
39
57
40
58
// second call should connect
41
59
try {
42
- await client . connect ( ) ;
60
+ await clientConnect ( ) ;
43
61
} catch ( err ) {
44
62
expect . fail ( `client connect throwed unexpected error` ) ;
45
63
}
0 commit comments