Skip to content

Commit aa2542d

Browse files
committed
Add prose test
1 parent 5d8bf78 commit aa2542d

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { expect } from 'chai';
2+
import { createServer, type Server } from 'net';
3+
4+
import { MongoClient, SERVER_HEARTBEAT_FAILED, SERVER_HEARTBEAT_STARTED } from '../../mongodb';
5+
6+
describe('Heartbeat tests', function () {
7+
let client: MongoClient;
8+
// Mock server
9+
let server: Server;
10+
// Shared array
11+
const events: string[] = [];
12+
13+
beforeEach(async function () {
14+
// Create TCP server that responds to hellos by closing the connection
15+
// and pushing "client connection created" to shared array
16+
server = createServer(clientSocket => {
17+
console.log(`connection from ${clientSocket.remoteAddress}`);
18+
events.push('client connected');
19+
clientSocket.end();
20+
});
21+
server.listen(9999);
22+
23+
// set up client to connect to mock server with the following configuration
24+
// {
25+
// serverSelectionTimeoutMS: 500,
26+
// maxPoolSize: 1,
27+
// minPoolSize: 0
28+
// }
29+
client = new MongoClient('mongodb://localhost:9999', {
30+
serverSelectionTimeoutMS: 500,
31+
maxPoolSize: 1,
32+
minPoolSize: 0
33+
});
34+
35+
// Listen to `ServerHeartbeatStartedEvent` and `ServerHeartbeatSucceededEvent`, pushing the
36+
// event name to the shared array when event is emitted
37+
for (const e of [SERVER_HEARTBEAT_STARTED, SERVER_HEARTBEAT_FAILED]) {
38+
client.on(e, () => {
39+
events.push(e);
40+
});
41+
}
42+
43+
// Attempt to connect to mock server
44+
const maybeError = await client.connect().catch(e => e);
45+
// Catch error
46+
expect(maybeError).to.be.instanceOf(Error);
47+
});
48+
49+
afterEach(async function () {
50+
if (server.listening) server.close();
51+
});
52+
53+
it('emits the first HeartbeatStartedEvent after the monitoring socket was created', async function () {
54+
expect(events).to.have.lengthOf(3);
55+
expect(events[0]).to.equal('client connection created');
56+
expect(events[1]).to.equal(SERVER_HEARTBEAT_STARTED);
57+
expect(events[3]).to.equal(SERVER_HEARTBEAT_FAILED);
58+
});
59+
});

0 commit comments

Comments
 (0)