From 617d9dec5180c5f7b67bd8c944c168d4cbd27e1c Mon Sep 17 00:00:00 2001 From: Eric Adum Date: Tue, 2 Feb 2021 09:48:44 -0500 Subject: [PATCH] fix: restore auto direct connection behavior (#2719) This was removed prematurely in the 3.6.3 release. NODE-2966 --- lib/core/uri_parser.js | 9 +++++++++ test/functional/bulk.test.js | 14 ++++++++------ test/functional/mongo_client_options.test.js | 18 ++++++++++++++++++ test/functional/sharding_connection.test.js | 7 ++++++- 4 files changed, 41 insertions(+), 7 deletions(-) diff --git a/lib/core/uri_parser.js b/lib/core/uri_parser.js index 6918508629..62584a8899 100644 --- a/lib/core/uri_parser.js +++ b/lib/core/uri_parser.js @@ -694,6 +694,15 @@ function parseConnectionString(uri, options, callback) { return callback(new MongoParseError('directConnection option requires exactly one host')); } + // NOTE: this behavior will go away in v4.0, we will always auto discover there + if ( + parsedOptions.directConnection == null && + hosts.length === 1 && + parsedOptions.replicaSet == null + ) { + parsedOptions.directConnection = true; + } + const result = { hosts: hosts, auth: auth.db || auth.username ? auth : null, diff --git a/test/functional/bulk.test.js b/test/functional/bulk.test.js index d42655c6b3..ced20b1f36 100644 --- a/test/functional/bulk.test.js +++ b/test/functional/bulk.test.js @@ -1687,9 +1687,10 @@ describe('Bulk', function() { ); } }) - .finally(() => { - return client.close(); - }); + .then( + () => client.close(), + () => client.close() + ); }); it('should respect toBSON conversion when checking for atomic operators', function() { @@ -1721,8 +1722,9 @@ describe('Bulk', function() { expect.fail(); // shouldn't throw any error } }) - .finally(() => { - return client.close(); - }); + .then( + () => client.close(), + () => client.close() + ); }); }); diff --git a/test/functional/mongo_client_options.test.js b/test/functional/mongo_client_options.test.js index 50cc791abd..d905c8c606 100644 --- a/test/functional/mongo_client_options.test.js +++ b/test/functional/mongo_client_options.test.js @@ -168,6 +168,24 @@ describe('MongoClient Options', function() { } }); + it('should directConnect when no replicaSet name is specified', { + metadata: { requires: { topology: 'replicaset' } }, + test: function(done) { + const urlNoReplicaSet = this.configuration + .url() + // strip the replicaSet parameter from the url if present + .replace(/([&?])replicaSet=.+?[&$]/, '$1') + // reduce down to a single host if multiple are provided + .replace(new RegExp('(^mongodb://[^,]+)[^/]+'), '$1'); + const client = this.configuration.newClient(urlNoReplicaSet); + client.connect(err => { + expect(err).to.not.exist; + expect(client.s.options.directConnection).to.be.true; + client.close(done); + }); + } + }); + /** * @ignore */ diff --git a/test/functional/sharding_connection.test.js b/test/functional/sharding_connection.test.js index 8d3b5ed5d8..c7adcd0cc9 100644 --- a/test/functional/sharding_connection.test.js +++ b/test/functional/sharding_connection.test.js @@ -13,7 +13,12 @@ describe('Sharding (Connection)', function() { it('Should use sharded topology', { metadata: { requires: { topology: 'sharded' } }, test: function() { - const client = this.configuration.newClient({}, { useUnifiedTopology: true }); + const client = this.configuration.newClient( + {}, + // note: auto-discovery will be the default behavior in 4.0, + // for now we must supply directConnection: false + { useUnifiedTopology: true, directConnection: false } + ); return withClient(client, (client, done) => { expect(client).to.exist; expect(client)