Skip to content

Commit

Permalink
fix: awaitable isMaster timeout must respect connectTimeoutMS (#2650)
Browse files Browse the repository at this point in the history
  • Loading branch information
emadum committed Dec 1, 2020
1 parent 98ae02c commit 8d44cc2
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 5 deletions.
5 changes: 4 additions & 1 deletion src/sdam/monitor.ts
Expand Up @@ -217,7 +217,10 @@ function checkServer(monitor: Monitor, callback: Callback<Document>) {
: { ismaster: true };

const options = isAwaitable
? { socketTimeout: connectTimeoutMS + maxAwaitTimeMS, exhaustAllowed: true }
? {
socketTimeout: connectTimeoutMS ? connectTimeoutMS + maxAwaitTimeMS : 0,
exhaustAllowed: true
}
: { socketTimeout: connectTimeoutMS };

if (isAwaitable && monitor[kRTTPinger] == null) {
Expand Down
61 changes: 57 additions & 4 deletions test/functional/mongo_client_options.test.js
@@ -1,8 +1,9 @@
'use strict';
const test = require('./shared').assert,
setupDatabase = require('./shared').setupDatabase,
expect = require('chai').expect;
const sinon = require('sinon');
const { setupDatabase } = require('./shared');
const { expect } = require('chai');
const { MongoClient } = require('../../src');
const { Connection } = require('../../src/cmap/connection');

describe('MongoClient Options', function () {
before(function () {
Expand All @@ -22,11 +23,63 @@ describe('MongoClient Options', function () {
validateOptions: true
},
function (err, client) {
test.ok(err.message.indexOf('option notlegal is not supported') !== -1);
expect(err)
.property('message')
.to.match(/option notlegal is not supported/);
expect(client).to.not.exist;
done();
}
);
}
});

it('must respect an infinite connectTimeoutMS for the streaming protocol', {
metadata: { requires: { topology: 'replicaset', mongodb: '>= 4.4' } },
test: function (done) {
const client = this.configuration.newClient({
connectTimeoutMS: 0,
heartbeatFrequencyMS: 500
});
client.connect(err => {
expect(err).to.not.exist;
const stub = sinon.stub(Connection.prototype, 'command').callsFake(function () {
const args = Array.prototype.slice.call(arguments);
const ns = args[0];
const command = args[1];
const options = args[2];
if (ns === 'admin.$cmd' && command.ismaster && options.exhaustAllowed) {
stub.restore();
expect(options).property('socketTimeout').to.equal(0);
client.close(done);
}
stub.wrappedMethod.apply(this, args);
});
});
}
});

it('must respect a finite connectTimeoutMS for the streaming protocol', {
metadata: { requires: { topology: 'replicaset', mongodb: '>= 4.4' } },
test: function (done) {
const client = this.configuration.newClient({
connectTimeoutMS: 10,
heartbeatFrequencyMS: 500
});
client.connect(err => {
expect(err).to.not.exist;
const stub = sinon.stub(Connection.prototype, 'command').callsFake(function () {
const args = Array.prototype.slice.call(arguments);
const ns = args[0];
const command = args[1];
const options = args[2];
if (ns === 'admin.$cmd' && command.ismaster && options.exhaustAllowed) {
stub.restore();
expect(options).property('socketTimeout').to.equal(510);
client.close(done);
}
stub.wrappedMethod.apply(this, args);
});
});
}
});
});

0 comments on commit 8d44cc2

Please sign in to comment.