diff --git a/src/sdam/monitor.ts b/src/sdam/monitor.ts index 835a631da72..d7cf62688e1 100644 --- a/src/sdam/monitor.ts +++ b/src/sdam/monitor.ts @@ -217,7 +217,10 @@ function checkServer(monitor: Monitor, callback: Callback) { : { ismaster: true }; const options = isAwaitable - ? { socketTimeout: connectTimeoutMS + maxAwaitTimeMS, exhaustAllowed: true } + ? { + socketTimeout: connectTimeoutMS ? connectTimeoutMS + maxAwaitTimeMS : 0, + exhaustAllowed: true + } : { socketTimeout: connectTimeoutMS }; if (isAwaitable && monitor[kRTTPinger] == null) { diff --git a/test/functional/mongo_client_options.test.js b/test/functional/mongo_client_options.test.js index ce7e2f6a60c..9e7ff1bc6f3 100644 --- a/test/functional/mongo_client_options.test.js +++ b/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 () { @@ -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); + }); + }); + } + }); });