mirror of
https://github.com/denoland/deno.git
synced 2025-01-11 00:21:05 -05:00
refactor: clean timers tests (#7679)
This commit is contained in:
parent
f1231ff58e
commit
b6c189e88e
1 changed files with 18 additions and 41 deletions
|
@ -7,39 +7,16 @@ import {
|
||||||
assertNotEquals,
|
assertNotEquals,
|
||||||
} from "./test_util.ts";
|
} from "./test_util.ts";
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
||||||
type AnyRecord = Record<any, any>;
|
|
||||||
|
|
||||||
function deferred(): {
|
|
||||||
promise: Promise<AnyRecord>;
|
|
||||||
resolve: (value?: AnyRecord | PromiseLike<AnyRecord>) => void;
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
||||||
reject: (reason?: any) => void;
|
|
||||||
} {
|
|
||||||
let resolve: (value?: AnyRecord | PromiseLike<AnyRecord>) => void;
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
||||||
let reject: ((reason?: any) => void) | undefined = undefined;
|
|
||||||
const promise = new Promise<AnyRecord>((res, rej): void => {
|
|
||||||
resolve = res;
|
|
||||||
reject = rej;
|
|
||||||
});
|
|
||||||
return {
|
|
||||||
promise,
|
|
||||||
resolve: resolve!,
|
|
||||||
reject: reject!,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function waitForMs(ms: number): Promise<number> {
|
function waitForMs(ms: number): Promise<number> {
|
||||||
return new Promise((resolve: () => void): number => setTimeout(resolve, ms));
|
return new Promise((resolve: () => void): number => setTimeout(resolve, ms));
|
||||||
}
|
}
|
||||||
|
|
||||||
unitTest(async function timeoutSuccess(): Promise<void> {
|
unitTest(async function timeoutSuccess(): Promise<void> {
|
||||||
const { promise, resolve } = deferred();
|
const promise = createResolvable();
|
||||||
let count = 0;
|
let count = 0;
|
||||||
setTimeout((): void => {
|
setTimeout((): void => {
|
||||||
count++;
|
count++;
|
||||||
resolve();
|
promise.resolve();
|
||||||
}, 500);
|
}, 500);
|
||||||
await promise;
|
await promise;
|
||||||
// count should increment
|
// count should increment
|
||||||
|
@ -47,14 +24,14 @@ unitTest(async function timeoutSuccess(): Promise<void> {
|
||||||
});
|
});
|
||||||
|
|
||||||
unitTest(async function timeoutArgs(): Promise<void> {
|
unitTest(async function timeoutArgs(): Promise<void> {
|
||||||
const { promise, resolve } = deferred();
|
const promise = createResolvable();
|
||||||
const arg = 1;
|
const arg = 1;
|
||||||
setTimeout(
|
setTimeout(
|
||||||
(a, b, c): void => {
|
(a, b, c): void => {
|
||||||
assertEquals(a, arg);
|
assertEquals(a, arg);
|
||||||
assertEquals(b, arg.toString());
|
assertEquals(b, arg.toString());
|
||||||
assertEquals(c, [arg]);
|
assertEquals(c, [arg]);
|
||||||
resolve();
|
promise.resolve();
|
||||||
},
|
},
|
||||||
10,
|
10,
|
||||||
arg,
|
arg,
|
||||||
|
@ -102,13 +79,13 @@ unitTest(async function timeoutCancelMultiple(): Promise<void> {
|
||||||
|
|
||||||
unitTest(async function timeoutCancelInvalidSilentFail(): Promise<void> {
|
unitTest(async function timeoutCancelInvalidSilentFail(): Promise<void> {
|
||||||
// Expect no panic
|
// Expect no panic
|
||||||
const { promise, resolve } = deferred();
|
const promise = createResolvable();
|
||||||
let count = 0;
|
let count = 0;
|
||||||
const id = setTimeout((): void => {
|
const id = setTimeout((): void => {
|
||||||
count++;
|
count++;
|
||||||
// Should have no effect
|
// Should have no effect
|
||||||
clearTimeout(id);
|
clearTimeout(id);
|
||||||
resolve();
|
promise.resolve();
|
||||||
}, 500);
|
}, 500);
|
||||||
await promise;
|
await promise;
|
||||||
assertEquals(count, 1);
|
assertEquals(count, 1);
|
||||||
|
@ -118,12 +95,12 @@ unitTest(async function timeoutCancelInvalidSilentFail(): Promise<void> {
|
||||||
});
|
});
|
||||||
|
|
||||||
unitTest(async function intervalSuccess(): Promise<void> {
|
unitTest(async function intervalSuccess(): Promise<void> {
|
||||||
const { promise, resolve } = deferred();
|
const promise = createResolvable();
|
||||||
let count = 0;
|
let count = 0;
|
||||||
const id = setInterval((): void => {
|
const id = setInterval((): void => {
|
||||||
count++;
|
count++;
|
||||||
clearInterval(id);
|
clearInterval(id);
|
||||||
resolve();
|
promise.resolve();
|
||||||
}, 100);
|
}, 100);
|
||||||
await promise;
|
await promise;
|
||||||
// Clear interval
|
// Clear interval
|
||||||
|
@ -178,11 +155,11 @@ unitTest(async function fireCallbackImmediatelyWhenDelayOverMaxValue(): Promise<
|
||||||
});
|
});
|
||||||
|
|
||||||
unitTest(async function timeoutCallbackThis(): Promise<void> {
|
unitTest(async function timeoutCallbackThis(): Promise<void> {
|
||||||
const { promise, resolve } = deferred();
|
const promise = createResolvable();
|
||||||
const obj = {
|
const obj = {
|
||||||
foo(): void {
|
foo(): void {
|
||||||
assertEquals(this, window);
|
assertEquals(this, window);
|
||||||
resolve();
|
promise.resolve();
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
setTimeout(obj.foo, 1);
|
setTimeout(obj.foo, 1);
|
||||||
|
@ -309,13 +286,13 @@ unitTest(async function timerMaxCpuBug(): Promise<void> {
|
||||||
unitTest(async function timerBasicMicrotaskOrdering(): Promise<void> {
|
unitTest(async function timerBasicMicrotaskOrdering(): Promise<void> {
|
||||||
let s = "";
|
let s = "";
|
||||||
let count = 0;
|
let count = 0;
|
||||||
const { promise, resolve } = deferred();
|
const promise = createResolvable();
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
Promise.resolve().then(() => {
|
Promise.resolve().then(() => {
|
||||||
count++;
|
count++;
|
||||||
s += "de";
|
s += "de";
|
||||||
if (count === 2) {
|
if (count === 2) {
|
||||||
resolve();
|
promise.resolve();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -323,7 +300,7 @@ unitTest(async function timerBasicMicrotaskOrdering(): Promise<void> {
|
||||||
count++;
|
count++;
|
||||||
s += "no";
|
s += "no";
|
||||||
if (count === 2) {
|
if (count === 2) {
|
||||||
resolve();
|
promise.resolve();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
await promise;
|
await promise;
|
||||||
|
@ -332,7 +309,7 @@ unitTest(async function timerBasicMicrotaskOrdering(): Promise<void> {
|
||||||
|
|
||||||
unitTest(async function timerNestedMicrotaskOrdering(): Promise<void> {
|
unitTest(async function timerNestedMicrotaskOrdering(): Promise<void> {
|
||||||
let s = "";
|
let s = "";
|
||||||
const { promise, resolve } = deferred();
|
const promise = createResolvable();
|
||||||
s += "0";
|
s += "0";
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
s += "4";
|
s += "4";
|
||||||
|
@ -341,7 +318,7 @@ unitTest(async function timerNestedMicrotaskOrdering(): Promise<void> {
|
||||||
.then(() => {
|
.then(() => {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
s += "B";
|
s += "B";
|
||||||
resolve();
|
promise.resolve();
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
.then(() => {
|
.then(() => {
|
||||||
|
@ -372,11 +349,11 @@ unitTest(function testQueueMicrotask() {
|
||||||
|
|
||||||
unitTest(async function timerIgnoresDateOverride(): Promise<void> {
|
unitTest(async function timerIgnoresDateOverride(): Promise<void> {
|
||||||
const OriginalDate = Date;
|
const OriginalDate = Date;
|
||||||
const { promise, resolve, reject } = deferred();
|
const promise = createResolvable();
|
||||||
let hasThrown = 0;
|
let hasThrown = 0;
|
||||||
try {
|
try {
|
||||||
const overrideCalled: () => number = () => {
|
const overrideCalled: () => number = () => {
|
||||||
reject("global Date override used over original Date object");
|
promise.reject("global Date override used over original Date object");
|
||||||
return 0;
|
return 0;
|
||||||
};
|
};
|
||||||
const DateOverride = (): void => {
|
const DateOverride = (): void => {
|
||||||
|
@ -386,7 +363,7 @@ unitTest(async function timerIgnoresDateOverride(): Promise<void> {
|
||||||
globalThis.Date.now = overrideCalled;
|
globalThis.Date.now = overrideCalled;
|
||||||
globalThis.Date.UTC = overrideCalled;
|
globalThis.Date.UTC = overrideCalled;
|
||||||
globalThis.Date.parse = overrideCalled;
|
globalThis.Date.parse = overrideCalled;
|
||||||
queueMicrotask(resolve);
|
queueMicrotask(promise.resolve);
|
||||||
await promise;
|
await promise;
|
||||||
hasThrown = 1;
|
hasThrown = 1;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|
Loading…
Reference in a new issue