diff options
author | Yoshiya Hinosawa <stibium121@gmail.com> | 2021-06-25 10:44:14 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-06-25 03:44:14 +0200 |
commit | 66c5f41c5bd5fa08daa09f8650135000c1787829 (patch) | |
tree | 05ad5900822d01316ad0c193cd7fcc7953bdf32c /cli/tests/unit/timers_test.ts | |
parent | dd4ed825762d32ca9f791bda3bb9c459886c7061 (diff) |
test(cli): refactor the usages of delay (#11098)
This PR refactors the usages of delay utility in js unit testing. The same
utiliy is defined in several places with different names. This PR replaces those
usages with the one provided in std/async/delay.ts to improve the readability
and consistency of test code.
Diffstat (limited to 'cli/tests/unit/timers_test.ts')
-rw-r--r-- | cli/tests/unit/timers_test.ts | 29 |
1 files changed, 10 insertions, 19 deletions
diff --git a/cli/tests/unit/timers_test.ts b/cli/tests/unit/timers_test.ts index 7e974d060..cb5f541d7 100644 --- a/cli/tests/unit/timers_test.ts +++ b/cli/tests/unit/timers_test.ts @@ -5,13 +5,10 @@ import { assertNotEquals, Deferred, deferred, + delay, unitTest, } from "./test_util.ts"; -function waitForMs(ms: number): Promise<void> { - return new Promise((resolve): number => setTimeout(resolve, ms)); -} - unitTest(async function functionParameterBindingSuccess(): Promise<void> { const promise = deferred(); let count = 0; @@ -111,7 +108,7 @@ unitTest(async function timeoutCancelSuccess(): Promise<void> { }, 1); // Cancelled, count should not increment clearTimeout(id); - await waitForMs(600); + await delay(600); assertEquals(count, 0); }); @@ -137,7 +134,7 @@ unitTest(async function timeoutCancelMultiple(): Promise<void> { clearTimeout(t4); // Sleep until we're certain that the cancelled timers aren't gonna fire. - await waitForMs(50); + await delay(50); }); unitTest(async function timeoutCancelInvalidSilentFail(): Promise<void> { @@ -172,7 +169,7 @@ unitTest(async function intervalSuccess(): Promise<void> { assertEquals(count, 1); // Similar false async leaking alarm. // Force next round of polling. - await waitForMs(0); + await delay(0); }); unitTest(async function intervalCancelSuccess(): Promise<void> { @@ -181,7 +178,7 @@ unitTest(async function intervalCancelSuccess(): Promise<void> { count++; }, 1); clearInterval(id); - await waitForMs(500); + await delay(500); assertEquals(count, 0); }); @@ -197,7 +194,7 @@ unitTest(async function intervalOrdering(): Promise<void> { for (let i = 0; i < 10; i++) { timers[i] = setTimeout(onTimeout, 1); } - await waitForMs(500); + await delay(500); assertEquals(timeouts, 1); }); @@ -213,7 +210,7 @@ unitTest(async function fireCallbackImmediatelyWhenDelayOverMaxValue(): Promise< setTimeout((): void => { count++; }, 2 ** 31); - await waitForMs(1); + await delay(1); assertEquals(count, 1); }); @@ -341,7 +338,7 @@ unitTest(async function timerMaxCpuBug(): Promise<void> { // 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); + await delay(100); const opsDispatched_ = Deno.metrics().opsDispatched; assert(opsDispatched_ - opsDispatched < 10); }); @@ -459,7 +456,7 @@ unitTest( const long = 10; const start = perf.now(); - const p = sleepAsync(short).then(() => { + const p = delay(short).then(() => { const after = perf.now(); // pending promises should resolve after the main thread comes out of sleep assert(after - start >= long); @@ -478,7 +475,7 @@ unitTest( const long = 10; const start = perf.now(); - const p = sleepAsync(long).then(() => { + const p = delay(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 @@ -489,9 +486,3 @@ unitTest( await p; }, ); - -function sleepAsync(delay: number): Promise<void> { - return new Promise((resolve) => { - setTimeout(() => resolve(), delay); - }); -} |