Skip to content

Commit 3cf3ed2

Browse files
committed
test: address flakiness by waiting for necessary condition
1 parent efed7eb commit 3cf3ed2

File tree

2 files changed

+33
-25
lines changed

2 files changed

+33
-25
lines changed

test/integration/connection-monitoring-and-pooling/rtt_pinger.test.ts

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,32 @@ import { expect } from 'chai';
22
import * as semver from 'semver';
33
import * as sinon from 'sinon';
44

5-
import { type MongoClient } from '../../mongodb';
5+
import { type Connection, type MongoClient, type RTTPinger } from '../../mongodb';
66
import { sleep } from '../../tools/utils';
77

8+
/**
9+
* RTTPinger creation depends on getting a response to the monitor's initial hello
10+
* and that hello containing a topologyVersion.
11+
* Subsequently the rttPinger creates its connection asynchronously
12+
*
13+
* I just went with a sleepy loop, until we have what we need, One could also use SDAM events in a clever way perhaps?
14+
*/
15+
async function getRTTPingers(client: MongoClient) {
16+
// eslint-disable-next-line no-constant-condition
17+
while (true) {
18+
const rttPingers = Array.from(client.topology?.s.servers.values() ?? [], s => {
19+
if (s.monitor?.rttPinger?.connection != null) return s.monitor?.rttPinger;
20+
else null;
21+
}).filter(rtt => rtt != null);
22+
23+
if (rttPingers.length !== 0) {
24+
return rttPingers as (Omit<RTTPinger, 'connection'> & { connection: Connection })[];
25+
}
26+
27+
await sleep(5);
28+
}
29+
}
30+
831
describe('class RTTPinger', () => {
932
afterEach(() => sinon.restore());
1033

@@ -43,16 +66,9 @@ describe('class RTTPinger', () => {
4366

4467
it('measures rtt with a hello command', async function () {
4568
await serverApiClient.connect();
46-
await sleep(1001); // rttPinger creation
47-
48-
const rttPingers = Array.from(serverApiClient.topology?.s.servers.values() ?? [], s => {
49-
if (s.monitor?.rttPinger) return s.monitor?.rttPinger;
50-
else expect.fail('expected rttPinger to be defined');
51-
});
52-
53-
await sleep(11); // rttPinger connection creation
69+
const rttPingers = await getRTTPingers(serverApiClient);
5470

55-
const spies = rttPingers.map(rtt => rtt.connection && sinon.spy(rtt.connection, 'command'));
71+
const spies = rttPingers.map(rtt => sinon.spy(rtt.connection, 'command'));
5672

5773
await sleep(11); // allow for another ping after spies have been made
5874

@@ -75,19 +91,12 @@ describe('class RTTPinger', () => {
7591

7692
it('destroys the connection', async function () {
7793
await client.connect();
78-
await sleep(1001); // rttPinger creation
94+
const rttPingers = await getRTTPingers(client);
7995

80-
const rttPingers = Array.from(client.topology?.s.servers.values() ?? [], s => {
81-
if (s.monitor?.rttPinger) return s.monitor?.rttPinger;
82-
else expect.fail('expected rttPinger to be defined');
83-
});
84-
85-
await sleep(11); // rttPinger connection creation
86-
87-
for (const rtt of rttPingers)
88-
rtt.connection && sinon.stub(rtt.connection, 'command').yieldsRight(new Error('any'));
89-
90-
const spies = rttPingers.map(rtt => rtt.connection && sinon.spy(rtt.connection, 'destroy'));
96+
for (const rtt of rttPingers) {
97+
sinon.stub(rtt.connection, 'command').yieldsRight(new Error('any'));
98+
}
99+
const spies = rttPingers.map(rtt => sinon.spy(rtt.connection, 'destroy'));
91100

92101
await sleep(11); // allow for another ping after spies have been made
93102

test/unit/sdam/topology.test.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -355,9 +355,8 @@ describe('Topology (unit)', function () {
355355
afterEach(() => {
356356
// The srv event starts a monitor that we need to clean up
357357
for (const [, server] of topology.s.servers) {
358-
const kMonitor = getSymbolFrom(server, 'monitor');
359-
const kMonitorId = getSymbolFrom(server[kMonitor], 'monitorId');
360-
server[kMonitor][kMonitorId].stop();
358+
const kMonitorId = getSymbolFrom(server.monitor, 'monitorId');
359+
server.monitor[kMonitorId].stop();
361360
}
362361
});
363362

0 commit comments

Comments
 (0)