Skip to content
7 changes: 4 additions & 3 deletions src/sdam/topology.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ import {
makeStateMachine,
noop,
now,
ns,
promiseWithResolvers,
shuffle
} from '../utils';
Expand Down Expand Up @@ -459,7 +458,7 @@ export class Topology extends TypedEventEmitter<TopologyEvents> {
waitQueueTimeoutMS: this.client.s.options.waitQueueTimeoutMS
});
const selectServerOptions = {
operationName: 'ping',
operationName: 'handshake',
...options,
timeoutContext
};
Expand All @@ -469,9 +468,11 @@ export class Topology extends TypedEventEmitter<TopologyEvents> {
readPreferenceServerSelector(readPreference),
selectServerOptions
);

const skipPingOnConnect = this.s.options.__skipPingOnConnect === true;
if (!skipPingOnConnect && this.s.credentials) {
await server.command(ns('admin.$cmd'), { ping: 1 }, { timeoutContext });
const connection = await server.pool.checkOut({ timeoutContext: timeoutContext });
server.pool.checkIn(connection);
stateTransition(this, STATE_CONNECTED);
this.emit(Topology.OPEN, this);
this.emit(Topology.CONNECT, this);
Expand Down
4 changes: 2 additions & 2 deletions test/integration/node-specific/abort_signal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ describe('AbortSignal support', () => {
mongodbLogComponentSeverities: { serverSelection: 'debug' },
mongodbLogPath: {
write: log => {
if (log.c === 'serverSelection' && log.operation === 'ping') {
if (log.c === 'serverSelection' && log.operation === 'handshake') {
controller.abort();
promise.resolve();
}
Expand Down Expand Up @@ -676,7 +676,7 @@ describe('AbortSignal support', () => {
mongodbLogComponentSeverities: { serverSelection: 'debug' },
mongodbLogPath: {
write: log => {
if (log.c === 'serverSelection' && log.operation === 'ping') {
if (log.c === 'serverSelection' && log.operation === 'handshake') {
controller.abort();
promise.resolve();
}
Expand Down
28 changes: 1 addition & 27 deletions test/integration/node-specific/auto_connect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
Topology,
TopologyType
} from '../../mongodb';
import { type FailPoint, sleep } from '../../tools/utils';
import { sleep } from '../../tools/utils';

describe('When executing an operation for the first time', () => {
let client: MongoClient;
Expand Down Expand Up @@ -842,32 +842,6 @@ describe('When executing an operation for the first time', () => {
});
});

describe(
'when the server requires auth and ping is delayed',
{ requires: { auth: 'enabled', mongodb: '>=4.4' } },
function () {
beforeEach(async function () {
// set failpoint to delay ping
// create new util client to avoid affecting the test client
const utilClient = this.configuration.newClient();
await utilClient.db('admin').command({
configureFailPoint: 'failCommand',
mode: { times: 1 },
data: { failCommands: ['ping'], blockConnection: true, blockTimeMS: 1000 }
} as FailPoint);
await utilClient.close();
});

it('timeoutMS from the client is not used for the internal `ping`', async function () {
const start = performance.now();
const returnedClient = await client.connect();
const end = performance.now();
expect(returnedClient).to.equal(client);
expect(end - start).to.be.within(1000, 1500); // timeoutMS is 1000, did not apply.
});
}
);

describe(
'when server selection takes longer than the timeout',
{ requires: { auth: 'enabled', mongodb: '>=4.4' } },
Expand Down
50 changes: 23 additions & 27 deletions test/integration/node-specific/mongo_client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
ServerDescription,
Topology
} from '../../mongodb';
import { clearFailPoint, configureFailPoint, runLater } from '../../tools/utils';
import { clearFailPoint, configureFailPoint } from '../../tools/utils';
import { setupDatabase } from '../shared';

describe('class MongoClient', function () {
Expand Down Expand Up @@ -588,30 +588,27 @@ describe('class MongoClient', function () {
});

it(
'creates topology and send ping when auth is enabled',
'creates topology and checks out connection when auth is enabled',
{ requires: { auth: 'enabled' } },
async function () {
const commandToBeStarted = once(client, 'commandStarted');
const checkoutStarted = once(client, 'connectionCheckOutStarted');
await client.connect();
const [pingOnConnect] = await commandToBeStarted;
expect(pingOnConnect).to.have.property('commandName', 'ping');
const checkout = await checkoutStarted;
expect(checkout).to.exist;
expect(client).to.have.property('topology').that.is.instanceOf(Topology);
}
);

it(
'does not send ping when authentication is disabled',
'does not checkout connection when authentication is disabled',
{ requires: { auth: 'disabled' } },
async function () {
const commandToBeStarted = once(client, 'commandStarted');
const checkoutStartedEvents = [];
client.on('connectionCheckOutStarted', event => {
checkoutStartedEvents.push(event);
});
await client.connect();
const delayedFind = runLater(async () => {
await client.db().collection('test').findOne();
}, 300);
const [findOneOperation] = await commandToBeStarted;
// Proves that the first command started event that is emitted is a find and not a ping
expect(findOneOperation).to.have.property('commandName', 'find');
await delayedFind;
expect(checkoutStartedEvents).to.be.empty;
expect(client).to.have.property('topology').that.is.instanceOf(Topology);
}
);
Expand All @@ -620,15 +617,15 @@ describe('class MongoClient', function () {
'permits operations to be run after connect is called',
{ requires: { auth: 'enabled' } },
async function () {
const pingCommandToBeStarted = once(client, 'commandStarted');
const checkoutStarted = once(client, 'connectionCheckOutStarted');
await client.connect();
const [pingOnConnect] = await pingCommandToBeStarted;
const checkout = await checkoutStarted;
expect(checkout).to.exist;

const findCommandToBeStarted = once(client, 'commandStarted');
await client.db('test').collection('test').findOne();
const [findCommandStarted] = await findCommandToBeStarted;

expect(pingOnConnect).to.have.property('commandName', 'ping');
expect(findCommandStarted).to.have.property('commandName', 'find');
expect(client).to.have.property('topology').that.is.instanceOf(Topology);
}
Expand Down Expand Up @@ -1186,24 +1183,28 @@ describe('class MongoClient', function () {

const tests = [
// only skipInitialPing=true will have no events upon connect
{ description: 'should skip ping command when set to true', value: true, expectEvents: 0 },
{
description: 'should not skip ping command when set to false',
description: 'should skip connection checkout when set to true',
value: true,
expectEvents: 0
},
{
description: 'should not skip connection checkout when set to false',
value: false,
expectEvents: 1
},
{
description: 'should not skip ping command when unset',
description: 'should not skip connection checkout command when unset',
value: undefined,
expectEvents: 1
}
];
for (const { description, value, expectEvents } of tests) {
it(description, async function () {
const options = value === undefined ? {} : { __skipPingOnConnect: value };
const client = this.configuration.newClient({}, { ...options, monitorCommands: true });
const client = this.configuration.newClient({}, { ...options });
const events = [];
client.on('commandStarted', event => events.push(event));
client.on('connectionCheckOutStarted', event => events.push(event));

try {
await client.connect();
Expand All @@ -1212,11 +1213,6 @@ describe('class MongoClient', function () {
}

expect(events).to.have.lengthOf(expectEvents);
if (expectEvents > 1) {
for (const event of events) {
expect(event).to.have.property('commandName', 'ping');
}
}
});
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
"selector": {
"$$exists": true
},
"operation": "ping",
"operation": "handshake",
"topologyDescription": {
"$$exists": true
}
Expand All @@ -93,7 +93,7 @@
"selector": {
"$$exists": true
},
"operation": "ping",
"operation": "handshake",
"topologyDescription": {
"$$exists": true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
"selector": {
"$$exists": true
},
"operation": "ping",
"operation": "handshake",
"topologyDescription": {
"$$exists": true
}
Expand All @@ -114,7 +114,7 @@
"selector": {
"$$exists": true
},
"operation": "ping",
"operation": "handshake",
"topologyDescription": {
"$$exists": true
},
Expand All @@ -134,7 +134,7 @@
"selector": {
"$$exists": true
},
"operation": "ping",
"operation": "handshake",
"topologyDescription": {
"$$exists": true
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
"selector": {
"$$exists": true
},
"operation": "ping",
"operation": "handshake",
"topologyDescription": {
"$$exists": true
}
Expand All @@ -101,7 +101,7 @@
"selector": {
"$$exists": true
},
"operation": "ping",
"operation": "handshake",
"topologyDescription": {
"$$exists": true
},
Expand All @@ -121,7 +121,7 @@
"selector": {
"$$exists": true
},
"operation": "ping",
"operation": "handshake",
"topologyDescription": {
"$$exists": true
},
Expand Down Expand Up @@ -244,7 +244,7 @@
"selector": {
"$$exists": true
},
"operation": "ping",
"operation": "handshake",
"topologyDescription": {
"$$exists": true
}
Expand All @@ -258,7 +258,7 @@
"selector": {
"$$exists": true
},
"operation": "ping",
"operation": "handshake",
"topologyDescription": {
"$$exists": true
},
Expand All @@ -278,7 +278,7 @@
"selector": {
"$$exists": true
},
"operation": "ping",
"operation": "handshake",
"topologyDescription": {
"$$exists": true
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
"selector": {
"$$exists": true
},
"operation": "ping",
"operation": "handshake",
"topologyDescription": {
"$$exists": true
}
Expand All @@ -98,7 +98,7 @@
"selector": {
"$$exists": true
},
"operation": "ping",
"operation": "handshake",
"topologyDescription": {
"$$exists": true
},
Expand All @@ -118,7 +118,7 @@
"selector": {
"$$exists": true
},
"operation": "ping",
"operation": "handshake",
"topologyDescription": {
"$$exists": true
},
Expand Down Expand Up @@ -241,7 +241,7 @@
"selector": {
"$$exists": true
},
"operation": "ping",
"operation": "handshake",
"topologyDescription": {
"$$exists": true
}
Expand All @@ -255,7 +255,7 @@
"selector": {
"$$exists": true
},
"operation": "ping",
"operation": "handshake",
"topologyDescription": {
"$$exists": true
},
Expand All @@ -275,7 +275,7 @@
"selector": {
"$$exists": true
},
"operation": "ping",
"operation": "handshake",
"topologyDescription": {
"$$exists": true
},
Expand Down