Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: restore auto direct connection behavior #2719

Merged
merged 4 commits into from Feb 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/core/uri_parser.js
Expand Up @@ -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,
Expand Down
14 changes: 8 additions & 6 deletions test/functional/bulk.test.js
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
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
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