|
| 1 | +import * as net from 'node:net'; |
| 2 | + |
1 | 3 | import { expect } from 'chai';
|
2 | 4 | import { coerce } from 'semver';
|
3 | 5 | import * as sinon from 'sinon';
|
4 | 6 | import { setTimeout } from 'timers';
|
5 | 7 |
|
| 8 | +import { MongoClient } from '../../mongodb'; |
6 | 9 | import {
|
7 | 10 | isHello,
|
8 | 11 | LEGACY_HELLO_COMMAND,
|
@@ -579,4 +582,32 @@ describe('monitoring', function () {
|
579 | 582 | });
|
580 | 583 | });
|
581 | 584 | });
|
| 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 | + }); |
582 | 613 | });
|
0 commit comments