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

fix(NODE-4591): only set loadBalanced on handshake when explicitly set #3386

Merged
merged 2 commits into from
Aug 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
19 changes: 15 additions & 4 deletions src/cmap/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,18 @@ export interface HandshakeDocument extends Document {
client: ClientMetadata;
compression: string[];
saslSupportedMechs?: string;
loadBalanced: boolean;
loadBalanced?: boolean;
}

function prepareHandshakeDocument(authContext: AuthContext, callback: Callback<HandshakeDocument>) {
/**
* @internal
*
* This function is only exposed for testing purposes.
*/
export function prepareHandshakeDocument(
authContext: AuthContext,
callback: Callback<HandshakeDocument>
) {
const options = authContext.options;
const compressors = options.compressors ? options.compressors : [];
const { serverApi } = authContext.connection;
Expand All @@ -226,10 +234,13 @@ function prepareHandshakeDocument(authContext: AuthContext, callback: Callback<H
[serverApi?.version ? 'hello' : LEGACY_HELLO_COMMAND]: true,
helloOk: true,
client: options.metadata || makeClientMetadata(options),
compression: compressors,
loadBalanced: options.loadBalanced
compression: compressors
};

if (options.loadBalanced === true) {
handshakeDoc.loadBalanced = true;
}

const credentials = authContext.credentials;
if (credentials) {
if (credentials.mechanism === AuthMechanism.MONGODB_DEFAULT && credentials.username) {
Expand Down
52 changes: 51 additions & 1 deletion test/unit/cmap/connect.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ const { expect } = require('chai');
const EventEmitter = require('events');
const { setTimeout } = require('timers');

const { connect } = require('../../../src/cmap/connect');
const {
connect,
prepareHandshakeDocument: prepareHandshakeDocumentCb
} = require('../../../src/cmap/connect');
const { MongoCredentials } = require('../../../src/cmap/auth/mongo_credentials');
const { genClusterTime } = require('../../tools/common');
const { MongoNetworkError } = require('../../../src/error');
const { HostAddress, isHello } = require('../../../src/utils');
const { LEGACY_HELLO_COMMAND } = require('../../../src/constants');
const { promisify } = require('util');

describe('Connect Tests', function () {
const test = {};
Expand Down Expand Up @@ -112,4 +116,50 @@ describe('Connect Tests', function () {
done();
});
}).skipReason = 'TODO(NODE-2941): stop using 240.0.0.1 in tests';

context('prepareHandshakeDocument', () => {
const prepareHandshakeDocument = promisify(prepareHandshakeDocumentCb);

context('loadBalanced option', () => {
context('when loadBalanced is not set as an option', () => {
it('does not set loadBalanced on the handshake document', async () => {
const options = {};
const authContext = {
connection: {},
options
};
const handshakeDocument = await prepareHandshakeDocument(authContext);
expect(handshakeDocument).not.to.have.property('loadBalanced');
});
});

context('when loadBalanced is set to false', () => {
it('does not set loadBalanced on the handshake document', async () => {
const options = {
loadBalanced: false
};
const authContext = {
connection: {},
options
};
const handshakeDocument = await prepareHandshakeDocument(authContext);
expect(handshakeDocument).not.to.have.property('loadBalanced');
});
});

context('when loadBalanced is set to true', () => {
it('does set loadBalanced on the handshake document', async () => {
const options = {
loadBalanced: true
};
const authContext = {
connection: {},
options
};
const handshakeDocument = await prepareHandshakeDocument(authContext);
expect(handshakeDocument).to.have.property('loadBalanced', true);
});
});
});
});
});