2021-01-11 12:13:41 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2020-03-03 12:22:53 -05:00
|
|
|
import {
|
|
|
|
assert,
|
|
|
|
assertEquals,
|
2020-03-28 13:03:49 -04:00
|
|
|
assertNotEquals,
|
2020-11-26 11:22:36 -05:00
|
|
|
deferred,
|
2020-09-27 06:22:32 -04:00
|
|
|
unitTest,
|
2020-03-03 12:22:53 -05:00
|
|
|
} from "./test_util.ts";
|
2018-09-16 16:35:16 -04:00
|
|
|
|
2021-01-06 08:53:30 -05:00
|
|
|
function waitForMs(ms: number): Promise<void> {
|
2020-11-23 17:31:10 -05:00
|
|
|
return new Promise((resolve): number => setTimeout(resolve, ms));
|
2018-09-16 16:35:16 -04:00
|
|
|
}
|
|
|
|
|
2021-01-06 08:53:30 -05:00
|
|
|
unitTest(async function functionParameterBindingSuccess(): Promise<void> {
|
|
|
|
const promise = deferred();
|
|
|
|
let count = 0;
|
|
|
|
|
|
|
|
const nullProto = (newCount: number): void => {
|
|
|
|
count = newCount;
|
|
|
|
promise.resolve();
|
|
|
|
};
|
|
|
|
|
|
|
|
Reflect.setPrototypeOf(nullProto, null);
|
|
|
|
|
|
|
|
setTimeout(nullProto, 500, 1);
|
|
|
|
await promise;
|
|
|
|
// count should be reassigned
|
|
|
|
assertEquals(count, 1);
|
|
|
|
});
|
|
|
|
|
|
|
|
unitTest(async function stringifyAndEvalNonFunctions(): Promise<void> {
|
|
|
|
// eval can only access global scope
|
|
|
|
const global = globalThis as unknown as {
|
|
|
|
globalPromise: ReturnType<typeof deferred>;
|
|
|
|
globalCount: number;
|
|
|
|
};
|
|
|
|
global.globalPromise = deferred();
|
|
|
|
global.globalCount = 0;
|
|
|
|
|
|
|
|
const notAFunction =
|
|
|
|
"globalThis.globalCount++; globalThis.globalPromise.resolve();" as unknown as () =>
|
|
|
|
void;
|
|
|
|
|
|
|
|
setTimeout(notAFunction, 500);
|
|
|
|
|
|
|
|
await global.globalPromise;
|
|
|
|
|
|
|
|
// count should be incremented
|
|
|
|
assertEquals(global.globalCount, 1);
|
|
|
|
|
|
|
|
Reflect.deleteProperty(global, "globalPromise");
|
|
|
|
Reflect.deleteProperty(global, "globalCount");
|
|
|
|
});
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(async function timeoutSuccess(): Promise<void> {
|
2020-11-26 11:22:36 -05:00
|
|
|
const promise = deferred();
|
2018-09-16 16:35:16 -04:00
|
|
|
let count = 0;
|
2019-04-21 16:40:10 -04:00
|
|
|
setTimeout((): void => {
|
2018-09-16 16:35:16 -04:00
|
|
|
count++;
|
2020-09-25 12:34:20 -04:00
|
|
|
promise.resolve();
|
2018-09-16 16:35:16 -04:00
|
|
|
}, 500);
|
|
|
|
await promise;
|
|
|
|
// count should increment
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(count, 1);
|
2018-09-16 16:35:16 -04:00
|
|
|
});
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(async function timeoutArgs(): Promise<void> {
|
2020-11-26 11:22:36 -05:00
|
|
|
const promise = deferred();
|
2018-10-05 07:29:55 -04:00
|
|
|
const arg = 1;
|
2018-10-08 02:58:44 -04:00
|
|
|
setTimeout(
|
2019-04-21 16:40:10 -04:00
|
|
|
(a, b, c): void => {
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(a, arg);
|
|
|
|
assertEquals(b, arg.toString());
|
|
|
|
assertEquals(c, [arg]);
|
2020-09-25 12:34:20 -04:00
|
|
|
promise.resolve();
|
2018-10-08 02:58:44 -04:00
|
|
|
},
|
|
|
|
10,
|
|
|
|
arg,
|
|
|
|
arg.toString(),
|
2020-07-14 15:24:17 -04:00
|
|
|
[arg],
|
2018-10-08 02:58:44 -04:00
|
|
|
);
|
|
|
|
await promise;
|
2018-09-05 01:35:29 -04:00
|
|
|
});
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(async function timeoutCancelSuccess(): Promise<void> {
|
2018-09-16 16:35:16 -04:00
|
|
|
let count = 0;
|
2019-04-21 16:40:10 -04:00
|
|
|
const id = setTimeout((): void => {
|
2018-09-16 16:35:16 -04:00
|
|
|
count++;
|
2019-04-21 14:06:57 -04:00
|
|
|
}, 1);
|
2018-09-16 16:35:16 -04:00
|
|
|
// Cancelled, count should not increment
|
|
|
|
clearTimeout(id);
|
|
|
|
await waitForMs(600);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(count, 0);
|
2018-09-16 16:35:16 -04:00
|
|
|
});
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(async function timeoutCancelMultiple(): Promise<void> {
|
2019-03-09 12:30:38 -05:00
|
|
|
function uncalled(): never {
|
|
|
|
throw new Error("This function should not be called.");
|
|
|
|
}
|
|
|
|
|
2018-10-08 13:52:16 -04:00
|
|
|
// Set timers and cancel them in the same order.
|
|
|
|
const t1 = setTimeout(uncalled, 10);
|
|
|
|
const t2 = setTimeout(uncalled, 10);
|
|
|
|
const t3 = setTimeout(uncalled, 10);
|
|
|
|
clearTimeout(t1);
|
|
|
|
clearTimeout(t2);
|
|
|
|
clearTimeout(t3);
|
|
|
|
|
|
|
|
// Set timers and cancel them in reverse order.
|
|
|
|
const t4 = setTimeout(uncalled, 20);
|
|
|
|
const t5 = setTimeout(uncalled, 20);
|
|
|
|
const t6 = setTimeout(uncalled, 20);
|
|
|
|
clearTimeout(t6);
|
|
|
|
clearTimeout(t5);
|
|
|
|
clearTimeout(t4);
|
|
|
|
|
|
|
|
// Sleep until we're certain that the cancelled timers aren't gonna fire.
|
|
|
|
await waitForMs(50);
|
|
|
|
});
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(async function timeoutCancelInvalidSilentFail(): Promise<void> {
|
2018-09-16 16:35:16 -04:00
|
|
|
// Expect no panic
|
2020-11-26 11:22:36 -05:00
|
|
|
const promise = deferred();
|
2018-09-16 16:35:16 -04:00
|
|
|
let count = 0;
|
2021-04-09 17:35:29 -04:00
|
|
|
// deno-lint-ignore no-unused-vars
|
2019-04-21 16:40:10 -04:00
|
|
|
const id = setTimeout((): void => {
|
2018-09-16 16:35:16 -04:00
|
|
|
count++;
|
|
|
|
// Should have no effect
|
|
|
|
clearTimeout(id);
|
2020-09-25 12:34:20 -04:00
|
|
|
promise.resolve();
|
2018-09-16 16:35:16 -04:00
|
|
|
}, 500);
|
|
|
|
await promise;
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(count, 1);
|
2018-09-16 16:35:16 -04:00
|
|
|
|
|
|
|
// Should silently fail (no panic)
|
|
|
|
clearTimeout(2147483647);
|
|
|
|
});
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(async function intervalSuccess(): Promise<void> {
|
2020-11-26 11:22:36 -05:00
|
|
|
const promise = deferred();
|
2018-09-16 16:35:16 -04:00
|
|
|
let count = 0;
|
2019-04-21 16:40:10 -04:00
|
|
|
const id = setInterval((): void => {
|
2018-09-16 16:35:16 -04:00
|
|
|
count++;
|
2019-04-21 14:06:57 -04:00
|
|
|
clearInterval(id);
|
2020-09-25 12:34:20 -04:00
|
|
|
promise.resolve();
|
2019-04-21 14:06:57 -04:00
|
|
|
}, 100);
|
2018-09-16 16:35:16 -04:00
|
|
|
await promise;
|
|
|
|
// Clear interval
|
|
|
|
clearInterval(id);
|
|
|
|
// count should increment twice
|
2019-04-21 14:06:57 -04:00
|
|
|
assertEquals(count, 1);
|
2020-03-19 10:45:28 -04:00
|
|
|
// Similar false async leaking alarm.
|
|
|
|
// Force next round of polling.
|
|
|
|
await waitForMs(0);
|
2018-09-16 16:35:16 -04:00
|
|
|
});
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(async function intervalCancelSuccess(): Promise<void> {
|
2018-09-16 16:35:16 -04:00
|
|
|
let count = 0;
|
2019-04-21 16:40:10 -04:00
|
|
|
const id = setInterval((): void => {
|
2018-09-16 16:35:16 -04:00
|
|
|
count++;
|
2019-04-17 13:02:32 -04:00
|
|
|
}, 1);
|
2018-09-16 16:35:16 -04:00
|
|
|
clearInterval(id);
|
2019-04-17 13:02:32 -04:00
|
|
|
await waitForMs(500);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(count, 0);
|
2018-09-16 16:35:16 -04:00
|
|
|
});
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(async function intervalOrdering(): Promise<void> {
|
2020-02-19 15:36:18 -05:00
|
|
|
const timers: number[] = [];
|
2018-09-05 01:35:29 -04:00
|
|
|
let timeouts = 0;
|
2019-03-09 12:30:38 -05:00
|
|
|
function onTimeout(): void {
|
2018-09-05 01:35:29 -04:00
|
|
|
++timeouts;
|
|
|
|
for (let i = 1; i < timers.length; i++) {
|
|
|
|
clearTimeout(timers[i]);
|
|
|
|
}
|
|
|
|
}
|
2019-03-09 12:30:38 -05:00
|
|
|
for (let i = 0; i < 10; i++) {
|
2019-04-17 13:02:32 -04:00
|
|
|
timers[i] = setTimeout(onTimeout, 1);
|
2019-03-09 12:30:38 -05:00
|
|
|
}
|
2019-04-17 13:02:32 -04:00
|
|
|
await waitForMs(500);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(timeouts, 1);
|
2018-09-05 01:35:29 -04:00
|
|
|
});
|
|
|
|
|
2020-03-20 09:38:34 -04:00
|
|
|
unitTest(function intervalCancelInvalidSilentFail(): void {
|
2018-09-16 16:35:16 -04:00
|
|
|
// Should silently fail (no panic)
|
|
|
|
clearInterval(2147483647);
|
|
|
|
});
|
2019-01-26 16:10:38 -05:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(async function fireCallbackImmediatelyWhenDelayOverMaxValue(): Promise<
|
2019-04-21 16:40:10 -04:00
|
|
|
void
|
|
|
|
> {
|
2019-01-26 16:10:38 -05:00
|
|
|
let count = 0;
|
2019-04-21 16:40:10 -04:00
|
|
|
setTimeout((): void => {
|
2019-01-26 16:10:38 -05:00
|
|
|
count++;
|
|
|
|
}, 2 ** 31);
|
|
|
|
await waitForMs(1);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(count, 1);
|
2019-01-26 16:10:38 -05:00
|
|
|
});
|
2019-06-11 03:50:36 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(async function timeoutCallbackThis(): Promise<void> {
|
2020-11-26 11:22:36 -05:00
|
|
|
const promise = deferred();
|
2019-06-11 03:50:36 -04:00
|
|
|
const obj = {
|
|
|
|
foo(): void {
|
|
|
|
assertEquals(this, window);
|
2020-09-25 12:34:20 -04:00
|
|
|
promise.resolve();
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2019-06-11 03:50:36 -04:00
|
|
|
};
|
|
|
|
setTimeout(obj.foo, 1);
|
|
|
|
await promise;
|
|
|
|
});
|
2019-06-13 11:08:27 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(async function timeoutBindThis(): Promise<void> {
|
2019-06-13 11:08:27 -04:00
|
|
|
const thisCheckPassed = [null, undefined, window, globalThis];
|
|
|
|
|
|
|
|
const thisCheckFailed = [
|
|
|
|
0,
|
|
|
|
"",
|
|
|
|
true,
|
|
|
|
false,
|
|
|
|
{},
|
|
|
|
[],
|
|
|
|
"foo",
|
|
|
|
(): void => {},
|
2020-03-28 13:03:49 -04:00
|
|
|
Object.prototype,
|
2019-06-13 11:08:27 -04:00
|
|
|
];
|
|
|
|
|
2020-03-03 12:22:53 -05:00
|
|
|
for (const thisArg of thisCheckPassed) {
|
2020-11-26 11:22:36 -05:00
|
|
|
const resolvable = deferred();
|
2020-03-03 12:22:53 -05:00
|
|
|
let hasThrown = 0;
|
|
|
|
try {
|
|
|
|
setTimeout.call(thisArg, () => resolvable.resolve(), 1);
|
|
|
|
hasThrown = 1;
|
|
|
|
} catch (err) {
|
|
|
|
if (err instanceof TypeError) {
|
|
|
|
hasThrown = 2;
|
|
|
|
} else {
|
|
|
|
hasThrown = 3;
|
2019-06-13 11:08:27 -04:00
|
|
|
}
|
|
|
|
}
|
2020-03-03 12:22:53 -05:00
|
|
|
await resolvable;
|
|
|
|
assertEquals(hasThrown, 1);
|
|
|
|
}
|
2019-06-13 11:08:27 -04:00
|
|
|
|
2020-03-03 12:22:53 -05:00
|
|
|
for (const thisArg of thisCheckFailed) {
|
|
|
|
let hasThrown = 0;
|
|
|
|
try {
|
|
|
|
setTimeout.call(thisArg, () => {}, 1);
|
|
|
|
hasThrown = 1;
|
|
|
|
} catch (err) {
|
|
|
|
if (err instanceof TypeError) {
|
|
|
|
hasThrown = 2;
|
|
|
|
} else {
|
|
|
|
hasThrown = 3;
|
2019-06-13 11:08:27 -04:00
|
|
|
}
|
|
|
|
}
|
2020-03-03 12:22:53 -05:00
|
|
|
assertEquals(hasThrown, 2);
|
|
|
|
}
|
2019-06-13 11:08:27 -04:00
|
|
|
});
|
2019-06-17 13:42:20 -04:00
|
|
|
|
2020-03-20 09:38:34 -04:00
|
|
|
unitTest(function clearTimeoutShouldConvertToNumber(): void {
|
2019-06-17 13:42:20 -04:00
|
|
|
let called = false;
|
|
|
|
const obj = {
|
|
|
|
valueOf(): number {
|
|
|
|
called = true;
|
|
|
|
return 1;
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2019-06-17 13:42:20 -04:00
|
|
|
};
|
|
|
|
clearTimeout((obj as unknown) as number);
|
|
|
|
assert(called);
|
|
|
|
});
|
2019-06-18 09:24:20 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(function setTimeoutShouldThrowWithBigint(): void {
|
2019-08-29 10:57:09 -04:00
|
|
|
let hasThrown = 0;
|
|
|
|
try {
|
|
|
|
setTimeout((): void => {}, (1n as unknown) as number);
|
|
|
|
hasThrown = 1;
|
|
|
|
} catch (err) {
|
|
|
|
if (err instanceof TypeError) {
|
|
|
|
hasThrown = 2;
|
|
|
|
} else {
|
|
|
|
hasThrown = 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assertEquals(hasThrown, 2);
|
|
|
|
});
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(function clearTimeoutShouldThrowWithBigint(): void {
|
2019-08-30 11:51:53 -04:00
|
|
|
let hasThrown = 0;
|
|
|
|
try {
|
|
|
|
clearTimeout((1n as unknown) as number);
|
|
|
|
hasThrown = 1;
|
|
|
|
} catch (err) {
|
|
|
|
if (err instanceof TypeError) {
|
|
|
|
hasThrown = 2;
|
|
|
|
} else {
|
|
|
|
hasThrown = 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assertEquals(hasThrown, 2);
|
|
|
|
});
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(function testFunctionName(): void {
|
2019-06-18 09:24:20 -04:00
|
|
|
assertEquals(clearTimeout.name, "clearTimeout");
|
|
|
|
assertEquals(clearInterval.name, "clearInterval");
|
|
|
|
});
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(function testFunctionParamsLength(): void {
|
2019-07-18 06:09:32 -04:00
|
|
|
assertEquals(setTimeout.length, 1);
|
|
|
|
assertEquals(setInterval.length, 1);
|
|
|
|
assertEquals(clearTimeout.length, 0);
|
|
|
|
assertEquals(clearInterval.length, 0);
|
|
|
|
});
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(function clearTimeoutAndClearIntervalNotBeEquals(): void {
|
2019-06-18 09:24:20 -04:00
|
|
|
assertNotEquals(clearTimeout, clearInterval);
|
|
|
|
});
|
2019-10-19 17:09:24 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(async function timerMaxCpuBug(): Promise<void> {
|
2019-10-19 17:09:24 -04:00
|
|
|
// There was a bug where clearing a timeout would cause Deno to use 100% CPU.
|
|
|
|
clearTimeout(setTimeout(() => {}, 1000));
|
|
|
|
// We can check this by counting how many ops have triggered in the interim.
|
|
|
|
// Certainly less than 10 ops should have been dispatched in next 100 ms.
|
|
|
|
const { opsDispatched } = Deno.metrics();
|
|
|
|
await waitForMs(100);
|
|
|
|
const opsDispatched_ = Deno.metrics().opsDispatched;
|
|
|
|
assert(opsDispatched_ - opsDispatched < 10);
|
|
|
|
});
|
2019-12-03 22:19:03 -05:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(async function timerBasicMicrotaskOrdering(): Promise<void> {
|
2019-12-03 22:19:03 -05:00
|
|
|
let s = "";
|
|
|
|
let count = 0;
|
2020-11-26 11:22:36 -05:00
|
|
|
const promise = deferred();
|
2019-12-03 22:19:03 -05:00
|
|
|
setTimeout(() => {
|
|
|
|
Promise.resolve().then(() => {
|
|
|
|
count++;
|
|
|
|
s += "de";
|
|
|
|
if (count === 2) {
|
2020-09-25 12:34:20 -04:00
|
|
|
promise.resolve();
|
2019-12-03 22:19:03 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
setTimeout(() => {
|
|
|
|
count++;
|
|
|
|
s += "no";
|
|
|
|
if (count === 2) {
|
2020-09-25 12:34:20 -04:00
|
|
|
promise.resolve();
|
2019-12-03 22:19:03 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
await promise;
|
|
|
|
assertEquals(s, "deno");
|
|
|
|
});
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(async function timerNestedMicrotaskOrdering(): Promise<void> {
|
2019-12-03 22:19:03 -05:00
|
|
|
let s = "";
|
2020-11-26 11:22:36 -05:00
|
|
|
const promise = deferred();
|
2019-12-03 22:19:03 -05:00
|
|
|
s += "0";
|
|
|
|
setTimeout(() => {
|
|
|
|
s += "4";
|
2020-03-19 10:45:28 -04:00
|
|
|
setTimeout(() => (s += "A"));
|
|
|
|
Promise.resolve()
|
|
|
|
.then(() => {
|
|
|
|
setTimeout(() => {
|
|
|
|
s += "B";
|
2020-09-25 12:34:20 -04:00
|
|
|
promise.resolve();
|
2020-03-19 10:45:28 -04:00
|
|
|
});
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
s += "5";
|
2019-12-03 22:19:03 -05:00
|
|
|
});
|
|
|
|
});
|
2020-03-19 10:45:28 -04:00
|
|
|
setTimeout(() => (s += "6"));
|
2019-12-03 22:19:03 -05:00
|
|
|
Promise.resolve().then(() => (s += "2"));
|
|
|
|
Promise.resolve().then(() =>
|
|
|
|
setTimeout(() => {
|
2020-03-19 10:45:28 -04:00
|
|
|
s += "7";
|
|
|
|
Promise.resolve()
|
|
|
|
.then(() => (s += "8"))
|
|
|
|
.then(() => {
|
|
|
|
s += "9";
|
|
|
|
});
|
2019-12-03 22:19:03 -05:00
|
|
|
})
|
|
|
|
);
|
|
|
|
Promise.resolve().then(() => Promise.resolve().then(() => (s += "3")));
|
|
|
|
s += "1";
|
|
|
|
await promise;
|
2020-03-19 10:45:28 -04:00
|
|
|
assertEquals(s, "0123456789AB");
|
2019-12-03 22:19:03 -05:00
|
|
|
});
|
2020-03-24 13:39:41 -04:00
|
|
|
|
|
|
|
unitTest(function testQueueMicrotask() {
|
|
|
|
assertEquals(typeof queueMicrotask, "function");
|
|
|
|
});
|
2020-07-04 15:16:27 -04:00
|
|
|
|
|
|
|
unitTest(async function timerIgnoresDateOverride(): Promise<void> {
|
|
|
|
const OriginalDate = Date;
|
2020-11-26 11:22:36 -05:00
|
|
|
const promise = deferred();
|
2020-07-04 15:16:27 -04:00
|
|
|
let hasThrown = 0;
|
|
|
|
try {
|
|
|
|
const overrideCalled: () => number = () => {
|
2020-09-25 12:34:20 -04:00
|
|
|
promise.reject("global Date override used over original Date object");
|
2020-07-04 15:16:27 -04:00
|
|
|
return 0;
|
|
|
|
};
|
2020-08-31 14:11:17 -04:00
|
|
|
const DateOverride = (): void => {
|
2020-07-04 15:16:27 -04:00
|
|
|
overrideCalled();
|
2020-08-31 14:11:17 -04:00
|
|
|
};
|
2020-07-04 15:16:27 -04:00
|
|
|
globalThis.Date = DateOverride as DateConstructor;
|
|
|
|
globalThis.Date.now = overrideCalled;
|
|
|
|
globalThis.Date.UTC = overrideCalled;
|
|
|
|
globalThis.Date.parse = overrideCalled;
|
2020-09-25 12:34:20 -04:00
|
|
|
queueMicrotask(promise.resolve);
|
2020-07-04 15:16:27 -04:00
|
|
|
await promise;
|
|
|
|
hasThrown = 1;
|
|
|
|
} catch (err) {
|
|
|
|
if (typeof err === "string") {
|
|
|
|
assertEquals(err, "global Date override used over original Date object");
|
|
|
|
hasThrown = 2;
|
|
|
|
} else if (err instanceof TypeError) {
|
|
|
|
hasThrown = 3;
|
|
|
|
} else {
|
|
|
|
hasThrown = 4;
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
globalThis.Date = OriginalDate;
|
|
|
|
}
|
|
|
|
assertEquals(hasThrown, 1);
|
|
|
|
});
|
2020-10-15 21:06:31 -04:00
|
|
|
|
|
|
|
unitTest({ perms: { hrtime: true } }, function sleepSync(): void {
|
|
|
|
const start = performance.now();
|
|
|
|
Deno.sleepSync(10);
|
|
|
|
const after = performance.now();
|
|
|
|
assert(after - start >= 10);
|
|
|
|
});
|
|
|
|
|
|
|
|
unitTest(
|
|
|
|
{ perms: { hrtime: true } },
|
|
|
|
async function sleepSyncShorterPromise(): Promise<void> {
|
|
|
|
const perf = performance;
|
|
|
|
const short = 5;
|
|
|
|
const long = 10;
|
|
|
|
|
|
|
|
const start = perf.now();
|
|
|
|
const p = sleepAsync(short).then(() => {
|
|
|
|
const after = perf.now();
|
|
|
|
// pending promises should resolve after the main thread comes out of sleep
|
|
|
|
assert(after - start >= long);
|
|
|
|
});
|
|
|
|
Deno.sleepSync(long);
|
|
|
|
|
|
|
|
await p;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
unitTest(
|
|
|
|
{ perms: { hrtime: true } },
|
|
|
|
async function sleepSyncLongerPromise(): Promise<void> {
|
|
|
|
const perf = performance;
|
|
|
|
const short = 5;
|
|
|
|
const long = 10;
|
|
|
|
|
|
|
|
const start = perf.now();
|
|
|
|
const p = sleepAsync(long).then(() => {
|
|
|
|
const after = perf.now();
|
|
|
|
// sleeping for less than the duration of a promise should have no impact
|
|
|
|
// on the resolution of that promise
|
|
|
|
assert(after - start >= long);
|
|
|
|
});
|
|
|
|
Deno.sleepSync(short);
|
|
|
|
|
|
|
|
await p;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
function sleepAsync(delay: number): Promise<void> {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
setTimeout(() => resolve(), delay);
|
|
|
|
});
|
|
|
|
}
|