Skip to content

Commit 4caa93a

Browse files
committed
TODO: t.mjs script repros but not test
1 parent d2964ba commit 4caa93a

File tree

2 files changed

+74
-4
lines changed

2 files changed

+74
-4
lines changed

t.mjs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#! /usr/bin/env node --unhandled-rejections=strict --enable-source-maps
2+
import util from 'node:util';
3+
4+
import { Code, MongoClient } from './lib/index.js';
5+
6+
util.inspect.defaultOptions.depth = 1000;
7+
const client = new MongoClient(process.env.MONGODB_URI);
8+
9+
async function main() {
10+
await client.connect();
11+
const collection = client.db('test_db').collection('test_collection');
12+
await collection.insertMany([{}]);
13+
14+
const updateOne = {
15+
filter: { $where: new Code('function () { sleep(1 * 100); return true; }') },
16+
update: { $inc: { x: 1 } }
17+
};
18+
const fnRes = await collection.bulkWrite([{ updateOne }], { maxTimeMS: 4 }).then(
19+
res => ({ res }),
20+
err => ({ err })
21+
);
22+
console.log({ fnRes });
23+
}
24+
25+
main(process.argv)
26+
.then(console.log)
27+
.catch(console.error)
28+
.finally(() => client.close());
29+
// node --unhandled-rejections=strict --enable-source-maps script.js

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

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/* Anything javascript specific relating to timeouts */
22
import { expect } from 'chai';
3-
import * as semver from 'semver';
43
import * as sinon from 'sinon';
54

65
import {
76
BSON,
87
type ClientSession,
8+
Code,
99
type Collection,
1010
Connection,
1111
type Db,
@@ -231,15 +231,56 @@ describe('CSOT driver tests', () => {
231231
});
232232
});
233233

234-
describe('when a maxTimeExpired error is returned inside a writeErrors array', () => {
235-
// Okay so allegedly this can never happen.
236-
// But the spec says it can, so let's be defensive and support it.
234+
describe('when a maxTimeExpired error is returned as the first error in a writeErrors array', () => {
237235
// {ok: 1, writeErrors: [{code: 50, codeName: "MaxTimeMSExpired", errmsg: "operation time limit exceeded"}]}
238236

237+
let client;
238+
let collection: Collection;
239+
240+
beforeEach(async function () {
241+
const utilClient = this.configuration.newClient();
242+
const collectionTmp = utilClient.db('test_db').collection('bulkWriteErrors');
243+
await collectionTmp.drop().catch(() => null);
244+
await collectionTmp.insertMany(Array.from({ length: 1000 }, () => ({})));
245+
utilClient.close();
246+
247+
client = this.configuration.newClient({ timeoutMS: 500_000, monitorCommands: true });
248+
collection = client.db('test_db').collection('bulkWriteErrors');
249+
});
250+
251+
afterEach(async () => {
252+
collection = undefined;
253+
await client.close();
254+
});
255+
256+
it('throws a MongoOperationTimeoutError error and emits command succeeded', async () => {
257+
const updateOne = {
258+
filter: { $where: new Code('function () { sleep(1 * 100); return true; }') },
259+
update: { $inc: { x: 1 } }
260+
};
261+
262+
const error = await collection.bulkWrite([{ updateOne }], { maxTimeMS: 4 }).catch(e => e);
263+
264+
expect(error).to.be.instanceOf(MongoOperationTimeoutError);
265+
expect(error.cause).to.be.instanceOf(MongoServerError);
266+
expect(error.cause).to.have.property('code', 50);
267+
268+
expect(commandsSucceeded).to.have.lengthOf(1);
269+
expect(commandsSucceeded).to.have.nested.property('[0].reply.writeErrors[0].code', 50);
270+
});
271+
});
272+
273+
describe('when a maxTimeExpired error is returned deeper inside a writeErrors array', () => {
274+
// The server should always return one maxTimeExpiredError at the front of the writeErrors array
275+
// But for the sake of defensive programming we will find any maxTime error in the array.
276+
239277
beforeEach(async () => {
240278
const writeErrorsReply = BSON.serialize({
241279
ok: 1,
242280
writeErrors: [
281+
{ code: 2, codeName: 'MaxTimeMSExpired', errmsg: 'operation time limit exceeded' },
282+
{ code: 3, codeName: 'MaxTimeMSExpired', errmsg: 'operation time limit exceeded' },
283+
{ code: 4, codeName: 'MaxTimeMSExpired', errmsg: 'operation time limit exceeded' },
243284
{ code: 50, codeName: 'MaxTimeMSExpired', errmsg: 'operation time limit exceeded' }
244285
]
245286
});

0 commit comments

Comments
 (0)