Skip to content

Commit

Permalink
fix(NODE-3305): beforeHandshake flag is always true (#2854)
Browse files Browse the repository at this point in the history
  • Loading branch information
nbbeeken authored Jun 22, 2021
1 parent 51e9768 commit 079bd6c
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 3 deletions.
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 @@ -189,8 +189,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');
});
});
});

0 comments on commit 079bd6c

Please sign in to comment.