Skip to content

Commit

Permalink
TODO: t.mjs script repros but not test
Browse files Browse the repository at this point in the history
  • Loading branch information
nbbeeken committed Aug 8, 2024
1 parent d2964ba commit 4caa93a
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 4 deletions.
29 changes: 29 additions & 0 deletions t.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#! /usr/bin/env node --unhandled-rejections=strict --enable-source-maps
import util from 'node:util';

import { Code, MongoClient } from './lib/index.js';

util.inspect.defaultOptions.depth = 1000;
const client = new MongoClient(process.env.MONGODB_URI);

async function main() {
await client.connect();
const collection = client.db('test_db').collection('test_collection');
await collection.insertMany([{}]);

const updateOne = {
filter: { $where: new Code('function () { sleep(1 * 100); return true; }') },
update: { $inc: { x: 1 } }
};
const fnRes = await collection.bulkWrite([{ updateOne }], { maxTimeMS: 4 }).then(
res => ({ res }),
err => ({ err })
);
console.log({ fnRes });
}

main(process.argv)
.then(console.log)
.catch(console.error)
.finally(() => client.close());
// node --unhandled-rejections=strict --enable-source-maps script.js
49 changes: 45 additions & 4 deletions test/integration/client-side-operations-timeout/node_csot.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/* Anything javascript specific relating to timeouts */
import { expect } from 'chai';
import * as semver from 'semver';
import * as sinon from 'sinon';

import {
BSON,
type ClientSession,
Code,
type Collection,
Connection,
type Db,
Expand Down Expand Up @@ -231,15 +231,56 @@ describe('CSOT driver tests', () => {
});
});

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

let client;
let collection: Collection;

beforeEach(async function () {
const utilClient = this.configuration.newClient();
const collectionTmp = utilClient.db('test_db').collection('bulkWriteErrors');
await collectionTmp.drop().catch(() => null);
await collectionTmp.insertMany(Array.from({ length: 1000 }, () => ({})));
utilClient.close();

client = this.configuration.newClient({ timeoutMS: 500_000, monitorCommands: true });
collection = client.db('test_db').collection('bulkWriteErrors');
});

afterEach(async () => {
collection = undefined;
await client.close();
});

it('throws a MongoOperationTimeoutError error and emits command succeeded', async () => {
const updateOne = {
filter: { $where: new Code('function () { sleep(1 * 100); return true; }') },
update: { $inc: { x: 1 } }
};

const error = await collection.bulkWrite([{ updateOne }], { maxTimeMS: 4 }).catch(e => e);

expect(error).to.be.instanceOf(MongoOperationTimeoutError);
expect(error.cause).to.be.instanceOf(MongoServerError);
expect(error.cause).to.have.property('code', 50);

expect(commandsSucceeded).to.have.lengthOf(1);
expect(commandsSucceeded).to.have.nested.property('[0].reply.writeErrors[0].code', 50);
});
});

describe('when a maxTimeExpired error is returned deeper inside a writeErrors array', () => {
// The server should always return one maxTimeExpiredError at the front of the writeErrors array
// But for the sake of defensive programming we will find any maxTime error in the array.

beforeEach(async () => {
const writeErrorsReply = BSON.serialize({
ok: 1,
writeErrors: [
{ code: 2, codeName: 'MaxTimeMSExpired', errmsg: 'operation time limit exceeded' },
{ code: 3, codeName: 'MaxTimeMSExpired', errmsg: 'operation time limit exceeded' },
{ code: 4, codeName: 'MaxTimeMSExpired', errmsg: 'operation time limit exceeded' },
{ code: 50, codeName: 'MaxTimeMSExpired', errmsg: 'operation time limit exceeded' }
]
});
Expand Down

0 comments on commit 4caa93a

Please sign in to comment.