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 b26c328 commit 1f17842
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 2 deletions.
Empty file modified .evergreen/run-typescript.sh
100644 → 100755
Empty file.
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
2 changes: 1 addition & 1 deletion src/mongo_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export type SupportedTLSSocketOptions = Pick<
export type SupportedSocketOptions = Pick<
TcpNetConnectOpts,
(typeof LEGAL_TCP_SOCKET_OPTIONS)[number]
>;
> & { autoSelectFamily?: boolean; autoSelectFamilyAttemptTimeout?: number };

/** @public */
export type SupportedNodeConnectionOptions = SupportedTLSConnectionOptions &
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 1f17842

Please sign in to comment.