From 07a3117f0676dbe4793fcd67a05b3d0eadf5ecbc Mon Sep 17 00:00:00 2001 From: emadum Date: Tue, 26 Jan 2021 21:16:24 -0500 Subject: [PATCH 1/4] fix: restore auto direct connection behavior --- lib/core/uri_parser.js | 9 +++++++++ test/functional/sharding_connection.test.js | 7 ++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/core/uri_parser.js b/lib/core/uri_parser.js index fca8032c172..57925d400eb 100644 --- a/lib/core/uri_parser.js +++ b/lib/core/uri_parser.js @@ -687,6 +687,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/sharding_connection.test.js b/test/functional/sharding_connection.test.js index 8d3b5ed5d8f..c7adcd0cc90 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) From b6e604b9b49d7404456da626f3aa76920352cb1f Mon Sep 17 00:00:00 2001 From: emadum Date: Thu, 28 Jan 2021 12:56:26 -0500 Subject: [PATCH 2/4] add test --- test/functional/mongo_client_options.test.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/functional/mongo_client_options.test.js b/test/functional/mongo_client_options.test.js index 50cc791abdb..b2ea9c9b519 100644 --- a/test/functional/mongo_client_options.test.js +++ b/test/functional/mongo_client_options.test.js @@ -168,6 +168,20 @@ describe('MongoClient Options', function() { } }); + it('should directConnect when no replicaSet name is specified', { + metadata: { requires: { topology: 'replicaset' } }, + test: function(done) { + // strip the replicaSet parameter from the url if present + const urlNoReplicaSet = this.configuration.url().replace(/([&?])replicaSet=.+?[&$]/, '$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 */ From 04442f605c2796028cb463b9d581f59766665efd Mon Sep 17 00:00:00 2001 From: emadum Date: Thu, 28 Jan 2021 18:26:01 -0500 Subject: [PATCH 3/4] fix test on evergreen --- test/functional/mongo_client_options.test.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/test/functional/mongo_client_options.test.js b/test/functional/mongo_client_options.test.js index b2ea9c9b519..d905c8c606f 100644 --- a/test/functional/mongo_client_options.test.js +++ b/test/functional/mongo_client_options.test.js @@ -171,8 +171,12 @@ describe('MongoClient Options', function() { it('should directConnect when no replicaSet name is specified', { metadata: { requires: { topology: 'replicaset' } }, test: function(done) { - // strip the replicaSet parameter from the url if present - const urlNoReplicaSet = this.configuration.url().replace(/([&?])replicaSet=.+?[&$]/, '$1'); + 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; From faeeb5a69b74ad5b7d74252d61c4f273e165650a Mon Sep 17 00:00:00 2001 From: emadum Date: Thu, 28 Jan 2021 18:26:17 -0500 Subject: [PATCH 4/4] remove finally for old node versions --- test/functional/bulk.test.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/test/functional/bulk.test.js b/test/functional/bulk.test.js index d42655c6b3a..ced20b1f36d 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() + ); }); });