Skip to content

Commit 6321b2c

Browse files
committed
feat(NODE-5754): allow auto select family options
1 parent b26c328 commit 6321b2c

File tree

7 files changed

+129
-2
lines changed

7 files changed

+129
-2
lines changed

.evergreen/run-typescript.sh

100644100755
File mode changed.

src/cmap/connect.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,8 @@ export const LEGAL_TLS_SOCKET_OPTIONS = [
269269

270270
/** @public */
271271
export const LEGAL_TCP_SOCKET_OPTIONS = [
272+
'autoSelectFamily',
273+
'autoSelectFamilyAttemptTimeout',
272274
'family',
273275
'hints',
274276
'localAddress',
@@ -287,6 +289,10 @@ function parseConnectOptions(options: ConnectionOptions): SocketConnectOpts {
287289
}
288290
}
289291

292+
if (!('autoSelectFamily' in result)) {
293+
result.autoSelectFamily = true;
294+
}
295+
290296
if (typeof hostAddress.socketPath === 'string') {
291297
result.path = hostAddress.socketPath;
292298
return result as net.IpcNetConnectOpts;

src/connection_string.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,12 @@ export const OPTIONS = {
740740
autoEncryption: {
741741
type: 'record'
742742
},
743+
autoSelectFamily: {
744+
type: 'boolean'
745+
},
746+
autoSelectFamilyAttemptTimeout: {
747+
type: 'uint'
748+
},
743749
bsonRegExp: {
744750
type: 'boolean'
745751
},

src/mongo_client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ export type SupportedTLSSocketOptions = Pick<
104104

105105
/** @public */
106106
export type SupportedSocketOptions = Pick<
107-
TcpNetConnectOpts,
107+
TcpNetConnectOpts & { autoSelectFamily?: boolean; autoSelectFamilyAttemptTimeout?: number },
108108
(typeof LEGAL_TCP_SOCKET_OPTIONS)[number]
109109
>;
110110

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { expect } from 'chai';
22
import { once } from 'events';
3+
import * as net from 'net';
34
import * as sinon from 'sinon';
45

56
import {
@@ -721,4 +722,56 @@ describe('class MongoClient', function () {
721722
});
722723
});
723724
});
725+
726+
context('when connecting', function () {
727+
let netSpy;
728+
729+
beforeEach(function () {
730+
netSpy = sinon.spy(net, 'createConnection');
731+
});
732+
733+
afterEach(function () {
734+
sinon.restore();
735+
});
736+
737+
context('when auto select options are provided', function () {
738+
beforeEach(function () {
739+
client = this.configuration.newClient({
740+
autoSelectFamily: false,
741+
autoSelectFamilyAttemptTimeout: 100
742+
});
743+
});
744+
745+
it('sets the provided options', {
746+
metadata: { requires: { topology: ['single'] } },
747+
test: async function () {
748+
await client.connect();
749+
expect(netSpy).to.have.been.calledWith({
750+
autoSelectFamily: false,
751+
autoSelectFamilyAttemptTimeout: 100,
752+
host: 'localhost',
753+
port: 27017
754+
});
755+
}
756+
});
757+
});
758+
759+
context('when auto select options are not provided', function () {
760+
beforeEach(function () {
761+
client = this.configuration.newClient();
762+
});
763+
764+
it('sets the default options', {
765+
metadata: { requires: { topology: ['single'] } },
766+
test: async function () {
767+
await client.connect();
768+
expect(netSpy).to.have.been.calledWith({
769+
autoSelectFamily: true,
770+
host: 'localhost',
771+
port: 27017
772+
});
773+
}
774+
});
775+
});
776+
});
724777
});

test/manual/mocharc.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
2-
"require": "ts-node/register",
2+
"require": [
3+
"ts-node/register",
4+
"test/tools/runner/chai_addons.ts"
5+
],
36
"reporter": "test/tools/reporter/mongodb_reporter.js",
47
"failZero": true,
58
"color": true,

test/manual/tls_support.test.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import * as process from 'node:process';
2+
import * as tls from 'node:tls';
23

34
import { expect } from 'chai';
45
import { promises as fs } from 'fs';
6+
import * as sinon from 'sinon';
57

68
import {
79
LEGACY_HELLO_COMMAND,
@@ -61,6 +63,63 @@ describe('TLS Support', function () {
6163
});
6264

6365
context('when tls filepaths have length > 0', () => {
66+
context('when auto family options are not set', function () {
67+
let tlsSpy;
68+
69+
afterEach(function () {
70+
sinon.restore();
71+
});
72+
73+
beforeEach(function () {
74+
client = new MongoClient(CONNECTION_STRING, tlsSettings);
75+
tlsSpy = sinon.spy(tls, 'connect');
76+
});
77+
78+
it('sets the default options', async function () {
79+
await client.connect();
80+
expect(tlsSpy).to.have.been.calledWith({
81+
autoSelectFamily: true,
82+
host: 'localhost',
83+
port: 27017,
84+
servername: 'localhost',
85+
ca: sinon.match.defined,
86+
cert: sinon.match.defined,
87+
key: sinon.match.defined
88+
});
89+
});
90+
});
91+
92+
context('when auto family options are set', function () {
93+
let tlsSpy;
94+
95+
afterEach(function () {
96+
sinon.restore();
97+
});
98+
99+
beforeEach(function () {
100+
client = new MongoClient(CONNECTION_STRING, {
101+
...tlsSettings,
102+
autoSelectFamily: false,
103+
autoSelectFamilyAttemptTimeout: 100
104+
});
105+
tlsSpy = sinon.spy(tls, 'connect');
106+
});
107+
108+
it('sets the provided options', async function () {
109+
await client.connect();
110+
expect(tlsSpy).to.have.been.calledWith({
111+
autoSelectFamily: false,
112+
autoSelectFamilyAttemptTimeout: 100,
113+
host: 'localhost',
114+
port: 27017,
115+
servername: 'localhost',
116+
ca: sinon.match.defined,
117+
cert: sinon.match.defined,
118+
key: sinon.match.defined
119+
});
120+
});
121+
});
122+
64123
context('when connection will succeed', () => {
65124
beforeEach(async () => {
66125
client = new MongoClient(CONNECTION_STRING, tlsSettings);

0 commit comments

Comments
 (0)