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(cmap): undo flipping of beforeHandshake flag for timeout errors #2813

Merged
merged 3 commits into from
May 21, 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 lib/cmap/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class Connection extends EventEmitter {
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 lib/core/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ 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
58 changes: 58 additions & 0 deletions test/unit/cmap/connection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,62 @@ describe('Connection', 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) {
request.reply(mock.DEFAULT_ISMASTER_36);
}
// respond to no other requests to trigger timeout event
});

const address = server.address();
const options = {
bson: new BSON(),
connectionType: Connection,
host: address.host,
port: address.port
};

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('$admin.cmd', { ping: 1 }, { socketTimeout: 50 }, err => {
const beforeHandshakeSymbol = Object.getOwnPropertySymbols(err)[0];
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 address = server.address();
const options = {
bson: new BSON(),
connectionType: Connection,
host: address.host,
port: address.port,
socketTimeout: 50
};

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

const beforeHandshakeSymbol = Object.getOwnPropertySymbols(err)[0];
expect(beforeHandshakeSymbol).to.be.a('symbol');
expect(err).to.have.property(beforeHandshakeSymbol, true);

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

const expect = require('chai').expect;
const MongoNetworkError = require('../../lib/core/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(Object.getOwnPropertySymbols(errorWithOptionTrue).length).to.equal(1);

const errorWithOptionFalse = new MongoNetworkError('', { beforeHandshake: false });
expect(Object.getOwnPropertySymbols(errorWithOptionFalse).length).to.equal(1);

const errorWithBadOption = new MongoNetworkError('', { beforeHandshake: 'not boolean' });
expect(Object.getOwnPropertySymbols(errorWithBadOption).length).to.equal(0);

const errorWithoutOption = new MongoNetworkError('');
expect(Object.getOwnPropertySymbols(errorWithoutOption).length).to.equal(0);
});
});
});