Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
curtisman committed Oct 7, 2022
1 parent fb05cc5 commit e1dd06a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 16 deletions.
3 changes: 1 addition & 2 deletions packages/test/mocha-test-setup/src/mochaHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,6 @@ export const mochaHooks = {
// Also, error in this code count as error of the test, instead of part of the hook, which
// would avoid rest of tests in the suite being skipped because of the hook error.

let newTestFunction: Mocha.TestFunction | undefined;

type BeforeTestFunc<T = any> = (this: Mocha.Context) => T;
type AfterTestFunc<T = any> = (this: Mocha.Context, beforeTestResult: T) => void;

Expand Down Expand Up @@ -152,6 +150,7 @@ function getWrappedFunction(fn: Mocha.Func | Mocha.AsyncFunc) {
};
}

let newTestFunction: Mocha.TestFunction | undefined;
function setupCustomTestHooks() {
const currentTestFunction = globalThis.it;
// the function `it` is reassign per test files. Trap it.
Expand Down
43 changes: 36 additions & 7 deletions packages/test/test-utils/src/test/timeoutUtils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,74 +4,103 @@
*/

import { strict as assert } from "assert";
import { timeoutPromise, defaultTimeoutDurationMs } from "../timeoutUtils";

let setTestEndTime;
let clearTestEndTime;
globalThis.registerMochaTestWrapperFuncs = function(a, b) {
setTestEndTime = a;
clearTestEndTime = b;
};

import { timeoutPromise } from "../timeoutUtils";

describe("TimeoutPromise", () => {
it("Timeout with no options", async () => {
const id = setTestEndTime.call({ timeout: () => 50 });
try {
await timeoutPromise(() => {});
assert(false, "should have timed out");
} catch (e: any) {
assert(e.message.startsWith("Timed out"), "expected timeout error message");
assert(e.message.startsWith("Timed out ("), "expected timeout error message");
}
clearTestEndTime(id);
});

it("Timeout with no duration", async () => {
const id = setTestEndTime.call({ timeout: () => 50 });
try {
await timeoutPromise(() => { }, {});
assert(false, "should have timed out");
} catch (e: any) {
assert(e.message.startsWith("Timed out"), "expected timeout error message");
assert(e.message.startsWith("Timed out ("), "expected timeout error message");
}
clearTestEndTime(id);
});

it("Timeout with duration", async () => {
const id = setTestEndTime.call({ timeout: () => 50 });
try {
await timeoutPromise(() => { }, { durationMs: 1 });
assert(false, "should have timed out");
} catch {

}
clearTestEndTime(id);
});

it("Timeout with zero duration", async () => {
const id = setTestEndTime.call({ timeout: () => 50 });
try {
await timeoutPromise((resolve) => {
setTimeout(resolve, defaultTimeoutDurationMs + 50);
setTimeout(resolve, 100);
}, { durationMs: 0 });
} catch {
assert(false, "should not have timed out");
}
clearTestEndTime(id);
});

it("Timeout with negative duration", async () => {
const id = setTestEndTime.call({ timeout: () => 50 });
try {
await timeoutPromise((resolve) => {
setTimeout(resolve, defaultTimeoutDurationMs + 50);
setTimeout(resolve, 100);
}, { durationMs: -1 });
} catch {
assert(false, "should not have timed out");
}
clearTestEndTime(id);
});

it("Timeout with Infinity duration", async () => {
const id = setTestEndTime.call({ timeout: () => 50 });
try {
await timeoutPromise((resolve) => {
setTimeout(resolve, defaultTimeoutDurationMs + 50);
setTimeout(resolve, 100);
}, { durationMs: Infinity });
} catch {
assert(false, "should not have timed out");
}
clearTestEndTime(id);
});

it("no timeout", async () => {
it("No timeout with valid duration", async () => {
try {
await timeoutPromise((resolve) => { setTimeout(resolve, 1); }, { durationMs: 100 });
} catch {
assert(false, "should not have timed out");
}
});

it("Timeout with valid duration", async () => {
try {
await timeoutPromise((resolve) => { setTimeout(resolve, 100); }, { durationMs: 50 });
assert(false, "should have timed out");
} catch {

}
});

it("Timeout with no reject option", async () => {
try {
const value = await timeoutPromise(() => {}, {
Expand Down
12 changes: 5 additions & 7 deletions packages/test/test-utils/src/timeoutUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,17 @@
import { Container } from "@fluidframework/container-loader";
import { assert } from "@fluidframework/common-utils";

// @deprecated this value is no longer used
export const defaultTimeoutDurationMs = 250;

// Patch mocha so we can timeout promises based on how much time is left in the test.
let currentTestEndTimeId = 0;
let currentTestEndTime = 0; // 0 means it is not set, -1 means timeout is disabled, otherwise, it is the test end time
let currentTestEndTime = 0; // 0 means it is no , otherwise, it is the test end time
const timeBuffer = 30; // leave 30ms leeway for finish processing

function getCurrentTestTimeout() {
if (currentTestEndTime === -1) {
return -1;
}
if (currentTestEndTime === 0) {
return defaultTimeoutDurationMs;
return 0;
}
// Even if we are passed our timeout, return 1ms so that we will still wait
return Math.max(currentTestEndTime - Date.now(), 1);
Expand All @@ -28,9 +26,9 @@ function setTestEndTime(this: Mocha.Context) {
const timeout = this.timeout();
const now = Date.now();
// Either the test timed out (so the test end time is less then now, or the promise resolved)
assert(currentTestEndTime < now && currentTestEndTime !== -1, "Unexpected nested tests detected");
assert(currentTestEndTime < now, "Unexpected nested tests detected");
const hasTimeout = Number.isFinite(timeout) && timeout > 0;
currentTestEndTime = hasTimeout ? now + timeout - timeBuffer : -1;
currentTestEndTime = hasTimeout ? now + timeout - timeBuffer : 0;
return ++currentTestEndTimeId;
}

Expand Down

0 comments on commit e1dd06a

Please sign in to comment.