diff options
Diffstat (limited to 'cli/tests/unit/timers_test.ts')
-rw-r--r-- | cli/tests/unit/timers_test.ts | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/cli/tests/unit/timers_test.ts b/cli/tests/unit/timers_test.ts index 35a21297f..fc6f9af4d 100644 --- a/cli/tests/unit/timers_test.ts +++ b/cli/tests/unit/timers_test.ts @@ -6,6 +6,7 @@ import { Deferred, deferred, delay, + unreachable, } from "./test_util.ts"; Deno.test(async function functionParameterBindingSuccess() { @@ -206,6 +207,60 @@ Deno.test(function intervalCancelInvalidSilentFail() { clearInterval(2147483647); }); +Deno.test(async function callbackTakesLongerThanInterval() { + const promise = deferred(); + + let timeEndOfFirstCallback: number | undefined; + const interval = setInterval(() => { + if (timeEndOfFirstCallback === undefined) { + // First callback + Deno.sleepSync(300); + timeEndOfFirstCallback = Date.now(); + } else { + // Second callback + assert(Date.now() - 100 >= timeEndOfFirstCallback); + clearInterval(interval); + promise.resolve(); + } + }, 100); + + await promise; +}); + +// https://github.com/denoland/deno/issues/11398 +Deno.test(async function clearTimeoutAfterNextTimerIsDue1() { + const promise = deferred(); + + setTimeout(() => { + promise.resolve(); + }, 300); + + const interval = setInterval(() => { + Deno.sleepSync(400); + // Both the interval and the timeout's due times are now in the past. + clearInterval(interval); + }, 100); + + await promise; +}); + +// https://github.com/denoland/deno/issues/11398 +Deno.test(async function clearTimeoutAfterNextTimerIsDue2() { + const promise = deferred(); + + const timeout1 = setTimeout(unreachable, 100); + + setTimeout(() => { + promise.resolve(); + }, 200); + + Deno.sleepSync(300); + // Both of the timeouts' due times are now in the past. + clearTimeout(timeout1); + + await promise; +}); + Deno.test(async function fireCallbackImmediatelyWhenDelayOverMaxValue() { let count = 0; setTimeout(() => { @@ -346,6 +401,35 @@ Deno.test(async function timerMaxCpuBug() { assert(opsDispatched_ - opsDispatched < 10); }); +Deno.test(async function timerOrdering() { + const array: number[] = []; + const donePromise = deferred(); + + function push(n: number) { + array.push(n); + if (array.length === 6) { + donePromise.resolve(); + } + } + + setTimeout(() => { + push(1); + setTimeout(() => push(4)); + }, 0); + setTimeout(() => { + push(2); + setTimeout(() => push(5)); + }, 0); + setTimeout(() => { + push(3); + setTimeout(() => push(6)); + }, 0); + + await donePromise; + + assertEquals(array, [1, 2, 3, 4, 5, 6]); +}); + Deno.test(async function timerBasicMicrotaskOrdering() { let s = ""; let count = 0; |