From 3d08ac5ed4b9d52d75a43a1192b8d0f083d2f102 Mon Sep 17 00:00:00 2001 From: Bruno Bernardino Date: Thu, 31 Mar 2022 15:44:45 +0100 Subject: [PATCH 01/14] Add `maxTimeMS` to FindOptions This is allowed, works, and is already being sent over, so it's really just a type thing. --- src/types.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/types.ts b/src/types.ts index 1affe2b..559eb72 100644 --- a/src/types.ts +++ b/src/types.ts @@ -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 { From 18ca45bbbdc10c9434e9cf920d769ebcee9d062f Mon Sep 17 00:00:00 2001 From: Bruno Bernardino Date: Fri, 1 Apr 2022 12:34:44 +0100 Subject: [PATCH 02/14] Add test case for passing and failing `maxTimeMS` option in `find` and `findOne`. --- tests/cases/03_curd.ts | 50 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/tests/cases/03_curd.ts b/tests/cases/03_curd.ts index 26140ab..754c7f3 100644 --- a/tests/cases/03_curd.ts +++ b/tests/cases/03_curd.ts @@ -1,4 +1,4 @@ -import { MongoInvalidArgumentError } from "../../src/error.ts"; +import { MongoError, MongoInvalidArgumentError } from "../../src/error.ts"; import { testWithClient, testWithTestDBClient } from "../common.ts"; import { assert, assertEquals, assertRejects } from "../test.deps.ts"; @@ -578,3 +578,51 @@ testWithTestDBClient("testFindEmptyAsyncIteration", async (db) => { await db.collection("mongo_test_users").drop(); }); + +testWithTestDBClient("testFindWithMaxTimeMS", async (db) => { + const users = db.collection("mongo_test_users"); + for (let i = 0; i < 10; i++) { + await users.insertOne({ + username: "testFindWithSort", + 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(5) || true", + }, { maxTimeMS: 1 }).toArray(); + assert(false); + } catch (e) { + assertEquals(e.ok, 0); + assertEquals(e.codeName, "MaxTimeMSExpired"); + assertEquals(e.errmsg, "operation exceeded time limit"); + } + + try { + await users.findOne({ + uid: 0, + $where: "sleep(5) || 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(); +}); From d087ffd639a984da4ecff854c85aced11afea742 Mon Sep 17 00:00:00 2001 From: Bruno Bernardino Date: Fri, 1 Apr 2022 12:36:15 +0100 Subject: [PATCH 03/14] Remove unused MongoError --- tests/cases/03_curd.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/cases/03_curd.ts b/tests/cases/03_curd.ts index 754c7f3..31eb618 100644 --- a/tests/cases/03_curd.ts +++ b/tests/cases/03_curd.ts @@ -1,4 +1,4 @@ -import { MongoError, MongoInvalidArgumentError } from "../../src/error.ts"; +import { MongoInvalidArgumentError } from "../../src/error.ts"; import { testWithClient, testWithTestDBClient } from "../common.ts"; import { assert, assertEquals, assertRejects } from "../test.deps.ts"; From f7deaed04cb812764a87599f10a5635574ab69f4 Mon Sep 17 00:00:00 2001 From: Bruno Bernardino Date: Fri, 1 Apr 2022 12:37:11 +0100 Subject: [PATCH 04/14] Fix fake username, so it doesn't look like it's in the wrong place --- tests/cases/03_curd.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/cases/03_curd.ts b/tests/cases/03_curd.ts index 31eb618..e250534 100644 --- a/tests/cases/03_curd.ts +++ b/tests/cases/03_curd.ts @@ -583,7 +583,7 @@ testWithTestDBClient("testFindWithMaxTimeMS", async (db) => { const users = db.collection("mongo_test_users"); for (let i = 0; i < 10; i++) { await users.insertOne({ - username: "testFindWithSort", + username: "testFindWithMaxTimeMS", password: "pass1", uid: i, }); From 81cc0a416bca403b1d3de726529ca5d796989f29 Mon Sep 17 00:00:00 2001 From: Bruno Bernardino Date: Fri, 1 Apr 2022 12:39:12 +0100 Subject: [PATCH 05/14] Support older mongo versions throwing the error --- tests/cases/03_curd.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/cases/03_curd.ts b/tests/cases/03_curd.ts index e250534..473e367 100644 --- a/tests/cases/03_curd.ts +++ b/tests/cases/03_curd.ts @@ -607,7 +607,7 @@ testWithTestDBClient("testFindWithMaxTimeMS", async (db) => { }, { maxTimeMS: 1 }).toArray(); assert(false); } catch (e) { - assertEquals(e.ok, 0); + assertEquals(Boolean(e.ok), false); assertEquals(e.codeName, "MaxTimeMSExpired"); assertEquals(e.errmsg, "operation exceeded time limit"); } @@ -619,7 +619,7 @@ testWithTestDBClient("testFindWithMaxTimeMS", async (db) => { }, { maxTimeMS: 1 }); assert(false); } catch (e) { - assertEquals(e.ok, 0); + assertEquals(Boolean(e.ok), false); assertEquals(e.codeName, "MaxTimeMSExpired"); assertEquals(e.errmsg, "operation exceeded time limit"); } From f5dc34241bc40f449b3292160b317a14c7316743 Mon Sep 17 00:00:00 2001 From: Bruno Bernardino Date: Fri, 1 Apr 2022 16:42:13 +0100 Subject: [PATCH 06/14] Fix test case for previous versions of mongo --- tests/cases/03_curd.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/tests/cases/03_curd.ts b/tests/cases/03_curd.ts index 473e367..d6b6e9c 100644 --- a/tests/cases/03_curd.ts +++ b/tests/cases/03_curd.ts @@ -607,8 +607,9 @@ testWithTestDBClient("testFindWithMaxTimeMS", async (db) => { }, { maxTimeMS: 1 }).toArray(); assert(false); } catch (e) { - assertEquals(Boolean(e.ok), false); - assertEquals(e.codeName, "MaxTimeMSExpired"); + typeof e.ok !== "undefined" && assertEquals(e.ok, 0); + typeof e.codeName !== "undefined" && + assertEquals(e.codeName, "MaxTimeMSExpired"); assertEquals(e.errmsg, "operation exceeded time limit"); } @@ -619,8 +620,9 @@ testWithTestDBClient("testFindWithMaxTimeMS", async (db) => { }, { maxTimeMS: 1 }); assert(false); } catch (e) { - assertEquals(Boolean(e.ok), false); - assertEquals(e.codeName, "MaxTimeMSExpired"); + typeof e.ok !== "undefined" && assertEquals(e.ok, 0); + typeof e.codeName !== "undefined" && + assertEquals(e.codeName, "MaxTimeMSExpired"); assertEquals(e.errmsg, "operation exceeded time limit"); } From 5635dab2412c58df423807aba1107f8358a84421 Mon Sep 17 00:00:00 2001 From: Bruno Bernardino Date: Fri, 1 Apr 2022 16:45:09 +0100 Subject: [PATCH 07/14] _Really_ fix the test for mongo 4.0 --- tests/cases/03_curd.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/tests/cases/03_curd.ts b/tests/cases/03_curd.ts index d6b6e9c..7b38e8e 100644 --- a/tests/cases/03_curd.ts +++ b/tests/cases/03_curd.ts @@ -610,7 +610,10 @@ testWithTestDBClient("testFindWithMaxTimeMS", async (db) => { typeof e.ok !== "undefined" && assertEquals(e.ok, 0); typeof e.codeName !== "undefined" && assertEquals(e.codeName, "MaxTimeMSExpired"); - assertEquals(e.errmsg, "operation exceeded time limit"); + typeof e.errmsg !== "undefined" && + assertEquals(e.errmsg, "operation exceeded time limit"); + typeof e.errmsg === "undefined" && + assert(e.toString().includes("operation exceeded time limit")); } try { @@ -623,7 +626,10 @@ testWithTestDBClient("testFindWithMaxTimeMS", async (db) => { typeof e.ok !== "undefined" && assertEquals(e.ok, 0); typeof e.codeName !== "undefined" && assertEquals(e.codeName, "MaxTimeMSExpired"); - assertEquals(e.errmsg, "operation exceeded time limit"); + typeof e.errmsg !== "undefined" && + assertEquals(e.errmsg, "operation exceeded time limit"); + typeof e.errmsg === "undefined" && + assert(e.toString().includes("operation exceeded time limit")); } await db.collection("mongo_test_users").drop(); From f6b5b4fca6765f1bc800e1f0c7a8f3f743bdff37 Mon Sep 17 00:00:00 2001 From: Bruno Bernardino Date: Fri, 1 Apr 2022 16:46:58 +0100 Subject: [PATCH 08/14] Debug in CI. --- tests/cases/03_curd.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/cases/03_curd.ts b/tests/cases/03_curd.ts index 7b38e8e..b6e46b1 100644 --- a/tests/cases/03_curd.ts +++ b/tests/cases/03_curd.ts @@ -613,7 +613,7 @@ testWithTestDBClient("testFindWithMaxTimeMS", async (db) => { typeof e.errmsg !== "undefined" && assertEquals(e.errmsg, "operation exceeded time limit"); typeof e.errmsg === "undefined" && - assert(e.toString().includes("operation exceeded time limit")); + console.log(e.toString()); } try { @@ -629,7 +629,7 @@ testWithTestDBClient("testFindWithMaxTimeMS", async (db) => { typeof e.errmsg !== "undefined" && assertEquals(e.errmsg, "operation exceeded time limit"); typeof e.errmsg === "undefined" && - assert(e.toString().includes("operation exceeded time limit")); + console.log(e.toString()); } await db.collection("mongo_test_users").drop(); From ec53b7729228c94ae9bfd8dd9054ca5b91cb504e Mon Sep 17 00:00:00 2001 From: Bruno Bernardino Date: Fri, 1 Apr 2022 17:04:35 +0100 Subject: [PATCH 09/14] Improve version recognition, keep some debug --- tests/cases/03_curd.ts | 34 ++++++++++++++++++++++------------ tests/test.deps.ts | 1 + 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/tests/cases/03_curd.ts b/tests/cases/03_curd.ts index b6e46b1..13c24ec 100644 --- a/tests/cases/03_curd.ts +++ b/tests/cases/03_curd.ts @@ -1,6 +1,12 @@ import { MongoInvalidArgumentError } from "../../src/error.ts"; import { testWithClient, testWithTestDBClient } from "../common.ts"; -import { assert, assertEquals, assertRejects } from "../test.deps.ts"; +import { + assert, + assertEquals, + AssertionError, + assertRejects, + semver, +} from "../test.deps.ts"; interface IUser { username?: string; @@ -579,7 +585,11 @@ testWithTestDBClient("testFindEmptyAsyncIteration", async (db) => { await db.collection("mongo_test_users").drop(); }); -testWithTestDBClient("testFindWithMaxTimeMS", async (db) => { +testWithClient("testFindWithMaxTimeMS", async (client) => { + const db = client.database("test"); + + const supportsMaxTimeMS = semver.gte(client.buildInfo!.version, "4.2.0"); + const users = db.collection("mongo_test_users"); for (let i = 0; i < 10; i++) { await users.insertOne({ @@ -603,33 +613,33 @@ testWithTestDBClient("testFindWithMaxTimeMS", async (db) => { try { await users.find({ uid: 0, - $where: "sleep(5) || true", + $where: "sleep(10) || true", }, { maxTimeMS: 1 }).toArray(); assert(false); } catch (e) { - typeof e.ok !== "undefined" && assertEquals(e.ok, 0); - typeof e.codeName !== "undefined" && + if (supportsMaxTimeMS) { assertEquals(e.codeName, "MaxTimeMSExpired"); - typeof e.errmsg !== "undefined" && assertEquals(e.errmsg, "operation exceeded time limit"); - typeof e.errmsg === "undefined" && + } else { console.log(e.toString()); + assert(e instanceof AssertionError); + } } try { await users.findOne({ uid: 0, - $where: "sleep(5) || true", + $where: "sleep(10) || true", }, { maxTimeMS: 1 }); assert(false); } catch (e) { - typeof e.ok !== "undefined" && assertEquals(e.ok, 0); - typeof e.codeName !== "undefined" && + if (supportsMaxTimeMS) { assertEquals(e.codeName, "MaxTimeMSExpired"); - typeof e.errmsg !== "undefined" && assertEquals(e.errmsg, "operation exceeded time limit"); - typeof e.errmsg === "undefined" && + } else { console.log(e.toString()); + assert(e instanceof AssertionError); + } } await db.collection("mongo_test_users").drop(); diff --git a/tests/test.deps.ts b/tests/test.deps.ts index a7cb37f..c7c2f38 100644 --- a/tests/test.deps.ts +++ b/tests/test.deps.ts @@ -1,6 +1,7 @@ export { assert, assertEquals, + AssertionError, assertNotEquals, assertRejects, assertThrows, From 1cce8594593a07d746a7037023f2a2f8157c4169 Mon Sep 17 00:00:00 2001 From: Bruno Bernardino Date: Fri, 1 Apr 2022 17:06:46 +0100 Subject: [PATCH 10/14] Fixes tests in 4.0 as well, remove debugging --- tests/cases/03_curd.ts | 34 ++++++++-------------------------- tests/test.deps.ts | 1 - 2 files changed, 8 insertions(+), 27 deletions(-) diff --git a/tests/cases/03_curd.ts b/tests/cases/03_curd.ts index 13c24ec..407f99c 100644 --- a/tests/cases/03_curd.ts +++ b/tests/cases/03_curd.ts @@ -1,12 +1,6 @@ import { MongoInvalidArgumentError } from "../../src/error.ts"; import { testWithClient, testWithTestDBClient } from "../common.ts"; -import { - assert, - assertEquals, - AssertionError, - assertRejects, - semver, -} from "../test.deps.ts"; +import { assert, assertEquals, assertRejects } from "../test.deps.ts"; interface IUser { username?: string; @@ -585,11 +579,7 @@ testWithTestDBClient("testFindEmptyAsyncIteration", async (db) => { await db.collection("mongo_test_users").drop(); }); -testWithClient("testFindWithMaxTimeMS", async (client) => { - const db = client.database("test"); - - const supportsMaxTimeMS = semver.gte(client.buildInfo!.version, "4.2.0"); - +testWithTestDBClient("testFindWithMaxTimeMS", async (db) => { const users = db.collection("mongo_test_users"); for (let i = 0; i < 10; i++) { await users.insertOne({ @@ -617,13 +607,9 @@ testWithClient("testFindWithMaxTimeMS", async (client) => { }, { maxTimeMS: 1 }).toArray(); assert(false); } catch (e) { - if (supportsMaxTimeMS) { - assertEquals(e.codeName, "MaxTimeMSExpired"); - assertEquals(e.errmsg, "operation exceeded time limit"); - } else { - console.log(e.toString()); - assert(e instanceof AssertionError); - } + assertEquals(e.ok, 0); + assertEquals(e.codeName, "MaxTimeMSExpired"); + assertEquals(e.errmsg, "operation exceeded time limit"); } try { @@ -633,13 +619,9 @@ testWithClient("testFindWithMaxTimeMS", async (client) => { }, { maxTimeMS: 1 }); assert(false); } catch (e) { - if (supportsMaxTimeMS) { - assertEquals(e.codeName, "MaxTimeMSExpired"); - assertEquals(e.errmsg, "operation exceeded time limit"); - } else { - console.log(e.toString()); - assert(e instanceof AssertionError); - } + assertEquals(e.ok, 0); + assertEquals(e.codeName, "MaxTimeMSExpired"); + assertEquals(e.errmsg, "operation exceeded time limit"); } await db.collection("mongo_test_users").drop(); diff --git a/tests/test.deps.ts b/tests/test.deps.ts index c7c2f38..a7cb37f 100644 --- a/tests/test.deps.ts +++ b/tests/test.deps.ts @@ -1,7 +1,6 @@ export { assert, assertEquals, - AssertionError, assertNotEquals, assertRejects, assertThrows, From 24f8edf52ff30043d8d6a4101364d76ea2839b84 Mon Sep 17 00:00:00 2001 From: Bruno Bernardino Date: Fri, 1 Apr 2022 17:09:57 +0100 Subject: [PATCH 11/14] Tweak time, so 4.0 fails with the maxTimeMS error. --- tests/cases/03_curd.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/cases/03_curd.ts b/tests/cases/03_curd.ts index 407f99c..c955e2b 100644 --- a/tests/cases/03_curd.ts +++ b/tests/cases/03_curd.ts @@ -603,7 +603,7 @@ testWithTestDBClient("testFindWithMaxTimeMS", async (db) => { try { await users.find({ uid: 0, - $where: "sleep(10) || true", + $where: "sleep(50) || true", }, { maxTimeMS: 1 }).toArray(); assert(false); } catch (e) { @@ -615,7 +615,7 @@ testWithTestDBClient("testFindWithMaxTimeMS", async (db) => { try { await users.findOne({ uid: 0, - $where: "sleep(10) || true", + $where: "sleep(50) || true", }, { maxTimeMS: 1 }); assert(false); } catch (e) { From 6e1ab31f050e6b33c16ca0fa6929bae1d33caaf7 Mon Sep 17 00:00:00 2001 From: Bruno Bernardino Date: Fri, 1 Apr 2022 17:13:46 +0100 Subject: [PATCH 12/14] Try theory that findOne doesn't fail in 4.0 --- tests/cases/03_curd.ts | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/tests/cases/03_curd.ts b/tests/cases/03_curd.ts index c955e2b..93a804c 100644 --- a/tests/cases/03_curd.ts +++ b/tests/cases/03_curd.ts @@ -603,7 +603,7 @@ testWithTestDBClient("testFindWithMaxTimeMS", async (db) => { try { await users.find({ uid: 0, - $where: "sleep(50) || true", + $where: "sleep(10) || true", }, { maxTimeMS: 1 }).toArray(); assert(false); } catch (e) { @@ -612,17 +612,17 @@ testWithTestDBClient("testFindWithMaxTimeMS", async (db) => { assertEquals(e.errmsg, "operation exceeded time limit"); } - try { - await users.findOne({ - uid: 0, - $where: "sleep(50) || true", - }, { maxTimeMS: 1 }); - assert(false); - } catch (e) { - assertEquals(e.ok, 0); - assertEquals(e.codeName, "MaxTimeMSExpired"); - assertEquals(e.errmsg, "operation exceeded time limit"); - } + // 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(); }); From a89c04df3c9581e10a25fb66ccd902352328deb0 Mon Sep 17 00:00:00 2001 From: Bruno Bernardino Date: Fri, 1 Apr 2022 17:29:13 +0100 Subject: [PATCH 13/14] Hopefully now it's all tested for different versions as supported --- tests/cases/03_curd.ts | 41 ++++++++++++++++++++++++++++------------- tests/test.deps.ts | 1 + 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/tests/cases/03_curd.ts b/tests/cases/03_curd.ts index 93a804c..b466022 100644 --- a/tests/cases/03_curd.ts +++ b/tests/cases/03_curd.ts @@ -1,6 +1,12 @@ import { MongoInvalidArgumentError } from "../../src/error.ts"; import { testWithClient, testWithTestDBClient } from "../common.ts"; -import { assert, assertEquals, assertRejects } from "../test.deps.ts"; +import { + assert, + assertEquals, + AssertionError, + assertRejects, + semver, +} from "../test.deps.ts"; interface IUser { username?: string; @@ -579,7 +585,14 @@ testWithTestDBClient("testFindEmptyAsyncIteration", async (db) => { await db.collection("mongo_test_users").drop(); }); -testWithTestDBClient("testFindWithMaxTimeMS", async (db) => { +testWithClient("testFindWithMaxTimeMS", async (client) => { + const db = client.database("local"); + + const supportsMaxTimeMSInFindOne = semver.gte( + client.buildInfo!.version, + "4.2.0", + ); + const users = db.collection("mongo_test_users"); for (let i = 0; i < 10; i++) { await users.insertOne({ @@ -612,17 +625,19 @@ testWithTestDBClient("testFindWithMaxTimeMS", async (db) => { assertEquals(e.errmsg, "operation exceeded time limit"); } - // 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"); - // } + 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(); }); diff --git a/tests/test.deps.ts b/tests/test.deps.ts index a7cb37f..c7c2f38 100644 --- a/tests/test.deps.ts +++ b/tests/test.deps.ts @@ -1,6 +1,7 @@ export { assert, assertEquals, + AssertionError, assertNotEquals, assertRejects, assertThrows, From c764fbe8b9582b44ce5108b5315c5ba67f7092fb Mon Sep 17 00:00:00 2001 From: Bruno Bernardino Date: Fri, 1 Apr 2022 17:30:33 +0100 Subject: [PATCH 14/14] Remove unused variable --- tests/cases/03_curd.ts | 8 +------- tests/test.deps.ts | 1 - 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/tests/cases/03_curd.ts b/tests/cases/03_curd.ts index b466022..3297eae 100644 --- a/tests/cases/03_curd.ts +++ b/tests/cases/03_curd.ts @@ -1,12 +1,6 @@ import { MongoInvalidArgumentError } from "../../src/error.ts"; import { testWithClient, testWithTestDBClient } from "../common.ts"; -import { - assert, - assertEquals, - AssertionError, - assertRejects, - semver, -} from "../test.deps.ts"; +import { assert, assertEquals, assertRejects, semver } from "../test.deps.ts"; interface IUser { username?: string; diff --git a/tests/test.deps.ts b/tests/test.deps.ts index c7c2f38..a7cb37f 100644 --- a/tests/test.deps.ts +++ b/tests/test.deps.ts @@ -1,7 +1,6 @@ export { assert, assertEquals, - AssertionError, assertNotEquals, assertRejects, assertThrows,