diff --git a/src/cmap/connect.ts b/src/cmap/connect.ts index 394b70689c..9b6e311bc8 100644 --- a/src/cmap/connect.ts +++ b/src/cmap/connect.ts @@ -289,6 +289,7 @@ export const LEGAL_TLS_SOCKET_OPTIONS = [ export const LEGAL_TCP_SOCKET_OPTIONS = [ 'autoSelectFamily', 'autoSelectFamilyAttemptTimeout', + 'keepAliveInitialDelay', 'family', 'hints', 'localAddress', @@ -376,7 +377,7 @@ export async function makeSocket(options: MakeConnectionOptions): Promise; diff --git a/test/integration/node-specific/mongo_client.test.ts b/test/integration/node-specific/mongo_client.test.ts index 4a97035d55..2f87a7a846 100644 --- a/test/integration/node-specific/mongo_client.test.ts +++ b/test/integration/node-specific/mongo_client.test.ts @@ -1,6 +1,7 @@ import { expect } from 'chai'; import { once } from 'events'; import * as net from 'net'; +import { Socket } from 'net'; import * as sinon from 'sinon'; import { @@ -135,6 +136,82 @@ describe('class MongoClient', function () { expect(error).to.be.instanceOf(MongoServerSelectionError); }); + describe('#connect', function () { + context('when keepAliveInitialDelay is provided', function () { + context('when the value is 0', function () { + const options = { keepAliveInitialDelay: 0 }; + let client; + let spy; + + beforeEach(async function () { + spy = sinon.spy(Socket.prototype, 'setKeepAlive'); + client = this.configuration.newClient(options); + await client.connect(); + }); + + afterEach(async function () { + await client?.close(); + spy.restore(); + }); + + it('passes through the option', function () { + expect(spy).to.have.been.calledWith(true, 0); + }); + }); + + context('when the value is positive', function () { + const options = { keepAliveInitialDelay: 100 }; + let client; + let spy; + + beforeEach(async function () { + spy = sinon.spy(Socket.prototype, 'setKeepAlive'); + client = this.configuration.newClient(options); + await client.connect(); + }); + + afterEach(async function () { + await client?.close(); + spy.restore(); + }); + + it('passes through the option', function () { + expect(spy).to.have.been.calledWith(true, 100); + }); + }); + + context('when the value is negative', function () { + const options = { keepAliveInitialDelay: -100 }; + + it('raises an error', function () { + expect(() => { + this.configuration.newClient(options); + }).to.throw(/keepAliveInitialDelay can only be a positive int value/); + }); + }); + }); + + context('when keepAliveInitialDelay is not provided', function () { + let client; + let spy; + + beforeEach(async function () { + spy = sinon.spy(Socket.prototype, 'setKeepAlive'); + client = this.configuration.newClient(); + await client.connect(); + }); + + afterEach(async function () { + await client?.close(); + spy.restore(); + }); + + it('sets keepalive to 120000', function () { + expect(spy).to.have.been.calledWith(true, 120000); + }); + }); + }); + it('Should correctly pass through appname', { metadata: { requires: {