Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add maxTimeMS to FindOptions #340

Merged
merged 14 commits into from
Apr 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
});