Skip to content

test(NODE-5724): heartbeat started is emitted before socket is connected #3960

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jan 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions test/unit/sdam/server_discovery_and_monitoring.prose.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { expect } from 'chai';
import { once } from 'events';
import { createServer, type Server } from 'net';

import { MongoClient, SERVER_HEARTBEAT_FAILED, SERVER_HEARTBEAT_STARTED } from '../../mongodb';

describe('Heartbeat tests', function () {
let client: MongoClient;
// Mock server
let server: Server;
// Shared array
const events: string[] = [];
const PORT = 9999;
const CONN_STRING = `mongodb://localhost:${PORT}`;

beforeEach(async function () {
// Create TCP server that responds to hellos by closing the connection
// and pushing "client connection created" to shared array
server = createServer(clientSocket => {
events.push('client connected');

clientSocket.once('data', () => {
events.push('client hello received');
clientSocket.destroy();
});
});
server.listen(PORT);

await once(server, 'listening');

// set up client to connect to mock server with the following configuration
// {
// serverSelectionTimeoutMS: 500,
// }
client = new MongoClient(CONN_STRING, {
serverSelectionTimeoutMS: 500
});

// Listen to `ServerHeartbeatStartedEvent` and `ServerHeartbeatSucceededEvent`, pushing the
// event name to the shared array when event is emitted
for (const e of [SERVER_HEARTBEAT_STARTED, SERVER_HEARTBEAT_FAILED]) {
client.on(e, () => {
events.push(e);
});
}
});

afterEach(async function () {
if (server.listening) server.close();
});

it('emits the first HeartbeatStartedEvent before the monitoring socket was created', async function () {
// Attempt to connect to mock server
const maybeError = await client.connect().catch(e => e);
// Catch error
expect(maybeError).to.be.instanceOf(Error);

expect(events).to.deep.equal([
SERVER_HEARTBEAT_STARTED,
'client connected',
'client hello received',
SERVER_HEARTBEAT_FAILED
]);
});
});