Skip to content

Commit 4865259

Browse files
srv test
lint
1 parent 3338fb9 commit 4865259

File tree

3 files changed

+175
-120
lines changed

3 files changed

+175
-120
lines changed

test/integration/node-specific/client_close.test.ts

Lines changed: 168 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { type TestConfiguration } from '../../tools/runner/config';
33
import { runScriptAndGetProcessInfo } from './resource_tracking_script_builder';
44

5-
describe.only('MongoClient.close() Integration', () => {
5+
describe.skip('MongoClient.close() Integration', () => {
66
// note: these tests are set-up in accordance of the resource ownership tree
77

88
let config: TestConfiguration;
@@ -76,33 +76,45 @@ describe.only('MongoClient.close() Integration', () => {
7676
describe('Node.js resource: Server Selection Timer', () => {
7777
describe('after a Topology is created through client.connect()', () => {
7878
it('server selection timers are cleaned up by client.close()', async () => {
79-
const run = async function ({ MongoClient, uri, expect, sinon, sleep, mongodb, getTimerCount }) {
80-
const serverSelectionTimeoutMS = 2222;
81-
const client = new MongoClient(uri, { minPoolSize: 1, serverSelectionTimeoutMS });
82-
const timers = require('timers');
83-
const timeoutStartedSpy = sinon.spy(timers, 'setTimeout');
84-
let serverSelectionTimeoutStarted = false;
85-
86-
// make server selection hang so check out timer isn't cleared and check that the timeout has started
87-
sinon.stub(Promise, 'race').callsFake(async ([serverPromise, timeout]) => {
88-
serverSelectionTimeoutStarted = timeoutStartedSpy.getCalls().filter(r => r.args.includes(serverSelectionTimeoutMS)).flat().length > 0;
89-
await timeout;
90-
});
79+
const run = async function ({
80+
MongoClient,
81+
uri,
82+
expect,
83+
sinon,
84+
sleep,
85+
mongodb,
86+
getTimerCount,
87+
timers
88+
}) {
89+
const serverSelectionTimeoutMS = 2222;
90+
const client = new MongoClient(uri, { minPoolSize: 1, serverSelectionTimeoutMS });
91+
const timeoutStartedSpy = sinon.spy(timers, 'setTimeout');
92+
let serverSelectionTimeoutStarted = false;
93+
94+
// make server selection hang so check out timer isn't cleared and check that the timeout has started
95+
sinon.stub(Promise, 'race').callsFake(async ([_serverPromise, timeout]) => {
96+
serverSelectionTimeoutStarted =
97+
timeoutStartedSpy
98+
.getCalls()
99+
.filter(r => r.args.includes(serverSelectionTimeoutMS))
100+
.flat().length > 0;
101+
await timeout;
102+
});
91103

92-
const insertPromise = client.db('db').collection('collection').insertOne({ x: 1 });
104+
const insertPromise = client.db('db').collection('collection').insertOne({ x: 1 });
93105

94-
// don't allow entire server selection timer to elapse to ensure close is called mid-timeout
95-
await sleep(serverSelectionTimeoutMS / 2);
96-
expect(serverSelectionTimeoutStarted).to.be.true;
106+
// don't allow entire server selection timer to elapse to ensure close is called mid-timeout
107+
await sleep(serverSelectionTimeoutMS / 2);
108+
expect(serverSelectionTimeoutStarted).to.be.true;
97109

98-
expect(getTimerCount()).to.not.equal(0);
99-
await client.close();
100-
expect(getTimerCount()).to.equal(0);
110+
expect(getTimerCount()).to.not.equal(0);
111+
await client.close();
112+
expect(getTimerCount()).to.equal(0);
101113

102-
const err = await insertPromise.catch(e => e);
103-
expect(err).to.be.instanceOf(mongodb.MongoServerSelectionError);
104-
};
105-
await runScriptAndGetProcessInfo('timer-server-selection', config, run);
114+
const err = await insertPromise.catch(e => e);
115+
expect(err).to.be.instanceOf(mongodb.MongoServerSelectionError);
116+
};
117+
await runScriptAndGetProcessInfo('timer-server-selection', config, run);
106118
});
107119
});
108120
});
@@ -119,57 +131,66 @@ describe.only('MongoClient.close() Integration', () => {
119131
describe('MonitorInterval', () => {
120132
describe('Node.js resource: Timer', () => {
121133
describe('after a new monitor is made', () => {
122-
it('monitor interval timer is cleaned up by client.close()', metadata, async function () {
123-
const run = async function ({ MongoClient, uri, expect, sleep, getTimerCount }) {
124-
const heartbeatFrequencyMS = 2000;
125-
const client = new MongoClient(uri, { heartbeatFrequencyMS });
126-
let heartbeatHappened = false;
127-
client.on('serverHeartbeatSucceeded', () => heartbeatHappened = true);
128-
await client.connect();
129-
await sleep(heartbeatFrequencyMS * 2.5);
130-
expect(heartbeatHappened).to.be.true;
134+
it(
135+
'monitor interval timer is cleaned up by client.close()',
136+
metadata,
137+
async function () {
138+
const run = async function ({ MongoClient, uri, expect, sleep, getTimerCount }) {
139+
const heartbeatFrequencyMS = 2000;
140+
const client = new MongoClient(uri, { heartbeatFrequencyMS });
141+
let heartbeatHappened = false;
142+
client.on('serverHeartbeatSucceeded', () => (heartbeatHappened = true));
143+
await client.connect();
144+
await sleep(heartbeatFrequencyMS * 2.5);
145+
expect(heartbeatHappened).to.be.true;
131146

132-
function getMonitorTimer(servers) {
133-
for (const server of servers) {
134-
return server[1]?.monitor.monitorId.timerId;
147+
function getMonitorTimer(servers) {
148+
for (const server of servers) {
149+
return server[1]?.monitor.monitorId.timerId;
150+
}
135151
}
136-
};
137-
const servers = client.topology.s.servers;
138-
expect(getMonitorTimer(servers)).to.exist;
139-
await client.close();
140-
expect(getMonitorTimer(servers)).to.not.exist;
152+
const servers = client.topology.s.servers;
153+
expect(getMonitorTimer(servers)).to.exist;
154+
await client.close();
155+
expect(getMonitorTimer(servers)).to.not.exist;
141156

142-
expect(getTimerCount()).to.equal(0);
143-
};
144-
await runScriptAndGetProcessInfo('timer-monitor-interval', config, run);
145-
});
157+
expect(getTimerCount()).to.equal(0);
158+
};
159+
await runScriptAndGetProcessInfo('timer-monitor-interval', config, run);
160+
}
161+
);
146162
});
147163

148164
describe('after a heartbeat fails', () => {
149-
it('the new monitor interval timer is cleaned up by client.close()', metadata, async () => {
150-
const run = async function ({ MongoClient, uri, expect, sleep, getTimerCount }) {
151-
const heartbeatFrequencyMS = 2000;
152-
const client = new MongoClient('mongodb://fakeUri', { heartbeatFrequencyMS });
153-
let heartbeatHappened = false;
154-
client.on('serverHeartbeatFailed', () => heartbeatHappened = true);
155-
client.connect();
156-
await sleep(heartbeatFrequencyMS * 2.5);
157-
expect(heartbeatHappened).to.be.true;
158-
159-
function getMonitorTimer(servers) {
160-
for (const server of servers) {
161-
return server[1]?.monitor.monitorId.timerId;
165+
it(
166+
'the new monitor interval timer is cleaned up by client.close()',
167+
metadata,
168+
async () => {
169+
/* eslint-disable-next-line @typescript-eslint/no-unused-vars */
170+
const run = async function ({ MongoClient, uri, expect, sleep, getTimerCount }) {
171+
const heartbeatFrequencyMS = 2000;
172+
const client = new MongoClient('mongodb://fakeUri', { heartbeatFrequencyMS });
173+
let heartbeatHappened = false;
174+
client.on('serverHeartbeatFailed', () => (heartbeatHappened = true));
175+
client.connect();
176+
await sleep(heartbeatFrequencyMS * 2.5);
177+
expect(heartbeatHappened).to.be.true;
178+
179+
function getMonitorTimer(servers) {
180+
for (const server of servers) {
181+
return server[1]?.monitor.monitorId.timerId;
182+
}
162183
}
163-
};
164-
const servers = client.topology.s.servers;
165-
expect(getMonitorTimer(servers)).to.exist;
166-
await client.close();
167-
expect(getMonitorTimer(servers)).to.not.exist;
184+
const servers = client.topology.s.servers;
185+
expect(getMonitorTimer(servers)).to.exist;
186+
await client.close();
187+
expect(getMonitorTimer(servers)).to.not.exist;
168188

169-
expect(getTimerCount()).to.equal(0);
170-
};
171-
await runScriptAndGetProcessInfo('timer-heartbeat-failed-monitor', config, run);
172-
});
189+
expect(getTimerCount()).to.equal(0);
190+
};
191+
await runScriptAndGetProcessInfo('timer-heartbeat-failed-monitor', config, run);
192+
}
193+
);
173194
});
174195
});
175196
});
@@ -211,36 +232,40 @@ describe.only('MongoClient.close() Integration', () => {
211232
describe('RTT Pinger', () => {
212233
describe('Node.js resource: Timer', () => {
213234
describe('after entering monitor streaming mode ', () => {
214-
it('the rtt pinger timer is cleaned up by client.close()', metadata, async function () {
215-
const run = async function ({ MongoClient, uri, expect, sleep, getTimerCount }) {
216-
const heartbeatFrequencyMS = 2000;
217-
const client = new MongoClient(uri, {
218-
serverMonitoringMode: 'stream',
219-
heartbeatFrequencyMS
220-
});
221-
await client.connect();
235+
it(
236+
'the rtt pinger timer is cleaned up by client.close()',
237+
metadata,
238+
async function () {
239+
const run = async function ({ MongoClient, uri, expect, sleep, getTimerCount }) {
240+
const heartbeatFrequencyMS = 2000;
241+
const client = new MongoClient(uri, {
242+
serverMonitoringMode: 'stream',
243+
heartbeatFrequencyMS
244+
});
245+
await client.connect();
222246

223-
let heartbeatHappened = false;
224-
client.on('serverHeartbeatSucceeded', () => heartbeatHappened = true);
225-
await sleep(heartbeatFrequencyMS * 2.5);
226-
expect(heartbeatHappened).to.be.true;
247+
let heartbeatHappened = false;
248+
client.on('serverHeartbeatSucceeded', () => (heartbeatHappened = true));
249+
await sleep(heartbeatFrequencyMS * 2.5);
250+
expect(heartbeatHappened).to.be.true;
227251

228-
function getRttTimer(servers) {
229-
for (const server of servers) {
230-
return server[1]?.monitor.rttPinger.monitorId;
252+
function getRttTimer(servers) {
253+
for (const server of servers) {
254+
return server[1]?.monitor.rttPinger.monitorId;
255+
}
231256
}
232-
};
233257

234-
const servers = client.topology.s.servers;
235-
expect(getRttTimer(servers)).to.exist;
258+
const servers = client.topology.s.servers;
259+
expect(getRttTimer(servers)).to.exist;
236260

237-
await client.close();
238-
expect(getRttTimer(servers)).to.not.exist;
261+
await client.close();
262+
expect(getRttTimer(servers)).to.not.exist;
239263

240-
expect(getTimerCount()).to.equal(0);
241-
};
242-
await runScriptAndGetProcessInfo('timer-rtt-monitor', config, run);
243-
});
264+
expect(getTimerCount()).to.equal(0);
265+
};
266+
await runScriptAndGetProcessInfo('timer-rtt-monitor', config, run);
267+
}
268+
);
244269
});
245270
});
246271

@@ -315,7 +340,6 @@ describe.only('MongoClient.close() Integration', () => {
315340

316341
const servers = client.topology?.s.servers;
317342

318-
319343
function getMinPoolSizeTimer(servers) {
320344
for (const server of servers) {
321345
return server[1].pool.minPoolSizeTimer;
@@ -336,7 +360,15 @@ describe.only('MongoClient.close() Integration', () => {
336360
describe('Node.js resource: checkOut Timer', () => {
337361
describe('after new connection pool is created', () => {
338362
it('the wait queue timer is cleaned up by client.close()', async function () {
339-
const run = async function ({ MongoClient, uri, expect, sinon, sleep, getTimerCount }) {
363+
const run = async function ({
364+
MongoClient,
365+
uri,
366+
expect,
367+
sinon,
368+
sleep,
369+
getTimerCount,
370+
timers
371+
}) {
340372
const waitQueueTimeoutMS = 1515;
341373

342374
// configure failPoint
@@ -345,38 +377,56 @@ describe.only('MongoClient.close() Integration', () => {
345377
const failPoint = {
346378
configureFailPoint: 'failCommand',
347379
mode: { times: 1 },
348-
data: { blockConnection: true, blockTimeMS: waitQueueTimeoutMS * 3, failCommands: ['insert'] }
349-
}
380+
data: {
381+
blockConnection: true,
382+
blockTimeMS: waitQueueTimeoutMS * 3,
383+
failCommands: ['insert']
384+
}
385+
};
350386
await utilClient.db('admin').command(failPoint);
351387

352-
const timers = require('timers');
353388
const timeoutStartedSpy = sinon.spy(timers, 'setTimeout');
354389

355-
const client = new MongoClient(uri, { minPoolSize: 1, maxPoolSize: 1, waitQueueTimeoutMS });
356-
const insertPromise = client.db('db').collection('collection').insertOne({ x: 1 }).catch(e => e);
357-
client.db('db').collection('collection').insertOne({ x: 1 }).catch(e => e);
390+
const client = new MongoClient(uri, {
391+
minPoolSize: 1,
392+
maxPoolSize: 1,
393+
waitQueueTimeoutMS
394+
});
395+
const insertPromise = client
396+
.db('db')
397+
.collection('collection')
398+
.insertOne({ x: 1 })
399+
.catch(e => e);
400+
client
401+
.db('db')
402+
.collection('collection')
403+
.insertOne({ x: 1 })
404+
.catch(e => e);
358405

359406
// don't allow entire checkout timer to elapse to ensure close is called mid-timeout
360407
await sleep(waitQueueTimeoutMS / 2);
361-
const checkoutTimeoutStarted = timeoutStartedSpy.getCalls().filter(r => r.args.includes(waitQueueTimeoutMS)).flat().length > 0;
408+
const checkoutTimeoutStarted =
409+
timeoutStartedSpy
410+
.getCalls()
411+
.filter(r => r.args.includes(waitQueueTimeoutMS))
412+
.flat().length > 0;
362413
expect(checkoutTimeoutStarted).to.be.true;
363414

364415
await client.close();
365416
expect(getTimerCount()).to.equal(0);
366417
// un-configure fail{oint
367-
await utilClient
368-
.db()
369-
.admin()
370-
.command({
371-
configureFailPoint: 'failCommand',
372-
mode: 'off'
373-
});
418+
await utilClient.db().admin().command({
419+
configureFailPoint: 'failCommand',
420+
mode: 'off'
421+
});
374422

375423
await utilClient.close();
376424

377425
const err = await insertPromise;
378426
expect(err).to.be.instanceOf(Error);
379-
expect(err.message).to.contain('Timed out while checking out a connection from connection pool');
427+
expect(err.message).to.contain(
428+
'Timed out while checking out a connection from connection pool'
429+
);
380430
};
381431
await runScriptAndGetProcessInfo('timer-check-out', config, run);
382432
});
@@ -422,19 +472,23 @@ describe.only('MongoClient.close() Integration', () => {
422472
// requires an srv environment that can transition to sharded
423473
const metadata: MongoDBMetadataUI = {
424474
requires: {
425-
predicate: () => process.env.ATLAS_SRV_REPL ? 'Skipped: this test requires an SRV environment' : true
475+
predicate: () =>
476+
process.env.ATLAS_SRV_REPL ? 'Skipped: this test requires an SRV environment' : true
426477
}
427478
};
428479

429480
describe('after SRVPoller is created', () => {
430-
it.only('timers are cleaned up by client.close()', metadata, async () => {
431-
const run = async function ({ MongoClient, uri, expect, sinon, getTimerCount }) {
481+
it('timers are cleaned up by client.close()', metadata, async () => {
482+
const run = async function ({ MongoClient, uri, expect, getTimerCount }) {
432483
const client = new MongoClient(uri);
433484
await client.connect();
434485
const description = client.topology.s.description;
435486
// simulate transition to sharded
436-
client.topology.emit('topologyDescriptionChanged', description, { ... description, type: 'Sharded'});
437-
expect(client.topology.s.srvPoller?._timeout).to.exist;
487+
client.topology.emit('topologyDescriptionChanged', description, {
488+
...description,
489+
type: 'Sharded'
490+
});
491+
expect(client.topology.s.srvPoller._timeout).to.exist;
438492
await client.close();
439493
expect(getTimerCount()).to.equal(0);
440494
};
@@ -581,4 +635,4 @@ describe.only('MongoClient.close() Integration', () => {
581635
it.skip('all active server-side cursors are closed by client.close()', async function () {});
582636
});
583637
});
584-
});
638+
});

0 commit comments

Comments
 (0)