Skip to content

Commit

Permalink
fixup: account for sinon fake timers
Browse files Browse the repository at this point in the history
  • Loading branch information
addaleax committed May 30, 2022
1 parent 6a7b446 commit a627675
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { HostAddress, isHello } from '../../../src/utils';
import * as mock from '../../tools/mongodb-mock/index';
import type { MockServer } from '../../tools/mongodb-mock/src/server';
import { processTick } from '../../tools/utils';
import { createTimerSandbox } from '../timer_sandbox';

/*
The SRV Prose Tests make use of the following REAL DNS records.
Expand Down Expand Up @@ -215,17 +216,20 @@ describe('Polling Srv Records for Mongos Discovery', () => {
let lookupStub: sinon.SinonStub;
let client: MongoClient;
let clock: sinon.SinonFakeTimers;
let timerSandbox: sinon.SinonSandbox;
const initialRecords = Object.freeze([
{ name: 'localhost.test.mock.test.build.10gen.cc', port: 2017 },
{ name: 'localhost.test.mock.test.build.10gen.cc', port: 2018 }
]);

beforeEach(() => {
timerSandbox = createTimerSandbox();
clock = sinon.useFakeTimers();
});

afterEach(() => {
if (clock) {
timerSandbox.restore();
clock.restore();
clock = undefined;
}
Expand Down
4 changes: 4 additions & 0 deletions test/unit/cmap/connection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { MongoNetworkTimeoutError } from '../../../src/error';
import { isHello, ns } from '../../../src/utils';
import * as mock from '../../tools/mongodb-mock/index';
import { getSymbolFrom } from '../../tools/utils';
import { createTimerSandbox } from '../timer_sandbox';

const connectionOptionsDefaults = {
id: 0,
Expand Down Expand Up @@ -139,6 +140,7 @@ describe('new Connection()', function () {
describe('onTimeout()', () => {
let connection: sinon.SinonSpiedInstance<Connection>;
let clock: sinon.SinonFakeTimers;
let timerSandbox: sinon.SinonFakeTimers;
let driverSocket: sinon.SinonSpiedInstance<FakeSocket>;
let messageStream: MessageStream;
let kDelayedTimeoutId: symbol;
Expand All @@ -164,6 +166,7 @@ describe('new Connection()', function () {
}

beforeEach(() => {
timerSandbox = createTimerSandbox();
clock = sinon.useFakeTimers();

NodeJSTimeoutClass = setTimeout(() => null, 1).constructor;
Expand All @@ -177,6 +180,7 @@ describe('new Connection()', function () {
});

afterEach(() => {
timerSandbox.restore();
clock.restore();
});

Expand Down
22 changes: 22 additions & 0 deletions test/unit/timer_sandbox.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';
const sinon = require('sinon');

/**
* sinon.useFakeTimers() only affects global methods, this function
* creates a sinon sandbox that ensures that require('timers')
* also uses the mocked variants.
*
* @returns {sinon.SinonSandbox}
*/
exports.createTimerSandbox = () => {
const timerSandbox = sinon.createSandbox();
const timers = require('timers');
for (const method in timers) {
if (method in global) {
timerSandbox.replace(timers, method, (...args) => {
return global[method](...args);
});
}
}
return timerSandbox;
};
5 changes: 4 additions & 1 deletion test/unit/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const { expect } = require('chai');
const sinon = require('sinon');
const { MongoRuntimeError } = require('../../src/error');
const { LEGACY_HELLO_COMMAND } = require('../../src/constants');
const { createTimerSandbox } = require('./timer_sandbox');

describe('driver utils', function () {
context('eachAsync()', function () {
Expand Down Expand Up @@ -44,9 +45,10 @@ describe('driver utils', function () {
});

describe('#makeInterruptibleAsyncInterval', function () {
let clock, executor, fnSpy;
let timerSandbox, clock, executor, fnSpy;

beforeEach(function () {
timerSandbox = createTimerSandbox();
clock = sinon.useFakeTimers();
fnSpy = sinon.spy(cb => {
cb();
Expand All @@ -58,6 +60,7 @@ describe('driver utils', function () {
executor.stop();
}
clock.restore();
timerSandbox.restore();
});

context('when the immediate option is provided', function () {
Expand Down

0 comments on commit a627675

Please sign in to comment.