Skip to content

Commit

Permalink
Merge pull request #340 from BrunoBernardino/patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
lucsoft authored Apr 1, 2022
2 parents 5d51c12 + c764fbe commit f7e439c
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ export interface FindOptions {
projection?: Document;
sort?: Document;
noCursorTimeout?: boolean;
/**
* The maximum time of milliseconds the operation is allowed to take
*/
maxTimeMS?: number;
}

export interface ListDatabaseInfo {
Expand Down
59 changes: 58 additions & 1 deletion tests/cases/03_curd.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { MongoInvalidArgumentError } from "../../src/error.ts";
import { testWithClient, testWithTestDBClient } from "../common.ts";
import { assert, assertEquals, assertRejects } from "../test.deps.ts";
import { assert, assertEquals, assertRejects, semver } from "../test.deps.ts";

interface IUser {
username?: string;
Expand Down Expand Up @@ -578,3 +578,60 @@ testWithTestDBClient("testFindEmptyAsyncIteration", async (db) => {

await db.collection("mongo_test_users").drop();
});

testWithClient("testFindWithMaxTimeMS", async (client) => {
const db = client.database("local");

const supportsMaxTimeMSInFindOne = semver.gte(
client.buildInfo!.version,
"4.2.0",
);

const users = db.collection<IUser>("mongo_test_users");
for (let i = 0; i < 10; i++) {
await users.insertOne({
username: "testFindWithMaxTimeMS",
password: "pass1",
uid: i,
});
}
const users1 = await users.find({
uid: 0,
}, { maxTimeMS: 100 }).toArray();

assertEquals(users1.length, 1);

const user1 = await users.findOne({
uid: 0,
}, { maxTimeMS: 100 });

assertEquals(user1!.uid, 0);

try {
await users.find({
uid: 0,
$where: "sleep(10) || true",
}, { maxTimeMS: 1 }).toArray();
assert(false);
} catch (e) {
assertEquals(e.ok, 0);
assertEquals(e.codeName, "MaxTimeMSExpired");
assertEquals(e.errmsg, "operation exceeded time limit");
}

if (supportsMaxTimeMSInFindOne) {
try {
await users.findOne({
uid: 0,
$where: "sleep(10) || true",
}, { maxTimeMS: 1 });
assert(false);
} catch (e) {
assertEquals(e.ok, 0);
assertEquals(e.codeName, "MaxTimeMSExpired");
assertEquals(e.errmsg, "operation exceeded time limit");
}
}

await db.collection("mongo_test_users").drop();
});

0 comments on commit f7e439c

Please sign in to comment.