diff options
author | Matt Mastracci <matthew@mastracci.com> | 2024-03-04 19:45:51 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-05 08:15:51 +0530 |
commit | c99a0492b7c02462e4f7e97904fc068b1ee26129 (patch) | |
tree | e6cee5de8697d9b7dcdd540f8b4a3a4c65911802 /tests/unit/timers_test.ts | |
parent | d9fa2dd5505a773ac2c8eb6ca2a8bbd237aca35d (diff) |
chore(tests): update flaky timer test (#22701)
This test was flaky after landing the new timer rewrite.
Diffstat (limited to 'tests/unit/timers_test.ts')
-rw-r--r-- | tests/unit/timers_test.ts | 32 |
1 files changed, 19 insertions, 13 deletions
diff --git a/tests/unit/timers_test.ts b/tests/unit/timers_test.ts index 9e3b082dd..0b2a66e6e 100644 --- a/tests/unit/timers_test.ts +++ b/tests/unit/timers_test.ts @@ -228,24 +228,30 @@ Deno.test(function intervalCancelInvalidSilentFail() { clearInterval(2147483647); }); +// If a repeating timer is dispatched, the next interval that should first is based on +// when the timer is dispatched, not when the timer handler completes. Deno.test(async function callbackTakesLongerThanInterval() { const { promise, resolve } = Promise.withResolvers<void>(); - - let timeEndOfFirstCallback: number | undefined; - const interval = setInterval(() => { - if (timeEndOfFirstCallback === undefined) { - // First callback - Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 300); - timeEndOfFirstCallback = Date.now(); - } else { - // Second callback should be nearly instantaneous - assert(Date.now() - timeEndOfFirstCallback < 10); - clearInterval(interval); - resolve(); + const output: number[] = []; + let last = 0; + const id = setInterval(() => { + const now = performance.now(); + if (last > 0) { + output.push(now - last); + if (output.length >= 10) { + resolve(); + clearTimeout(id); + } + } + last = now; + while (performance.now() - now < 300) { + /* hot loop */ } }, 100); - await promise; + const total = output.reduce((t, n) => t + n, 0) / output.length; + console.log(output); + assert(total < 350 && total > 299, "Total was out of range: " + total); }); // https://github.com/denoland/deno/issues/11398 |