timers: add some more unit tests

Closes #682.
This commit is contained in:
Li Hao 2018-09-05 13:35:29 +08:00 committed by Bert Belder
parent aa691ea26c
commit 99e2c42d16
No known key found for this signature in database
GPG key ID: 7A77887B2E2ED461

View file

@ -1,3 +1,4 @@
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
import { test, assertEqual } from "./test_util.ts";
function deferred() {
@ -30,6 +31,28 @@ test(async function timeoutSuccess() {
assertEqual(count, 1);
});
test(async function timeoutArgs() {
let arg = 1;
await new Promise((resolve, reject) => {
setTimeout(
(a, b, c) => {
try {
assertEqual(a, arg);
assertEqual(b, arg.toString());
assertEqual(c, [arg]);
resolve();
} catch (e) {
reject(e);
}
},
10,
arg,
arg.toString(),
[arg]
);
});
});
test(async function timeoutCancelSuccess() {
let count = 0;
const id = setTimeout(() => {
@ -91,6 +114,30 @@ test(async function intervalCancelSuccess() {
assertEqual(count, 0);
});
test(async function intervalCancelSuccess2() {
const timers = [];
let timeouts = 0;
for (let i = 0; i < 5; i++) {
timers[i] = setTimeout(onTimeout, 20 * i);
}
function onTimeout() {
++timeouts;
for (let i = 1; i < timers.length; i++) {
clearTimeout(timers[i]);
}
}
await new Promise((resolve, reject) => {
setTimeout(() => {
try {
assertEqual(timeouts, 1);
resolve();
} catch (e) {
reject(e);
}
}, 200);
});
});
test(async function intervalCancelInvalidSilentFail() {
// Should silently fail (no panic)
clearInterval(2147483647);