Skip to content

Commit 94a37d0

Browse files
committed
feat(NODE-7223): run checkout on connect regardless of credentials
1 parent 8a67346 commit 94a37d0

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

src/sdam/topology.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ export class Topology extends TypedEventEmitter<TopologyEvents> {
470470
);
471471

472472
const skipPingOnConnect = this.s.options.__skipPingOnConnect === true;
473-
if (!skipPingOnConnect && this.s.credentials) {
473+
if (!skipPingOnConnect) {
474474
const connection = await server.pool.checkOut({ timeoutContext: timeoutContext });
475475
server.pool.checkIn(connection);
476476
stateTransition(this, STATE_CONNECTED);

test/integration/node-specific/mongo_client.test.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
Db,
1313
getTopology,
1414
MongoClient,
15+
MongoNetworkError,
1516
MongoNotConnectedError,
1617
MongoServerSelectionError,
1718
ReadPreference,
@@ -333,6 +334,28 @@ describe('class MongoClient', function () {
333334
}
334335
});
335336
});
337+
338+
it('throws ENOTFOUND error when connecting to non-existent host with no auth and loadBalanced=true', async function () {
339+
const configuration = this.configuration;
340+
const client = configuration.newClient(
341+
'mongodb://iLoveJavaScript:27017/test?loadBalanced=true',
342+
{ serverSelectionTimeoutMS: 100 }
343+
);
344+
345+
const error = await client.connect().catch(error => error);
346+
expect(error).to.be.instanceOf(MongoNetworkError); // not server selection like other topologies
347+
expect(error.message).to.match(/ENOTFOUND/);
348+
});
349+
350+
it('throws an error when srv is not a real record', async function () {
351+
const client = this.configuration.newClient('mongodb+srv://iLoveJavaScript/test', {
352+
serverSelectionTimeoutMS: 100
353+
});
354+
355+
const error = await client.connect().catch(error => error);
356+
expect(error).to.be.instanceOf(Error);
357+
expect(error.message).to.match(/ENOTFOUND/);
358+
});
336359
});
337360

338361
it('Should correctly pass through appname', {
@@ -600,15 +623,17 @@ describe('class MongoClient', function () {
600623
);
601624

602625
it(
603-
'does not checkout connection when authentication is disabled',
626+
'checks out connection to confirm connectivity even when authentication is disabled',
604627
{ requires: { auth: 'disabled' } },
605628
async function () {
606629
const checkoutStartedEvents = [];
607630
client.on('connectionCheckOutStarted', event => {
608631
checkoutStartedEvents.push(event);
609632
});
633+
const checkoutStarted = once(client, 'connectionCheckOutStarted');
610634
await client.connect();
611-
expect(checkoutStartedEvents).to.be.empty;
635+
const checkout = await checkoutStarted;
636+
expect(checkout).to.exist;
612637
expect(client).to.have.property('topology').that.is.instanceOf(Topology);
613638
}
614639
);

0 commit comments

Comments
 (0)