Skip to content

Commit

Permalink
test: heartbeat duration
Browse files Browse the repository at this point in the history
  • Loading branch information
nbbeeken committed Jan 23, 2024
1 parent 0ba5e40 commit 8ddcfaf
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions test/unit/sdam/monitor.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import * as net from 'node:net';

import { expect } from 'chai';
import { coerce } from 'semver';
import * as sinon from 'sinon';
import { setTimeout } from 'timers';

import { MongoClient } from '../../mongodb';
import {
isHello,
LEGACY_HELLO_COMMAND,
Expand Down Expand Up @@ -579,4 +582,32 @@ describe('monitoring', function () {
});
});
});

describe('Heartbeat duration', function () {
let client: MongoClient;
let serverHeartbeatFailed;

beforeEach(async function () {
// Artificially make creating a connection take 200ms
sinon.stub(net, 'createConnection').callsFake(function () {
const socket = new net.Socket();
setTimeout(() => socket.emit('connect'), 80);
socket.on('data', () => socket.destroy(new Error('I am not real!')));
return socket;
});

client = new MongoClient(`mongodb://localhost:1`, { serverSelectionTimeoutMS: 200 });
client.on('serverHeartbeatFailed', ev => (serverHeartbeatFailed = ev));
});

afterEach(function () {
sinon.restore();
});

it('includes only the time to perform handshake', async function () {
const maybeError = await client.connect().catch(e => e);
expect(maybeError).to.be.instanceOf(Error);
expect(serverHeartbeatFailed).to.have.property('duration').that.is.lessThan(20); // way less than 80ms
});
});
});

0 comments on commit 8ddcfaf

Please sign in to comment.