Skip to content
Prev Previous commit
Next Next commit
Add test to verify that rtt is never reported as zero for a successfu…
…ll heartbeat
  • Loading branch information
W-A-James committed May 14, 2024
commit f7b5d0c39bdb986a786a8d68f84c89c7ec64babd
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import { EventEmitter, once } from 'node:events';

import { expect } from 'chai';

import { type MongoClient, type ServerHeartbeatSucceededEvent } from '../../mongodb';
import { loadSpecTests } from '../../spec';
import { runUnifiedSuite } from '../../tools/unified-spec-runner/runner';

Expand All @@ -8,3 +13,44 @@ describe('SDAM Unified Tests (Node Driver)', function () {
);
runUnifiedSuite(clonedAndAlteredSpecTests);
});

describe('Monitoring rtt tests', function () {
let client: MongoClient;
let durations: number[];
const thresh = 40;
const ee = new EventEmitter();
const listener = (ev: ServerHeartbeatSucceededEvent) => {
durations.push(ev.duration);
if (durations.length >= thresh) {
client.off('serverHeartbeatSucceeded', listener);
ee.emit('done');
}
};

beforeEach(function () {
durations = [];
});

afterEach(async function () {
await client?.close();
});

for (const serverMonitoringMode of ['poll', 'stream']) {
context(`when serverMonitoringMode is set to '${serverMonitoringMode}'`, function () {
beforeEach(async function () {
client = this.configuration.newClient({
heartbeatFrequencyMS: 100,
serverMonitoringMode
});
client.on('serverHeartbeatSucceeded', listener);

await client.connect();
});

it('duration of a successful heartbeat is never reported as 0ms', async function () {
await once(ee, 'done');
expect(durations.every(x => x !== 0)).to.be.true;
});
});
}
});