Skip to content

Commit 0cea7fd

Browse files
server selection test fix
1 parent 92275d0 commit 0cea7fd

File tree

1 file changed

+12
-27
lines changed

1 file changed

+12
-27
lines changed

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

Lines changed: 12 additions & 27 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.skip('MongoClient.close() Integration', () => {
5+
describe('MongoClient.close() Integration', () => {
66
// note: these tests are set-up in accordance of the resource ownership tree
77

88
let config: TestConfiguration;
@@ -75,44 +75,29 @@ describe.skip('MongoClient.close() Integration', () => {
7575
describe('Topology', () => {
7676
describe('Node.js resource: Server Selection Timer', () => {
7777
describe('after a Topology is created through client.connect()', () => {
78-
it('server selection timers are cleaned up by client.close()', async () => {
79-
const run = async function ({
80-
MongoClient,
81-
uri,
82-
expect,
83-
sinon,
84-
sleep,
85-
mongodb,
86-
getTimerCount,
87-
timers
88-
}) {
78+
const metadata: MongoDBMetadataUI = { requires: { topology: 'replicaset' } };
79+
80+
it('server selection timers are cleaned up by client.close()', metadata, async () => {
81+
const run = async function ({ MongoClient, uri, expect, sleep, mongodb, getTimerCount }) {
8982
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;
83+
const client = new MongoClient(uri, {
84+
minPoolSize: 1,
85+
serverSelectionTimeoutMS,
86+
readPreference: new mongodb.ReadPreference('secondary', [
87+
{ something: 'that does not exist' }
88+
])
10289
});
103-
10490
const insertPromise = client.db('db').collection('collection').insertOne({ x: 1 });
10591

10692
// don't allow entire server selection timer to elapse to ensure close is called mid-timeout
10793
await sleep(serverSelectionTimeoutMS / 2);
108-
expect(serverSelectionTimeoutStarted).to.be.true;
10994

11095
expect(getTimerCount()).to.not.equal(0);
11196
await client.close();
11297
expect(getTimerCount()).to.equal(0);
11398

11499
const err = await insertPromise.catch(e => e);
115-
expect(err).to.be.instanceOf(mongodb.MongoServerSelectionError);
100+
expect(err).to.be.instanceOf(mongodb.MongoTopologyClosedError);
116101
};
117102
await runScriptAndGetProcessInfo('timer-server-selection', config, run);
118103
});

0 commit comments

Comments
 (0)