|
1 | 1 | import { expect } from 'chai';
|
2 | 2 | import { once } from 'events';
|
| 3 | +import * as sinon from 'sinon'; |
| 4 | +import { setTimeout } from 'timers/promises'; |
3 | 5 |
|
| 6 | +import { connect } from '../../mongodb'; |
4 | 7 | import {
|
5 | 8 | CONNECTION_POOL_CLEARED,
|
6 | 9 | CONNECTION_POOL_READY,
|
7 | 10 | type MongoClient,
|
8 | 11 | SERVER_HEARTBEAT_FAILED,
|
| 12 | + SERVER_HEARTBEAT_STARTED, |
9 | 13 | SERVER_HEARTBEAT_SUCCEEDED
|
10 | 14 | } from '../../mongodb';
|
11 | 15 |
|
@@ -179,4 +183,40 @@ describe('Server Discovery and Monitoring Prose Tests', function () {
|
179 | 183 | }
|
180 | 184 | });
|
181 | 185 | });
|
| 186 | + |
| 187 | + describe('Heartbeat tests', function () { |
| 188 | + let client: MongoClient; |
| 189 | + let connectSpy; |
| 190 | + const events: { event: any; time: number }[] = []; |
| 191 | + |
| 192 | + // Spy on socket constructor |
| 193 | + |
| 194 | + beforeEach(function () { |
| 195 | + client = this.configuration.newClient({ |
| 196 | + heartbeatFrequencyMS: 10000, |
| 197 | + appName: 'HeartbeatTest', |
| 198 | + maxPoolSize: 1, |
| 199 | + minPoolSize: 0 |
| 200 | + }); |
| 201 | + |
| 202 | + client.on(SERVER_HEARTBEAT_STARTED, event => { |
| 203 | + events.push({ event, time: performance.now() }); |
| 204 | + }); |
| 205 | + |
| 206 | + // set up spy |
| 207 | + connectSpy = sinon.spy(connect); |
| 208 | + }); |
| 209 | + |
| 210 | + afterEach(async function () { |
| 211 | + sinon.restore(); |
| 212 | + }); |
| 213 | + |
| 214 | + it('emits the first HeartbeatStartedEvent after the monitoring socket was created', async function () { |
| 215 | + await client.connect(); |
| 216 | + await setTimeout(2000); |
| 217 | + await client.close(); |
| 218 | + |
| 219 | + expect(events).to.have.length.gte(2); |
| 220 | + }); |
| 221 | + }); |
182 | 222 | });
|
0 commit comments