Skip to content

Commit

Permalink
feat(NODE-5754): allow auto select family options
Browse files Browse the repository at this point in the history
  • Loading branch information
durran committed Jul 30, 2024
1 parent 74916f2 commit b8c5eff
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .evergreen/run-typescript.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export TSC="./node_modules/typescript/bin/tsc"
export TS_VERSION=$(get_ts_version)

# On old versions of TS we need to put the node types back to 18.11.19
npm install --no-save --force typescript@"$TS_VERSION" "$(if [[ $TS_VERSION == '4.4' ]]; then echo "@types/node@18.11.19"; else echo ""; fi)"
npm install --no-save --force typescript@"$TS_VERSION" "$(if [[ $TS_VERSION == '4.4' ]]; then echo "@types/node@20.0.0"; else echo ""; fi)"

echo "Typescript $($TSC -v)"

Expand Down
6 changes: 6 additions & 0 deletions src/cmap/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,8 @@ export const LEGAL_TLS_SOCKET_OPTIONS = [

/** @public */
export const LEGAL_TCP_SOCKET_OPTIONS = [
'autoSelectFamily',
'autoSelectFamilyAttemptTimeout',
'family',
'hints',
'localAddress',
Expand All @@ -287,6 +289,10 @@ function parseConnectOptions(options: ConnectionOptions): SocketConnectOpts {
}
}

if (!('autoSelectFamily' in result)) {
result.autoSelectFamily = true;
}

if (typeof hostAddress.socketPath === 'string') {
result.path = hostAddress.socketPath;
return result as net.IpcNetConnectOpts;
Expand Down
6 changes: 6 additions & 0 deletions src/connection_string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,12 @@ export const OPTIONS = {
autoEncryption: {
type: 'record'
},
autoSelectFamily: {
type: 'boolean'
},
autoSelectFamilyAttemptTimeout: {
type: 'uint'
},
bsonRegExp: {
type: 'boolean'
},
Expand Down
53 changes: 53 additions & 0 deletions test/integration/node-specific/mongo_client.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { expect } from 'chai';
import { once } from 'events';
import * as net from 'net';
import * as sinon from 'sinon';

import {
Expand Down Expand Up @@ -721,4 +722,56 @@ describe('class MongoClient', function () {
});
});
});

context('when connecting', function () {
let netSpy;

beforeEach(function () {
netSpy = sinon.spy(net, 'createConnection');
});

afterEach(function () {
sinon.restore();
});

context('when auto select options are provided', function () {
beforeEach(function () {
client = this.configuration.newClient({
autoSelectFamily: false,
autoSelectFamilyAttemptTimeout: 100
});
});

it('sets the provided options', {
metadata: { requires: { topology: ['single'] } },
test: async function () {
await client.connect();
expect(netSpy).to.have.been.calledWith({
autoSelectFamily: false,
autoSelectFamilyAttemptTimeout: 100,
host: 'localhost',
port: 27017
});
}
});
});

context('when auto select options are not provided', function () {
beforeEach(function () {
client = this.configuration.newClient();
});

it('sets the default options', {
metadata: { requires: { topology: ['single'] } },
test: async function () {
await client.connect();
expect(netSpy).to.have.been.calledWith({
autoSelectFamily: true,
host: 'localhost',
port: 27017
});
}
});
});
});
});
5 changes: 4 additions & 1 deletion test/manual/mocharc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"require": "ts-node/register",
"require": [
"ts-node/register",
"test/tools/runner/chai_addons.ts"
],
"reporter": "test/tools/reporter/mongodb_reporter.js",
"failZero": true,
"color": true,
Expand Down
59 changes: 59 additions & 0 deletions test/manual/tls_support.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import * as process from 'node:process';
import * as tls from 'node:tls';

import { expect } from 'chai';
import { promises as fs } from 'fs';
import * as sinon from 'sinon';

import {
LEGACY_HELLO_COMMAND,
Expand Down Expand Up @@ -61,6 +63,63 @@ describe('TLS Support', function () {
});

context('when tls filepaths have length > 0', () => {
context('when auto family options are not set', function () {
let tlsSpy;

afterEach(function () {
sinon.restore();
});

beforeEach(function () {
client = new MongoClient(CONNECTION_STRING, tlsSettings);
tlsSpy = sinon.spy(tls, 'connect');
});

it('sets the default options', async function () {
await client.connect();
expect(tlsSpy).to.have.been.calledWith({
autoSelectFamily: true,
host: 'localhost',
port: 27017,
servername: 'localhost',
ca: sinon.match.defined,
cert: sinon.match.defined,
key: sinon.match.defined
});
});
});

context('when auto family options are set', function () {
let tlsSpy;

afterEach(function () {
sinon.restore();
});

beforeEach(function () {
client = new MongoClient(CONNECTION_STRING, {
...tlsSettings,
autoSelectFamily: false,
autoSelectFamilyAttemptTimeout: 100
});
tlsSpy = sinon.spy(tls, 'connect');
});

it('sets the provided options', async function () {
await client.connect();
expect(tlsSpy).to.have.been.calledWith({
autoSelectFamily: false,
autoSelectFamilyAttemptTimeout: 100,
host: 'localhost',
port: 27017,
servername: 'localhost',
ca: sinon.match.defined,
cert: sinon.match.defined,
key: sinon.match.defined
});
});
});

context('when connection will succeed', () => {
beforeEach(async () => {
client = new MongoClient(CONNECTION_STRING, tlsSettings);
Expand Down

0 comments on commit b8c5eff

Please sign in to comment.