Skip to content

Commit 4c0e2fb

Browse files
committed
feat(NODE-6245): pass through as any
1 parent 6ec4660 commit 4c0e2fb

File tree

3 files changed

+51
-17
lines changed

3 files changed

+51
-17
lines changed

src/cmap/connect.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,10 @@ function parseConnectOptions(options: ConnectionOptions): SocketConnectOpts {
307307
(result as Document)[name] = options[name];
308308
}
309309
}
310+
if (result.keepAliveInitialDelay == null) {
311+
result.keepAliveInitialDelay = 120000;
312+
}
313+
result.keepAlive = true;
310314

311315
if (typeof hostAddress.socketPath === 'string') {
312316
result.path = hostAddress.socketPath;
@@ -377,7 +381,6 @@ export async function makeSocket(options: MakeConnectionOptions): Promise<Stream
377381
socket = net.createConnection(parseConnectOptions(options));
378382
}
379383

380-
socket.setKeepAlive(true, options.keepAliveInitialDelay ?? 120000);
381384
socket.setTimeout(connectTimeoutMS);
382385
socket.setNoDelay(noDelay);
383386

src/connection_string.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -872,9 +872,6 @@ export const OPTIONS = {
872872
return wc;
873873
}
874874
},
875-
keepAliveInitialDelay: {
876-
type: 'uint'
877-
},
878875
loadBalanced: {
879876
default: false,
880877
type: 'boolean'
@@ -1276,6 +1273,7 @@ export const OPTIONS = {
12761273
requestCert: { type: 'any' },
12771274
rejectUnauthorized: { type: 'any' },
12781275
checkServerIdentity: { type: 'any' },
1276+
keepAliveInitialDelay: { type: 'any' },
12791277
ALPNProtocols: { type: 'any' },
12801278
SNICallback: { type: 'any' },
12811279
session: { type: 'any' },

test/integration/node-specific/mongo_client.test.ts

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { expect } from 'chai';
22
import { once } from 'events';
33
import * as net from 'net';
4-
import { Socket } from 'net';
54
import * as sinon from 'sinon';
65

76
import {
@@ -144,8 +143,9 @@ describe('class MongoClient', function () {
144143
let spy;
145144

146145
beforeEach(async function () {
147-
spy = sinon.spy(Socket.prototype, 'setKeepAlive');
148-
client = this.configuration.newClient(options);
146+
spy = sinon.spy(net, 'createConnection');
147+
const uri = this.configuration.url();
148+
client = new MongoClient(uri, options);
149149
await client.connect();
150150
});
151151

@@ -155,7 +155,12 @@ describe('class MongoClient', function () {
155155
});
156156

157157
it('passes through the option', function () {
158-
expect(spy).to.have.been.calledWith(true, 0);
158+
expect(spy).to.have.been.calledWith(
159+
sinon.match({
160+
keepAlive: true,
161+
keepAliveInitialDelay: 0
162+
})
163+
);
159164
});
160165
});
161166

@@ -165,8 +170,9 @@ describe('class MongoClient', function () {
165170
let spy;
166171

167172
beforeEach(async function () {
168-
spy = sinon.spy(Socket.prototype, 'setKeepAlive');
169-
client = this.configuration.newClient(options);
173+
spy = sinon.spy(net, 'createConnection');
174+
const uri = this.configuration.url();
175+
client = new MongoClient(uri, options);
170176
await client.connect();
171177
});
172178

@@ -176,17 +182,39 @@ describe('class MongoClient', function () {
176182
});
177183

178184
it('passes through the option', function () {
179-
expect(spy).to.have.been.calledWith(true, 100);
185+
expect(spy).to.have.been.calledWith(
186+
sinon.match({
187+
keepAlive: true,
188+
keepAliveInitialDelay: 100
189+
})
190+
);
180191
});
181192
});
182193

183194
context('when the value is negative', function () {
184195
const options = { keepAliveInitialDelay: -100 };
196+
let client;
197+
let spy;
198+
199+
beforeEach(async function () {
200+
spy = sinon.spy(net, 'createConnection');
201+
const uri = this.configuration.url();
202+
client = new MongoClient(uri, options);
203+
await client.connect();
204+
});
185205

186-
it('raises an error', function () {
187-
expect(() => {
188-
this.configuration.newClient(options);
189-
}).to.throw(/keepAliveInitialDelay can only be a positive int value/);
206+
afterEach(async function () {
207+
await client?.close();
208+
spy.restore();
209+
});
210+
211+
it('sets the option to 0', function () {
212+
expect(spy).to.have.been.calledWith(
213+
sinon.match({
214+
keepAlive: true,
215+
keepAliveInitialDelay: 0
216+
})
217+
);
190218
});
191219
});
192220
});
@@ -196,7 +224,7 @@ describe('class MongoClient', function () {
196224
let spy;
197225

198226
beforeEach(async function () {
199-
spy = sinon.spy(Socket.prototype, 'setKeepAlive');
227+
spy = sinon.spy(net, 'createConnection');
200228
client = this.configuration.newClient();
201229
await client.connect();
202230
});
@@ -207,7 +235,12 @@ describe('class MongoClient', function () {
207235
});
208236

209237
it('sets keepalive to 120000', function () {
210-
expect(spy).to.have.been.calledWith(true, 120000);
238+
expect(spy).to.have.been.calledWith(
239+
sinon.match({
240+
keepAlive: true,
241+
keepAliveInitialDelay: 120000
242+
})
243+
);
211244
});
212245
});
213246
});

0 commit comments

Comments
 (0)