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-3305): beforeHandshake flag is always true #2854

Merged
merged 1 commit into from
Jun 22, 2021
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
2 changes: 1 addition & 1 deletion src/cmap/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
if (issue.isTimeout) {
op.cb(
new MongoNetworkTimeoutError(`connection ${this.id} to ${this.address} timed out`, {
beforeHandshake: !!this[kIsMaster]
beforeHandshake: this.ismaster == null
})
);
} else if (issue.isClose) {
Expand Down
4 changes: 2 additions & 2 deletions src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ export class MongoNetworkError extends MongoError {
super(message);
this.name = 'MongoNetworkError';

if (options && options.beforeHandshake === true) {
this[kBeforeHandshake] = true;
if (options && typeof options.beforeHandshake === 'boolean') {
this[kBeforeHandshake] = options.beforeHandshake;
}
}
}
Expand Down
48 changes: 48 additions & 0 deletions test/unit/cmap/connection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const { connect } = require('../../../src/cmap/connect');
const { Connection } = require('../../../src/cmap/connection');
const { expect } = require('chai');
const { ns } = require('../../../src/utils');
const { getSymbolFrom } = require('../../tools/utils');

describe('Connection - unit/cmap', function () {
let server;
Expand Down Expand Up @@ -58,4 +59,51 @@ describe('Connection - unit/cmap', function () {
});
});
});

it('should throw a network error with kBeforeHandshake set to false on timeout after hand shake', function (done) {
server.setMessageHandler(request => {
const doc = request.document;
if (doc.ismaster || doc.hello) {
request.reply(mock.DEFAULT_ISMASTER_36);
}
// respond to no other requests to trigger timeout event
});

const options = {
hostAddress: server.hostAddress()
};

connect(options, (err, conn) => {
expect(err).to.be.a('undefined');
expect(conn).to.be.instanceOf(Connection);
expect(conn).to.have.property('ismaster').that.is.a('object');

conn.command(ns('$admin.cmd'), { ping: 1 }, { socketTimeoutMS: 50 }, err => {
const beforeHandshakeSymbol = getSymbolFrom(err, 'beforeHandshake', false);
expect(beforeHandshakeSymbol).to.be.a('symbol');
expect(err).to.have.property(beforeHandshakeSymbol, false);

done();
});
});
});

it('should throw a network error with kBeforeHandshake set to true on timeout before hand shake', function (done) {
// respond to no requests to trigger timeout event
server.setMessageHandler(() => {});

const options = {
hostAddress: server.hostAddress(),
socketTimeoutMS: 50
};

connect(options, (err, conn) => {
expect(conn).to.be.a('undefined');

const beforeHandshakeSymbol = getSymbolFrom(err, 'beforeHandshake');
expect(err).to.have.property(beforeHandshakeSymbol, true);

done();
});
});
});
23 changes: 23 additions & 0 deletions test/unit/errors.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';

const expect = require('chai').expect;
const { getSymbolFrom } = require('../tools/utils');
const MongoNetworkError = require('../../src/error').MongoNetworkError;

describe('MongoErrors', function () {
describe('MongoNetworkError', function () {
it('should only define beforeHandshake symbol if boolean option passed in', function () {
const errorWithOptionTrue = new MongoNetworkError('', { beforeHandshake: true });
expect(getSymbolFrom(errorWithOptionTrue, 'beforeHandshake', false)).to.be.a('symbol');

const errorWithOptionFalse = new MongoNetworkError('', { beforeHandshake: false });
expect(getSymbolFrom(errorWithOptionFalse, 'beforeHandshake', false)).to.be.a('symbol');

const errorWithBadOption = new MongoNetworkError('', { beforeHandshake: 'not boolean' });
expect(getSymbolFrom(errorWithBadOption, 'beforeHandshake', false)).to.be.an('undefined');

const errorWithoutOption = new MongoNetworkError('');
expect(getSymbolFrom(errorWithoutOption, 'beforeHandshake', false)).to.be.an('undefined');
});
});
});