Skip to content

Commit 38f44df

Browse files
chore: fix a few flaky CSOT tests (#4278)
1 parent 31a1a66 commit 38f44df

File tree

6 files changed

+82
-42
lines changed

6 files changed

+82
-42
lines changed

.evergreen/run-resource-management-feature-integration.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#! /bin/bash
22

3-
source $DRIVERS_TOOLS/.evergreen/init-node-and-npm-env.sh
3+
# source $DRIVERgit addS_TOOLS/.evergreen/init-node-and-npm-env.sh
4+
5+
echo "node: $(node --version)"
6+
echo "npm: $(npm --version)"
47

58
echo "Building driver..."
69
npm pack

test/integration/client-side-operations-timeout/client_side_operations_timeout.prose.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ describe('CSOT spec prose tests', function () {
343343

344344
client = this.configuration.newClient(undefined, {
345345
monitorCommands: true,
346-
timeoutMS: 100,
346+
timeoutMS: 150,
347347
minPoolSize: 20
348348
});
349349
await client.connect();

test/integration/client-side-operations-timeout/node_csot.test.ts

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -361,15 +361,15 @@ describe('CSOT driver tests', metadata, () => {
361361
describe('Non-Tailable cursors', () => {
362362
let client: MongoClient;
363363
let internalClient: MongoClient;
364-
let commandStarted: CommandStartedEvent[];
364+
let commandStarted: (CommandStartedEvent & { command: { maxTimeMS?: number } })[];
365365
let commandSucceeded: CommandSucceededEvent[];
366366
const failpoint: FailPoint = {
367367
configureFailPoint: 'failCommand',
368368
mode: 'alwaysOn',
369369
data: {
370370
failCommands: ['find', 'getMore'],
371371
blockConnection: true,
372-
blockTimeMS: 50
372+
blockTimeMS: 150
373373
}
374374
};
375375

@@ -435,7 +435,7 @@ describe('CSOT driver tests', metadata, () => {
435435
const cursor = client
436436
.db('db')
437437
.collection('coll')
438-
.find({}, { batchSize: 1, timeoutMode: 'iteration', timeoutMS: 100 })
438+
.find({}, { batchSize: 1, timeoutMode: 'iteration', timeoutMS: 200 })
439439
.project({ _id: 0 });
440440

441441
// Iterating over 3 documents in the collection, each artificially taking ~50 ms due to failpoint. If timeoutMS is not refreshed, then we'd expect to error
@@ -457,20 +457,25 @@ describe('CSOT driver tests', metadata, () => {
457457
const cursor = client
458458
.db('db')
459459
.collection('coll')
460-
.find({}, { batchSize: 1, timeoutMode: 'iteration', timeoutMS: 100 })
460+
.find({}, { batchSize: 1, timeoutMode: 'iteration', timeoutMS: 200 })
461461
.project({ _id: 0 });
462462
await cursor.toArray();
463463

464-
expect(commandStarted).to.have.length.gte(3); // Find and 2 getMores
465-
expect(
466-
commandStarted.filter(ev => {
467-
return (
468-
ev.command.find != null &&
469-
ev.command.getMore != null &&
470-
ev.command.maxTimeMS != null
471-
);
472-
})
473-
).to.have.lengthOf(0);
464+
const commands = commandStarted.filter(c =>
465+
['find', 'getMore'].includes(c.commandName)
466+
);
467+
expect(commands).to.have.lengthOf(4); // Find and 2 getMores
468+
469+
const [
470+
{ command: aggregate },
471+
{ command: getMore1 },
472+
{ command: getMore2 },
473+
{ command: getMore3 }
474+
] = commands;
475+
expect(aggregate).not.to.have.property('maxTimeMS');
476+
expect(getMore1).not.to.have.property('maxTimeMS');
477+
expect(getMore2).not.to.have.property('maxTimeMS');
478+
expect(getMore3).not.to.have.property('maxTimeMS');
474479
}
475480
);
476481
});
@@ -644,7 +649,7 @@ describe('CSOT driver tests', metadata, () => {
644649
client = this.configuration.newClient(undefined, { monitorCommands: true, minPoolSize });
645650
commandStarted = [];
646651
client.on('commandStarted', ev => commandStarted.push(ev));
647-
await client.connect();
652+
await waitUntilPoolsFilled(client, AbortSignal.timeout(30_000), minPoolSize);
648653
});
649654

650655
afterEach(async function () {
@@ -685,11 +690,13 @@ describe('CSOT driver tests', metadata, () => {
685690
.db('db')
686691
.collection('coll')
687692
.find({}, { timeoutMS: 150, tailable: true, awaitData: true, batchSize: 1 });
688-
for (let i = 0; i < 5; i++) {
689-
// Iterate cursor 5 times (server would have blocked for 500ms overall, but client
690-
// should not throw
691-
await cursor.next();
692-
}
693+
// Iterate cursor 5 times (server would have blocked for 500ms overall, but client
694+
// should not throw
695+
await cursor.next();
696+
await cursor.next();
697+
await cursor.next();
698+
await cursor.next();
699+
await cursor.next();
693700
});
694701

695702
it('does not use timeoutMS to compute maxTimeMS for getMores', metadata, async function () {

test/integration/crud/client_bulk_write.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,9 @@ describe('Client Bulk Write', function () {
321321

322322
it(
323323
'timeoutMS is refreshed to the timeoutMS passed to the bulk write for the killCursors command',
324-
metadata,
324+
{
325+
requires: { ...metadata.requires, topology: '!load-balanced' }
326+
},
325327
async function () {
326328
const models = await makeMultiResponseBatchModelArray(this.configuration);
327329
const timeoutError = await client

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

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { inspect } from 'util';
77
import {
88
AbstractCursor,
99
type Collection,
10+
type CommandStartedEvent,
1011
CursorTimeoutContext,
1112
CursorTimeoutMode,
1213
type FindCursor,
@@ -17,7 +18,8 @@ import {
1718
MongoServerError,
1819
TimeoutContext
1920
} from '../../mongodb';
20-
import { type FailPoint } from '../../tools/utils';
21+
import { clearFailPoint, configureFailPoint } from '../../tools/utils';
22+
import { filterForCommands } from '../shared';
2123

2224
describe('class AbstractCursor', function () {
2325
describe('regression tests NODE-5372', function () {
@@ -405,9 +407,11 @@ describe('class AbstractCursor', function () {
405407
let client: MongoClient;
406408
let collection: Collection;
407409
let context: CursorTimeoutContext;
410+
const commands: CommandStartedEvent[] = [];
408411

409412
beforeEach(async function () {
410-
client = this.configuration.newClient();
413+
client = this.configuration.newClient({}, { monitorCommands: true });
414+
client.on('commandStarted', filterForCommands('killCursors', commands));
411415

412416
collection = client.db('abstract_cursor_integration').collection('test');
413417

@@ -473,39 +477,59 @@ describe('class AbstractCursor', function () {
473477
});
474478

475479
describe('when the cursor refreshes the timeout for killCursors', function () {
476-
it(
477-
'the provided timeoutContext is not modified',
478-
{
479-
requires: {
480-
mongodb: '>=4.4'
481-
}
482-
},
483-
async function () {
484-
await client.db('admin').command({
480+
let uri: string;
481+
482+
before(function () {
483+
uri = this.configuration.url({ useMultipleMongoses: false });
484+
});
485+
486+
beforeEach(async function () {
487+
commands.length = 0;
488+
await configureFailPoint(
489+
this.configuration,
490+
{
485491
configureFailPoint: 'failCommand',
486492
mode: { times: 1 },
487493
data: {
488494
failCommands: ['getMore'],
489495
blockConnection: true,
490496
blockTimeMS: 5000
491497
}
492-
} as FailPoint);
498+
},
499+
uri
500+
);
501+
});
502+
503+
afterEach(async function () {
504+
await clearFailPoint(this.configuration, uri);
505+
});
493506

507+
it(
508+
'the provided timeoutContext is not modified',
509+
{
510+
requires: {
511+
mongodb: '>=4.4',
512+
topology: '!load-balanced'
513+
}
514+
},
515+
async function () {
494516
const cursor = collection.find(
495517
{},
496518
{
497519
timeoutContext: context,
498-
timeoutMS: 1000,
520+
timeoutMS: 150,
499521
timeoutMode: CursorTimeoutMode.LIFETIME,
500522
batchSize: 1
501523
}
502524
);
503525

526+
const refresh = sinon.spy(context, 'refresh');
527+
const refreshed = sinon.spy(context, 'refreshed');
504528
const error = await cursor.toArray().catch(e => e);
505529

506530
expect(error).to.be.instanceof(MongoOperationTimeoutError);
507-
// @ts-expect-error We know we have a CSOT timeout context but TS does not.
508-
expect(context.timeoutContext.remainingTimeMS).to.be.lessThan(0);
531+
expect(refresh.called).to.be.false;
532+
expect(refreshed.called).to.be.true;
509533
}
510534
);
511535
});

test/tools/utils.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -601,8 +601,12 @@ export async function waitUntilPoolsFilled(
601601
await Promise.all([wait$(), client.connect()]);
602602
}
603603

604-
export async function configureFailPoint(configuration: TestConfiguration, failPoint: FailPoint) {
605-
const utilClient = configuration.newClient();
604+
export async function configureFailPoint(
605+
configuration: TestConfiguration,
606+
failPoint: FailPoint,
607+
uri = configuration.url()
608+
) {
609+
const utilClient = configuration.newClient(uri);
606610
await utilClient.connect();
607611

608612
try {
@@ -612,8 +616,8 @@ export async function configureFailPoint(configuration: TestConfiguration, failP
612616
}
613617
}
614618

615-
export async function clearFailPoint(configuration: TestConfiguration) {
616-
const utilClient = configuration.newClient();
619+
export async function clearFailPoint(configuration: TestConfiguration, uri = configuration.url()) {
620+
const utilClient = configuration.newClient(uri);
617621
await utilClient.connect();
618622

619623
try {

0 commit comments

Comments
 (0)