forked from mongodb/node-mongodb-native
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongo_client.test.ts
68 lines (55 loc) · 2 KB
/
mongo_client.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { expect } from 'chai';
import * as sinon from 'sinon';
import { MongoClient } from '../../src';
describe('MongoClient', () => {
let client: MongoClient;
let topologyOpenEvents;
/** Keep track number of call to client connect to close as many as connect (otherwise leak_checker hook will failed) */
let clientConnectCounter: number;
/**
* Wrap the connect method of the client to keep track
* of number of times connect is called
*/
function clientConnect() {
if (!client) {
return;
}
clientConnectCounter++;
return client.connect();
}
beforeEach(async function () {
client = this.configuration.newClient();
topologyOpenEvents = [];
clientConnectCounter = 0;
client.on('open', event => topologyOpenEvents.push(event));
});
afterEach(async function () {
/** Close as many times as connect calls in the runned test (tracked by clientConnectCounter) */
const clientClosePromises = [...new Array(clientConnectCounter)].map(() => client.close());
await Promise.all(clientClosePromises);
});
it('Concurrents client connect correctly locked (only one topology created)', async function () {
await Promise.all([clientConnect(), clientConnect(), clientConnect()]);
expect(topologyOpenEvents).to.have.lengthOf(1);
expect(client.topology?.isConnected()).to.be.true;
});
it('Failed client connect must properly release lock', async function () {
const internalConnectStub = sinon.stub(client, '_connect' as keyof MongoClient);
internalConnectStub.onFirstCall().rejects();
// first call rejected to simulate a connection failure
try {
await clientConnect();
} catch (err) {
expect(err).to.exist;
}
internalConnectStub.restore();
// second call should connect
try {
await clientConnect();
} catch (err) {
expect.fail(`client connect throwed unexpected error`);
}
expect(topologyOpenEvents).to.have.lengthOf(1);
expect(client.topology?.isConnected()).to.be.true;
});
});