Skip to content

Commit 6e8bd46

Browse files
committed
test: no kill cursors on lb and don't wait on connection close
1 parent 5af48af commit 6e8bd46

File tree

1 file changed

+25
-29
lines changed

1 file changed

+25
-29
lines changed

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

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,18 @@ describe('AbortSignal support', () => {
215215
let signal: AbortSignal;
216216
let cursor: AbstractCursor<{ a: number }>;
217217
const commandsStarted = [];
218+
let waitForKillCursors;
218219

219220
beforeEach(async function () {
221+
waitForKillCursors =
222+
this.configuration.topologyType === 'LoadBalanced'
223+
? async () => null
224+
: async () => {
225+
for await (const [ev] of events.on(client, 'commandStarted')) {
226+
if (ev.commandName === 'killCursors') return ev;
227+
}
228+
};
229+
220230
commandsStarted.length = 0;
221231
const utilClient = this.configuration.newClient();
222232
try {
@@ -243,12 +253,6 @@ describe('AbortSignal support', () => {
243253
client.on('commandStarted', e => commandsStarted.push(e));
244254
});
245255

246-
const waitForKillCursors = async () => {
247-
for await (const [ev] of events.on(client, 'commandStarted')) {
248-
if (ev.commandName === 'killCursors') return ev;
249-
}
250-
};
251-
252256
afterEach(async () => {
253257
await cursor?.close();
254258
sinon.restore();
@@ -398,36 +402,30 @@ describe('AbortSignal support', () => {
398402
let controller: AbortController;
399403
let signal: AbortSignal;
400404
let cursor: AbstractCursor<{ a: number }>;
401-
let checkedOutId;
402-
const waitForConnectionClosed = async () => {
403-
for await (const [ev] of events.on(client, 'connectionClosed')) {
404-
if ((ev as ConnectionClosedEvent).connectionId === checkedOutId) return ev;
405-
}
406-
};
407405

408406
beforeEach(async function () {
409-
checkedOutId = undefined;
410407
controller = new AbortController();
411408
signal = controller.signal;
412409
cursor = method(filter, { signal });
413410
});
414411

415412
afterEach(async function () {
416-
checkedOutId = undefined;
417413
sinon.restore();
418414
await cursor?.close();
419415
});
420416

421417
it(`rejects ${cursorAPI.toString()}`, async () => {
422418
await db.command({ ping: 1 }, { readPreference: 'primary' }); // fill the connection pool with 1 connection.
423-
const connectionClosed = waitForConnectionClosed();
424419

425-
client.on('connectionCheckedOut', ev => (checkedOutId = ev.connectionId));
426420
const willBeResultBlocked = iterateUntilDocumentOrError(cursor, cursorAPI, args);
427421

422+
let cursorCommandSocket;
423+
428424
for (const [, server] of client.topology.s.servers) {
429425
//@ts-expect-error: private property
430426
for (const connection of server.pool.connections) {
427+
//@ts-expect-error: private property
428+
cursorCommandSocket = connection.socket;
431429
//@ts-expect-error: private property
432430
const stub = sinon.stub(connection.socket, 'write').callsFake(function (...args) {
433431
controller.abort();
@@ -444,7 +442,7 @@ describe('AbortSignal support', () => {
444442

445443
expect(result).to.be.instanceOf(DOMException);
446444

447-
await connectionClosed;
445+
expect(cursorCommandSocket).to.have.property('destroyed', true);
448446
});
449447
}
450448

@@ -471,38 +469,36 @@ describe('AbortSignal support', () => {
471469
}
472470
});
473471

474-
checkedOutId = undefined;
475472
controller = new AbortController();
476473
signal = controller.signal;
477474
cursor = method(filter, { signal });
478475
});
479476

480-
let checkedOutId;
481-
const waitForConnectionClosed = async () => {
482-
for await (const [ev] of events.on(client, 'connectionClosed')) {
483-
if ((ev as ConnectionClosedEvent).connectionId === checkedOutId) return ev;
484-
}
485-
};
486-
487477
afterEach(async function () {
488-
checkedOutId = undefined;
489478
await clearFailPoint(this.configuration);
490479
await cursor?.close();
491480
});
492481

493482
it(`rejects ${cursorAPI.toString()}`, async () => {
494483
await db.command({ ping: 1 }, { readPreference: 'primary' }); // fill the connection pool with 1 connection.
495-
const connectionClosed = waitForConnectionClosed();
496484

497-
client.on('connectionCheckedOut', ev => (checkedOutId = ev.connectionId));
485+
let cursorCommandSocket;
486+
for (const [, server] of client.topology.s.servers) {
487+
//@ts-expect-error: private property
488+
for (const connection of server.pool.connections) {
489+
//@ts-expect-error: private property
490+
cursorCommandSocket = connection.socket;
491+
}
492+
}
493+
498494
client.on('commandStarted', e => e.commandName === cursorName && controller.abort());
499495
const willBeResultBlocked = iterateUntilDocumentOrError(cursor, cursorAPI, args);
500496

501497
const result = await willBeResultBlocked;
502498

503499
expect(result).to.be.instanceOf(DOMException);
504500

505-
await connectionClosed;
501+
expect(cursorCommandSocket).to.have.property('destroyed', true);
506502
});
507503
}
508504

0 commit comments

Comments
 (0)