Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified .evergreen/run-orchestration.sh
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion src/sdam/topology.ts
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ export class Topology extends TypedEventEmitter<TopologyEvents> {

closeCheckedOutConnections() {
for (const server of this.s.servers.values()) {
return server.closeCheckedOutConnections();
server.closeCheckedOutConnections();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ describe('Connection Pool', function () {
});
});

const metadata: MongoDBMetadataUI = { requires: { mongodb: '>=4.4', topology: 'single' } };
const metadata: MongoDBMetadataUI = { requires: { mongodb: '>=4.4' } };

describe('ConnectionCheckedInEvent', metadata, function () {
let client: MongoClient;
Expand All @@ -89,13 +89,13 @@ describe('Connection Pool', function () {
configureFailPoint: 'failCommand',
mode: 'alwaysOn',
data: {
failCommands: ['insert'],
failCommands: ['find'],
blockConnection: true,
blockTimeMS: 500
}
});

client = this.configuration.newClient();
client = this.configuration.newClient({}, {});
await client.connect();
await Promise.all(Array.from({ length: 100 }, () => client.db().command({ ping: 1 })));
});
Expand All @@ -120,37 +120,37 @@ describe('Connection Pool', function () {
.on('connectionCheckedIn', pushToClientEvents)
.on('connectionClosed', pushToClientEvents);

const inserts = Promise.allSettled([
client.db('test').collection('test').insertOne({ a: 1 }),
client.db('test').collection('test').insertOne({ a: 1 }),
client.db('test').collection('test').insertOne({ a: 1 })
const finds = Promise.allSettled([
client.db('test').collection('test').findOne({ a: 1 }),
client.db('test').collection('test').findOne({ a: 1 }),
client.db('test').collection('test').findOne({ a: 1 })
]);

// wait until all pings are pending on the server
// wait until all finds are pending on the server
while (allClientEvents.filter(e => e.name === 'connectionCheckedOut').length < 3) {
await sleep(1);
}

const insertConnectionIds = allClientEvents
const findConnectionIds = allClientEvents
.filter(e => e.name === 'connectionCheckedOut')
.map(({ address, connectionId }) => `${address} + ${connectionId}`);

await client.close();

const insertCheckInAndCloses = allClientEvents
const findCheckInAndCloses = allClientEvents
.filter(e => e.name === 'connectionCheckedIn' || e.name === 'connectionClosed')
.filter(({ address, connectionId }) =>
insertConnectionIds.includes(`${address} + ${connectionId}`)
findConnectionIds.includes(`${address} + ${connectionId}`)
);

expect(insertCheckInAndCloses).to.have.lengthOf(6);
expect(findCheckInAndCloses).to.have.lengthOf(6);

// check that each check-in is followed by a close (not proceeded by one)
expect(insertCheckInAndCloses.map(e => e.name)).to.deep.equal(
expect(findCheckInAndCloses.map(e => e.name)).to.deep.equal(
Array.from({ length: 3 }, () => ['connectionCheckedIn', 'connectionClosed']).flat(1)
);

await inserts;
await finds;
}
);
});
Expand Down
134 changes: 133 additions & 1 deletion test/integration/node-specific/client_close.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ import {
type FindCursor,
type MongoClient
} from '../../mongodb';
import { configureMongocryptdSpawnHooks } from '../../tools/utils';
import {
clearFailPoint,
configureFailPoint,
configureMongocryptdSpawnHooks,
sleep
} from '../../tools/utils';
import { filterForCommands } from '../shared';
import { runScriptAndGetProcessInfo } from './resource_tracking_script_builder';

Expand Down Expand Up @@ -498,6 +503,44 @@ describe('MongoClient.close() Integration', () => {
});

describe('when MongoClient.close is called', function () {
it('clears resources on a never-connected client', async () => {
client.db('x').collection('y').find();
client.startSession();

expect(client.topology).to.be.undefined;
expect(client.s.activeCursors.size).not.to.equal(0);
expect(client.s.activeSessions.size).not.to.equal(0);
await client.close();
expect(client.topology).to.be.undefined;
expect(client.s.activeCursors.size).to.equal(0);
expect(client.s.activeSessions.size).to.equal(0);
});

it('clears resources on a connected client', async () => {
await client.connect();
client.db('x').collection('y').find();
client.startSession();

expect(client.topology).not.to.be.undefined;
expect(client.s.activeCursors.size).not.to.equal(0);
expect(client.s.activeSessions.size).not.to.equal(0);
await client.close();
expect(client.topology).to.be.undefined;
expect(client.s.activeCursors.size).to.equal(0);
expect(client.s.activeSessions.size).to.equal(0);
});

it('is resilient to invalid close() call orders', async () => {
// close() before connect()
await client.close();
await client.connect();
expect(client.topology).not.to.be.undefined;
// double close
await client.close();
expect(client.topology).to.be.undefined;
await client.close();
});

describe('when sessions are supported', function () {
it('sends an endSessions command', async function () {
await client.db('a').collection('a').insertOne({ a: 1 });
Expand Down Expand Up @@ -716,6 +759,95 @@ describe('MongoClient.close() Integration', () => {
});
});

describe('closeCheckedOutConnections', () => {
const metadata: MongoDBMetadataUI = { requires: { mongodb: '>=4.4' } };
let client: MongoClient;

beforeEach(async function () {
for (const hostAddress of this.configuration.options.hostAddresses) {
await configureFailPoint(
this.configuration,
{
configureFailPoint: 'failCommand',
mode: 'alwaysOn',
data: {
failCommands: ['find'],
blockConnection: true,
blockTimeMS: 500
}
},
`mongodb://${hostAddress}/?directConnection=true`
);
}
client = this.configuration.newClient({}, {});
});

afterEach(async function () {
for (const hostAddress of this.configuration.options.hostAddresses) {
await clearFailPoint(
this.configuration,
'failCommand',
`mongodb://${hostAddress}/?directConnection=true`
);
}
await client?.close();
});

it(
'emits connectionCheckedIn immediately followed by connectionClosed for each in-flight connection across topology',
metadata,
async function () {
type ConnEvent = { name: string; address: string; connectionId: number };
const events: ConnEvent[] = [];
const push = ({ name, address, connectionId }: ConnEvent) =>
events.push({ name, address, connectionId });

client
.on('connectionCheckedOut', push)
.on('connectionCheckedIn', push)
.on('connectionClosed', push);

const finds = Promise.allSettled([
// secondary read ensures at least one connection is checked out from a different pool
client.db('test').collection('test').findOne({ a: 1 }, { readPreference: 'secondary' }),
client.db('test').collection('test').findOne({ a: 1 }),
client.db('test').collection('test').findOne({ a: 1 })
]);

await client.connect();

while (events.filter(e => e.name === 'connectionCheckedOut').length < 3) {
await sleep(1);
}

// all events at this point are connectionCheckedOut; snapshot their ids before close
const findConnKeys = new Set(events.map(e => `${e.address}:${e.connectionId}`));

await client.close();

// spec requires each connectionCheckedIn to be immediately followed by connectionClosed
const closeSequence = events
.filter(
e =>
e.name !== 'connectionCheckedOut' &&
findConnKeys.has(`${e.address}:${e.connectionId}`)
)
.map(e => e.name);

expect(closeSequence).to.deep.equal([
'connectionCheckedIn',
'connectionClosed',
'connectionCheckedIn',
'connectionClosed',
'connectionCheckedIn',
'connectionClosed'
]);

await finds;
}
);
});

describe('Server resource: Cursor', () => {
describe('after cursors are created', () => {
let client: MongoClient;
Expand Down
Loading