Skip to content

Commit

Permalink
comments
Browse files Browse the repository at this point in the history
  • Loading branch information
baileympearson committed Oct 10, 2024
1 parent 79aa064 commit 5792dfd
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 25 deletions.
17 changes: 7 additions & 10 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -517,18 +517,15 @@ export function hasAtomicOperators(doc: Document | Document[]): boolean {

export function resolveTimeoutOptions<T extends Partial<TimeoutContextOptions>>(
client: MongoClient,
options?: T
): Pick<
MongoClient['s']['options'],
'serverSelectionTimeoutMS' | 'socketTimeoutMS' | 'waitQueueTimeoutMS' | 'timeoutMS'
> &
T {
options: T
): T &
Pick<
MongoClient['s']['options'],
'timeoutMS' | 'serverSelectionTimeoutMS' | 'waitQueueTimeoutMS' | 'socketTimeoutMS'
> {
const { socketTimeoutMS, serverSelectionTimeoutMS, waitQueueTimeoutMS, timeoutMS } =
client.s.options;
return Object.assign(
{ socketTimeoutMS, serverSelectionTimeoutMS, waitQueueTimeoutMS, timeoutMS },
options
);
return { socketTimeoutMS, serverSelectionTimeoutMS, waitQueueTimeoutMS, timeoutMS, ...options };
}
/**
* Merge inherited properties from parent into options, prioritizing values from options,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1266,7 +1266,7 @@ describe('CSOT spec prose tests', function () {
const error = await client.bulkWrite(models).catch(error => error);

expect(error, error.stack).to.be.instanceOf(MongoOperationTimeoutError);
expect(writes.length).to.equal(2);
expect(writes).to.have.lengthOf(2);
});
}
);
Expand Down
63 changes: 49 additions & 14 deletions test/integration/crud/client_bulk_write.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { expect } from 'chai';
import { setTimeout } from 'timers/promises';
import { inspect } from 'util';

import {
type CommandStartedEvent,
type Connection,
type ConnectionPool,
type MongoClient,
MongoError,
MongoOperationTimeoutError,
now,
TimeoutContext
} from '../../mongodb';
import {
Expand Down Expand Up @@ -45,6 +48,7 @@ describe('Client Bulk Write', function () {
});

it('timeoutMS is used as the timeout for the bulk write', metadata, async function () {
const start = now();
const timeoutError = await client
.bulkWrite([
{
Expand All @@ -54,7 +58,9 @@ describe('Client Bulk Write', function () {
}
])
.catch(e => e);
expect(timeoutError).to.be.instanceOf(MongoOperationTimeoutError);
const end = now();
expect(timeoutError).to.be.instanceOf(MongoError);
expect(end - start).to.be.within(300 - 100, 300 + 100);
});
});

Expand All @@ -72,6 +78,7 @@ describe('Client Bulk Write', function () {
});

it('timeoutMS is used as the timeout for the bulk write', metadata, async function () {
const start = now();
const timeoutError = await client
.bulkWrite(
[
Expand All @@ -84,7 +91,9 @@ describe('Client Bulk Write', function () {
{ timeoutMS: 300 }
)
.catch(e => e);
expect(timeoutError).to.be.instanceOf(MongoOperationTimeoutError);
const end = now();
expect(timeoutError).to.be.instanceOf(MongoError);
expect(end - start).to.be.within(300 - 100, 300 + 100);
});
});

Expand All @@ -102,6 +111,7 @@ describe('Client Bulk Write', function () {
});

it('bulk write options take precedence over the client options', metadata, async function () {
const start = now();
const timeoutError = await client
.bulkWrite(
[
Expand All @@ -114,7 +124,9 @@ describe('Client Bulk Write', function () {
{ timeoutMS: 300 }
)
.catch(e => e);
expect(timeoutError).to.be.instanceOf(MongoOperationTimeoutError);
const end = now();
expect(timeoutError).to.be.instanceOf(MongoError);
expect(end - start).to.be.within(300 - 100, 300 + 100);
});
});

Expand Down Expand Up @@ -150,7 +162,9 @@ describe('Client Bulk Write', function () {
await client.close();
});

it('respects timeoutMS for a single batch', async function () {
it('a single batch bulk write does not take longer than timeoutMS', async function () {
const start = now();
let end;
const timeoutError = client
.bulkWrite(
[
Expand All @@ -162,12 +176,16 @@ describe('Client Bulk Write', function () {
],
{ timeoutMS: 200, writeConcern: { w: 0 } }
)
.catch(e => e);
.catch(e => e)
.then(e => {
end = now();
return e;
});

await setTimeout(250);

const error = await timeoutError;
expect(error).to.be.instanceOf(MongoOperationTimeoutError);
expect(await timeoutError).to.be.instanceOf(MongoError);
expect(end - start).to.be.within(200 - 100, 200 + 100);
});

it(
Expand All @@ -180,12 +198,18 @@ describe('Client Bulk Write', function () {
},
async function () {
const models = await makeMultiBatchWrite(this.configuration);
const start = now();
let end;
const timeoutError = client
.bulkWrite(models, {
timeoutMS: 400,
writeConcern: { w: 0 }
})
.catch(e => e);
.catch(e => e)
.then(r => {
end = now();
return r;
});

await setTimeout(210);

Expand All @@ -199,8 +223,8 @@ describe('Client Bulk Write', function () {

await setTimeout(210);

const error = await timeoutError;
expect(error).to.be.instanceOf(MongoOperationTimeoutError);
expect(await timeoutError).to.be.instanceOf(MongoError);
expect(end - start).to.be.within(400 - 100, 400 + 100);
}
);
}
Expand All @@ -221,6 +245,7 @@ describe('Client Bulk Write', function () {
});

it('the operation times out', metadata, async function () {
const start = now();
const timeoutError = await client
.bulkWrite(
[
Expand All @@ -233,7 +258,9 @@ describe('Client Bulk Write', function () {
{ timeoutMS: 300 }
)
.catch(e => e);
expect(timeoutError).to.be.instanceOf(MongoOperationTimeoutError);
const end = now();
expect(timeoutError).to.be.instanceOf(MongoError);
expect(end - start).to.be.within(300 - 100, 300 + 100);
});
});

Expand All @@ -242,7 +269,7 @@ describe('Client Bulk Write', function () {

beforeEach(async function () {
client = this.configuration.newClient({}, { monitorCommands: true });
client.on('commandStarted', filterForCommands('getMore', commands));
client.on('commandStarted', filterForCommands(['getMore'], commands));
await client.connect();

await configureFailPoint(this.configuration, {
Expand All @@ -254,14 +281,19 @@ describe('Client Bulk Write', function () {

it('the bulk write operation times out', metadata, async function () {
const models = await makeMultiResponseBatchModelArray(this.configuration);
const start = now();
const timeoutError = await client
.bulkWrite(models, {
verboseResults: true,
timeoutMS: 1500
})
.catch(e => e);

expect(timeoutError).to.be.instanceOf(MongoOperationTimeoutError);
const end = now();
expect(timeoutError).to.be.instanceOf(MongoError);

// DRIVERS-3005 - killCursors causes cursor cleanup to extend past timeoutMS.
expect(end - start).to.be.within(2000 - 100, 2000 + 100);
expect(commands).to.have.lengthOf(1);
});
});
Expand Down Expand Up @@ -333,13 +365,16 @@ describe('Client Bulk Write', function () {
},
async function () {
const models = await makeMultiBatchWrite(this.configuration);
const start = now();
const timeoutError = await client
.bulkWrite(models, {
timeoutMS: 2000
})
.catch(e => e);
expect(timeoutError, timeoutError.stack).to.be.instanceOf(MongoOperationTimeoutError);

const end = now();
expect(timeoutError).to.be.instanceOf(MongoError);
expect(end - start).to.be.within(2000 - 100, 2000 + 100);
expect(commands.length, 'Test must execute two batches.').to.equal(2);
}
);
Expand Down

0 comments on commit 5792dfd

Please sign in to comment.