Skip to content

Commit 8ddcfaf

Browse files
committed
test: heartbeat duration
1 parent 0ba5e40 commit 8ddcfaf

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

test/unit/sdam/monitor.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import * as net from 'node:net';
2+
13
import { expect } from 'chai';
24
import { coerce } from 'semver';
35
import * as sinon from 'sinon';
46
import { setTimeout } from 'timers';
57

8+
import { MongoClient } from '../../mongodb';
69
import {
710
isHello,
811
LEGACY_HELLO_COMMAND,
@@ -579,4 +582,32 @@ describe('monitoring', function () {
579582
});
580583
});
581584
});
585+
586+
describe('Heartbeat duration', function () {
587+
let client: MongoClient;
588+
let serverHeartbeatFailed;
589+
590+
beforeEach(async function () {
591+
// Artificially make creating a connection take 200ms
592+
sinon.stub(net, 'createConnection').callsFake(function () {
593+
const socket = new net.Socket();
594+
setTimeout(() => socket.emit('connect'), 80);
595+
socket.on('data', () => socket.destroy(new Error('I am not real!')));
596+
return socket;
597+
});
598+
599+
client = new MongoClient(`mongodb://localhost:1`, { serverSelectionTimeoutMS: 200 });
600+
client.on('serverHeartbeatFailed', ev => (serverHeartbeatFailed = ev));
601+
});
602+
603+
afterEach(function () {
604+
sinon.restore();
605+
});
606+
607+
it('includes only the time to perform handshake', async function () {
608+
const maybeError = await client.connect().catch(e => e);
609+
expect(maybeError).to.be.instanceOf(Error);
610+
expect(serverHeartbeatFailed).to.have.property('duration').that.is.lessThan(20); // way less than 80ms
611+
});
612+
});
582613
});

0 commit comments

Comments
 (0)