Skip to content

Commit 5f1947b

Browse files
committed
test: heartbeat duration
1 parent 0ba5e40 commit 5f1947b

File tree

4 files changed

+60
-51
lines changed

4 files changed

+60
-51
lines changed

src/cmap/connect.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ export async function makeSocket(options: MakeConnectionOptions): Promise<Stream
373373
const connectEvent = useTLS ? 'secureConnect' : 'connect';
374374
socket
375375
.once(connectEvent, () => resolve(socket))
376-
.once('error', error => reject(error))
376+
.once('error', error => reject(connectionFailureError('error', error)))
377377
.once('timeout', () => reject(connectionFailureError('timeout')))
378378
.once('close', () => reject(connectionFailureError('close')));
379379

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ describe('Connection', function () {
3535
it('should execute a command against a server', {
3636
metadata: { requires: { apiVersion: false, topology: '!load-balanced' } },
3737
test: async function () {
38-
const connectOptions: Partial<ConnectionOptions> = {
38+
const connectOptions: ConnectionOptions = {
3939
connectionType: Connection,
4040
...this.configuration.options,
4141
metadata: makeClientMetadata({ driverInfo: {} })
4242
};
4343

4444
let conn;
4545
try {
46-
conn = await promisify(connect)(connectOptions as any as ConnectionOptions);
46+
conn = await connect(connectOptions);
4747
const hello = await conn?.command(ns('admin.$cmd'), { [LEGACY_HELLO_COMMAND]: 1 });
4848
expect(hello).to.have.property('ok', 1);
4949
} finally {
@@ -55,7 +55,7 @@ describe('Connection', function () {
5555
it('should emit command monitoring events', {
5656
metadata: { requires: { apiVersion: false, topology: '!load-balanced' } },
5757
test: async function () {
58-
const connectOptions: Partial<ConnectionOptions> = {
58+
const connectOptions: ConnectionOptions = {
5959
connectionType: Connection,
6060
...this.configuration.options,
6161
monitorCommands: true,
@@ -64,7 +64,7 @@ describe('Connection', function () {
6464

6565
let conn;
6666
try {
67-
conn = await promisify(connect)(connectOptions as any as ConnectionOptions);
67+
conn = await connect(connectOptions);
6868

6969
const events: any[] = [];
7070
conn.on('commandStarted', event => events.push(event));

test/unit/cmap/connect.test.ts

Lines changed: 24 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ describe('Connect Tests', function () {
5151

5252
afterEach(() => mock.cleanup());
5353

54-
it('should auth against a non-arbiter', function (done) {
54+
it('should auth against a non-arbiter', async function () {
5555
const whatHappened = {};
5656

5757
test.server.setMessageHandler(request => {
@@ -71,19 +71,13 @@ describe('Connect Tests', function () {
7171
}
7272
});
7373

74-
connect(test.connectOptions, err => {
75-
try {
76-
expect(whatHappened).to.have.property(LEGACY_HELLO_COMMAND, true);
77-
expect(whatHappened).to.have.property('saslStart', true);
78-
} catch (_err) {
79-
err = _err;
80-
}
74+
await connect(test.connectOptions);
8175

82-
done(err);
83-
});
76+
expect(whatHappened).to.have.property(LEGACY_HELLO_COMMAND, true);
77+
expect(whatHappened).to.have.property('saslStart', true);
8478
});
8579

86-
it('should not auth against an arbiter', function (done) {
80+
it('should not auth against an arbiter', async function () {
8781
const whatHappened = {};
8882
test.server.setMessageHandler(request => {
8983
const doc = request.document;
@@ -102,16 +96,10 @@ describe('Connect Tests', function () {
10296
}
10397
});
10498

105-
connect(test.connectOptions, err => {
106-
try {
107-
expect(whatHappened).to.have.property(LEGACY_HELLO_COMMAND, true);
108-
expect(whatHappened).to.not.have.property('saslStart');
109-
} catch (_err) {
110-
err = _err;
111-
}
99+
await connect(test.connectOptions);
112100

113-
done(err);
114-
});
101+
expect(whatHappened).to.have.property(LEGACY_HELLO_COMMAND, true);
102+
expect(whatHappened).to.not.have.property('saslStart');
115103
});
116104
});
117105

@@ -133,10 +121,7 @@ describe('Connect Tests', function () {
133121
socketTimeoutMS: 15000
134122
};
135123

136-
connection = await promisify<Connection>(callback =>
137-
//@ts-expect-error: Callbacks do not have mutual exclusion for error/result existence
138-
connect(connectOptions, callback)
139-
)();
124+
connection = await connect(connectOptions);
140125
});
141126

142127
afterEach(async () => {
@@ -166,19 +151,13 @@ describe('Connect Tests', function () {
166151
});
167152
});
168153

169-
const error = await promisify<Connection>(callback =>
170-
connect(
171-
{
172-
...connectOptions,
173-
// Ensure these timeouts do not fire first
174-
socketTimeoutMS: 5000,
175-
connectTimeoutMS: 5000,
176-
cancellationToken
177-
},
178-
//@ts-expect-error: Callbacks do not have mutual exclusion for error/result existence
179-
callback
180-
)
181-
)().catch(error => error);
154+
const error = await connect({
155+
...connectOptions,
156+
// Ensure these timeouts do not fire first
157+
socketTimeoutMS: 5000,
158+
connectTimeoutMS: 5000,
159+
cancellationToken
160+
}).catch(error => error);
182161

183162
expect(error, error.stack).to.match(/connection establishment was cancelled/);
184163
});
@@ -189,21 +168,20 @@ describe('Connect Tests', function () {
189168
// set no response handler for mock server, effectively black hole requests
190169
server.setMessageHandler(() => null);
191170

192-
const error = await promisify<Connection>(callback =>
193-
//@ts-expect-error: Callbacks do not have mutual exclusion for error/result existence
194-
connect({ ...connectOptions, connectTimeoutMS: 5 }, callback)
195-
)().catch(error => error);
171+
const error = await connect({ ...connectOptions, connectTimeoutMS: 5 }).catch(
172+
error => error
173+
);
196174

197175
expect(error).to.match(/timed out/);
198176
});
199177
});
200178
});
201179

202-
it('should emit `MongoNetworkError` for network errors', function (done) {
203-
connect({ hostAddress: new HostAddress('non-existent:27018') }, err => {
204-
expect(err).to.be.instanceOf(MongoNetworkError);
205-
done();
206-
});
180+
it('should emit `MongoNetworkError` for network errors', async function () {
181+
const error = await connect({
182+
hostAddress: new HostAddress('non-existent:27018')
183+
}).catch(e => e);
184+
expect(error).to.be.instanceOf(MongoNetworkError);
207185
});
208186

209187
context('prepareHandshakeDocument', () => {

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)