Skip to content

Commit ca3fa4d

Browse files
committed
fix(NODE-5106): Fix tests to pass leak_checker
1 parent 9bc400d commit ca3fa4d

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

test/integration/mongo_client.test.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,36 @@ describe('MongoClient', () => {
77
let client: MongoClient;
88
let topologyOpenEvents;
99

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+
1025
beforeEach(async function () {
1126
client = this.configuration.newClient();
1227
topologyOpenEvents = [];
28+
clientConnectCounter = 0;
1329
client.on('open', event => topologyOpenEvents.push(event));
1430
});
1531

1632
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);
1836
});
1937

2038
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()]);
2240

2341
expect(topologyOpenEvents).to.have.lengthOf(1);
2442
expect(client.topology?.isConnected()).to.be.true;
@@ -30,7 +48,7 @@ describe('MongoClient', () => {
3048

3149
// first call rejected to simulate a connection failure
3250
try {
33-
await client.connect();
51+
await clientConnect();
3452
} catch (err) {
3553
expect(err).to.exist;
3654
}
@@ -39,7 +57,7 @@ describe('MongoClient', () => {
3957

4058
// second call should connect
4159
try {
42-
await client.connect();
60+
await clientConnect();
4361
} catch (err) {
4462
expect.fail(`client connect throwed unexpected error`);
4563
}

0 commit comments

Comments
 (0)