Skip to content

Commit

Permalink
fix: restore auto direct connection behavior (#2719)
Browse files Browse the repository at this point in the history
This was removed prematurely in the 3.6.3 release.

NODE-2966
  • Loading branch information
emadum authored Feb 2, 2021
1 parent 8082c89 commit 617d9de
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 7 deletions.
9 changes: 9 additions & 0 deletions lib/core/uri_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
14 changes: 8 additions & 6 deletions test/functional/bulk.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -1721,8 +1722,9 @@ describe('Bulk', function() {
expect.fail(); // shouldn't throw any error
}
})
.finally(() => {
return client.close();
});
.then(
() => client.close(),
() => client.close()
);
});
});
18 changes: 18 additions & 0 deletions test/functional/mongo_client_options.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
7 changes: 6 additions & 1 deletion test/functional/sharding_connection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 617d9de

Please sign in to comment.