Skip to content

Commit 165a852

Browse files
committed
chore: fix leaky cursors tests
1 parent bf4d871 commit 165a852

File tree

3 files changed

+41
-14
lines changed

3 files changed

+41
-14
lines changed

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

+21-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
FindCursor,
1717
ListCollectionsCursor,
1818
type Log,
19+
Long,
1920
type MongoClient,
2021
MongoServerError,
2122
promiseWithResolvers,
@@ -52,9 +53,11 @@ describe('AbortSignal support', () => {
5253
let db: Db;
5354
let collection: Collection<{ a: number; ssn: string }>;
5455
const logs: Log[] = [];
56+
const cursors = [];
5557

5658
beforeEach(async function () {
5759
logs.length = 0;
60+
cursors.length = 0;
5861

5962
client = this.configuration.newClient(
6063
{},
@@ -70,17 +73,34 @@ describe('AbortSignal support', () => {
7073
await client.connect();
7174
db = client.db('abortSignal');
7275
collection = db.collection('support');
76+
77+
client.on('commandSucceeded', ev => {
78+
if (
79+
ev.commandName === 'find' ||
80+
ev.commandName === 'aggregate' ||
81+
ev.commandName === 'listCollections'
82+
) {
83+
const cursorId = Long.isLong(ev.reply.cursor.id)
84+
? ev.reply.cursor.id
85+
: Long.fromNumber(ev.reply.cursor.id);
86+
87+
if (!Long.ZERO.equals(cursorId)) {
88+
cursors.push(cursorId);
89+
}
90+
}
91+
});
7392
});
7493

7594
afterEach(async function () {
7695
logs.length = 0;
7796
const utilClient = this.configuration.newClient();
7897
try {
98+
await utilClient.db('abortSignal').command({ killCursors: 'support', cursors });
7999
await utilClient.db('abortSignal').collection('support').deleteMany({});
80100
} finally {
81101
await utilClient.close();
102+
await client?.close();
82103
}
83-
await client?.close();
84104
});
85105

86106
function testCursor(cursorName: string, constructor: any) {

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

+19-6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
type ChangeStream,
88
ClientSession,
99
type Collection,
10+
Long,
1011
MongoClient,
1112
MongoNotConnectedError,
1213
ProfilingLevel,
@@ -19,7 +20,7 @@ describe('When executing an operation for the first time', () => {
1920
let client: MongoClient;
2021

2122
beforeEach('create client', async function () {
22-
client = this.configuration.newClient();
23+
client = this.configuration.newClient({}, { monitorCommands: true });
2324
});
2425

2526
beforeEach('create test namespace', async function () {
@@ -212,26 +213,38 @@ describe('When executing an operation for the first time', () => {
212213
let changeCausingCollection: Collection;
213214
let collection: Collection;
214215
let cs: ChangeStream;
216+
const cursors = [];
215217

216218
beforeEach(async function () {
219+
cursors.length = 0;
220+
217221
if (this.configuration.topologyType === TopologyType.Single) {
218222
return;
219223
}
220224
changeCausingClient = this.configuration.newClient();
221-
await changeCausingClient
225+
changeCausingCollection = await changeCausingClient
222226
.db('auto-connect-change')
223227
.createCollection('auto-connect')
224228
.catch(() => null);
225229

226-
changeCausingCollection = changeCausingClient
227-
.db('auto-connect-change')
228-
.collection('auto-connect');
229-
230230
collection = client.db('auto-connect-change').collection('auto-connect');
231231
cs = collection.watch();
232+
233+
client.on('commandSucceeded', ev => {
234+
if (ev.commandName === 'aggregate') {
235+
const cursorId = Long.isLong(ev.reply.cursor.id)
236+
? ev.reply.cursor.id
237+
: Long.fromNumber(ev.reply.cursor.id);
238+
239+
if (!Long.ZERO.equals(cursorId)) {
240+
cursors.push(cursorId);
241+
}
242+
}
243+
});
232244
});
233245

234246
afterEach(async function () {
247+
await client.db('auto-connect-change').command({ killCursors: 'auto-connect', cursors });
235248
await changeCausingClient?.close();
236249
await cs?.close();
237250
});

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

+1-7
Original file line numberDiff line numberDiff line change
@@ -755,13 +755,7 @@ describe('MongoClient.close() Integration', () => {
755755
);
756756
};
757757

758-
cursor = coll.find(
759-
{},
760-
{
761-
tailable: true,
762-
awaitData: true
763-
}
764-
);
758+
cursor = coll.find({}, { batchSize: 1 });
765759
await cursor.next();
766760

767761
// assert creation

0 commit comments

Comments
 (0)