Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(NODE-5754): allow auto select family options #4185

Merged
merged 3 commits into from
Aug 1, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
@@ -269,6 +269,8 @@ export const LEGAL_TLS_SOCKET_OPTIONS = [

/** @public */
export const LEGAL_TCP_SOCKET_OPTIONS = [
'autoSelectFamily',
'autoSelectFamilyAttemptTimeout',
'family',
'hints',
'localAddress',
@@ -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;
6 changes: 6 additions & 0 deletions src/connection_string.ts
Original file line number Diff line number Diff line change
@@ -740,6 +740,12 @@ export const OPTIONS = {
autoEncryption: {
type: 'record'
},
autoSelectFamily: {
type: 'boolean'
},
autoSelectFamilyAttemptTimeout: {
type: 'uint'
},
bsonRegExp: {
type: 'boolean'
},
2 changes: 1 addition & 1 deletion src/mongo_client.ts
Original file line number Diff line number Diff line change
@@ -104,7 +104,7 @@ export type SupportedTLSSocketOptions = Pick<

/** @public */
export type SupportedSocketOptions = Pick<
TcpNetConnectOpts,
TcpNetConnectOpts & { autoSelectFamily?: boolean; autoSelectFamilyAttemptTimeout?: number },
(typeof LEGAL_TCP_SOCKET_OPTIONS)[number]
>;

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 {
@@ -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,
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,
@@ -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);