2019-01-21 14:03:30 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2019-03-06 20:48:46 -05:00
|
|
|
import { test, assertEquals } from "./test_util.ts";
|
2018-09-16 16:35:16 -04:00
|
|
|
|
2019-03-09 12:30:38 -05:00
|
|
|
function deferred(): {
|
|
|
|
promise: Promise<{}>;
|
|
|
|
resolve: (value?: {} | PromiseLike<{}>) => void;
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
reject: (reason?: any) => void;
|
|
|
|
} {
|
2018-09-16 16:35:16 -04:00
|
|
|
let resolve;
|
|
|
|
let reject;
|
2019-04-21 16:40:10 -04:00
|
|
|
const promise = new Promise(
|
|
|
|
(res, rej): void => {
|
|
|
|
resolve = res;
|
|
|
|
reject = rej;
|
|
|
|
}
|
|
|
|
);
|
2018-09-16 16:35:16 -04:00
|
|
|
return {
|
|
|
|
promise,
|
|
|
|
resolve,
|
|
|
|
reject
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-03-09 12:30:38 -05:00
|
|
|
async function waitForMs(ms): Promise<number> {
|
2019-04-21 16:40:10 -04:00
|
|
|
return new Promise((resolve): number => setTimeout(resolve, ms));
|
2018-09-16 16:35:16 -04:00
|
|
|
}
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
test(async function timeoutSuccess(): Promise<void> {
|
2018-09-16 16:35:16 -04:00
|
|
|
const { promise, resolve } = deferred();
|
|
|
|
let count = 0;
|
2019-04-21 16:40:10 -04:00
|
|
|
setTimeout((): void => {
|
2018-09-16 16:35:16 -04:00
|
|
|
count++;
|
|
|
|
resolve();
|
|
|
|
}, 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
|
|
|
});
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
test(async function timeoutArgs(): Promise<void> {
|
2018-10-08 02:58:44 -04:00
|
|
|
const { promise, resolve } = 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]);
|
2018-10-08 02:58:44 -04:00
|
|
|
resolve();
|
|
|
|
},
|
|
|
|
10,
|
|
|
|
arg,
|
|
|
|
arg.toString(),
|
|
|
|
[arg]
|
|
|
|
);
|
|
|
|
await promise;
|
2018-09-05 01:35:29 -04:00
|
|
|
});
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
test(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
|
|
|
});
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
test(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);
|
|
|
|
});
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
test(async function timeoutCancelInvalidSilentFail(): Promise<void> {
|
2018-09-16 16:35:16 -04:00
|
|
|
// Expect no panic
|
|
|
|
const { promise, resolve } = deferred();
|
|
|
|
let count = 0;
|
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);
|
|
|
|
resolve();
|
|
|
|
}, 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);
|
|
|
|
});
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
test(async function intervalSuccess(): Promise<void> {
|
2018-09-16 16:35:16 -04:00
|
|
|
const { promise, resolve } = deferred();
|
|
|
|
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);
|
|
|
|
resolve();
|
|
|
|
}, 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);
|
2018-09-16 16:35:16 -04:00
|
|
|
});
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
test(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
|
|
|
});
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
test(async function intervalOrdering(): Promise<void> {
|
2018-09-05 01:35:29 -04:00
|
|
|
const timers = [];
|
|
|
|
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
|
|
|
});
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
test(async function intervalCancelInvalidSilentFail(): Promise<void> {
|
2018-09-16 16:35:16 -04:00
|
|
|
// Should silently fail (no panic)
|
|
|
|
clearInterval(2147483647);
|
|
|
|
});
|
2019-01-26 16:10:38 -05:00
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
test(async function fireCallbackImmediatelyWhenDelayOverMaxValue(): Promise<
|
|
|
|
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
|
|
|
|
|
|
|
test(async function timeoutCallbackThis(): Promise<void> {
|
|
|
|
const { promise, resolve } = deferred();
|
|
|
|
const obj = {
|
|
|
|
foo(): void {
|
|
|
|
assertEquals(this, window);
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
setTimeout(obj.foo, 1);
|
|
|
|
await promise;
|
|
|
|
});
|