0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 17:34:47 -05:00

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.
This commit is contained in:
Yoshiya Hinosawa 2021-06-25 10:44:14 +09:00 committed by GitHub
parent dd4ed82576
commit 66c5f41c5b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 17 additions and 32 deletions

View file

@ -8,6 +8,7 @@ import {
assertEquals, assertEquals,
assertThrowsAsync, assertThrowsAsync,
deferred, deferred,
delay,
unitTest, unitTest,
} from "./test_util.ts"; } from "./test_util.ts";
@ -375,8 +376,6 @@ unitTest(
unitTest( unitTest(
{ perms: { net: true } }, { perms: { net: true } },
async function httpServerNextRequestResolvesOnClose() { async function httpServerNextRequestResolvesOnClose() {
const delay = (n: number) =>
new Promise((resolve) => setTimeout(resolve, n));
const httpConnList: Deno.HttpConn[] = []; const httpConnList: Deno.HttpConn[] = [];
async function serve(l: Deno.Listener) { async function serve(l: Deno.Listener) {

View file

@ -6,6 +6,7 @@ import {
assertThrows, assertThrows,
assertThrowsAsync, assertThrowsAsync,
deferred, deferred,
delay,
unitTest, unitTest,
} from "./test_util.ts"; } from "./test_util.ts";
@ -409,9 +410,7 @@ unitTest(
const listener = Deno.listen(addr); const listener = Deno.listen(addr);
iterate(listener); iterate(listener);
await new Promise<void>((resolve) => { await delay(100);
setTimeout(resolve, 100);
});
const conn1 = await Deno.connect(addr); const conn1 = await Deno.connect(addr);
conn1.close(); conn1.close();
const conn2 = await Deno.connect(addr); const conn2 = await Deno.connect(addr);

View file

@ -4,15 +4,10 @@ import {
assertEquals, assertEquals,
assertThrows, assertThrows,
deferred, deferred,
delay,
unitTest, unitTest,
} from "./test_util.ts"; } from "./test_util.ts";
function defer(n: number): Promise<void> {
return new Promise((resolve: () => void, _) => {
setTimeout(resolve, n);
});
}
unitTest( unitTest(
{ ignore: Deno.build.os !== "windows" }, { ignore: Deno.build.os !== "windows" },
function signalsNotImplemented(): void { function signalsNotImplemented(): void {
@ -113,11 +108,11 @@ unitTest(
let c = 0; let c = 0;
const sig = Deno.signal(Deno.Signal.SIGUSR1); const sig = Deno.signal(Deno.Signal.SIGUSR1);
setTimeout(async () => { setTimeout(async () => {
await defer(20); await delay(20);
for (const _ of Array(3)) { for (const _ of Array(3)) {
// Sends SIGUSR1 3 times. // Sends SIGUSR1 3 times.
Deno.kill(Deno.pid, Deno.Signal.SIGUSR1); Deno.kill(Deno.pid, Deno.Signal.SIGUSR1);
await defer(20); await delay(20);
} }
sig.dispose(); sig.dispose();
resolvable.resolve(); resolvable.resolve();

View file

@ -22,6 +22,7 @@ export {
} from "../../../test_util/std/testing/asserts.ts"; } from "../../../test_util/std/testing/asserts.ts";
export { deferred } from "../../../test_util/std/async/deferred.ts"; export { deferred } from "../../../test_util/std/async/deferred.ts";
export type { Deferred } from "../../../test_util/std/async/deferred.ts"; export type { Deferred } from "../../../test_util/std/async/deferred.ts";
export { delay } from "../../../test_util/std/async/delay.ts";
export { readLines } from "../../../test_util/std/io/bufio.ts"; export { readLines } from "../../../test_util/std/io/bufio.ts";
export { parse as parseArgs } from "../../../test_util/std/flags/mod.ts"; export { parse as parseArgs } from "../../../test_util/std/flags/mod.ts";

View file

@ -5,13 +5,10 @@ import {
assertNotEquals, assertNotEquals,
Deferred, Deferred,
deferred, deferred,
delay,
unitTest, unitTest,
} from "./test_util.ts"; } from "./test_util.ts";
function waitForMs(ms: number): Promise<void> {
return new Promise((resolve): number => setTimeout(resolve, ms));
}
unitTest(async function functionParameterBindingSuccess(): Promise<void> { unitTest(async function functionParameterBindingSuccess(): Promise<void> {
const promise = deferred(); const promise = deferred();
let count = 0; let count = 0;
@ -111,7 +108,7 @@ unitTest(async function timeoutCancelSuccess(): Promise<void> {
}, 1); }, 1);
// Cancelled, count should not increment // Cancelled, count should not increment
clearTimeout(id); clearTimeout(id);
await waitForMs(600); await delay(600);
assertEquals(count, 0); assertEquals(count, 0);
}); });
@ -137,7 +134,7 @@ unitTest(async function timeoutCancelMultiple(): Promise<void> {
clearTimeout(t4); clearTimeout(t4);
// Sleep until we're certain that the cancelled timers aren't gonna fire. // 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> { unitTest(async function timeoutCancelInvalidSilentFail(): Promise<void> {
@ -172,7 +169,7 @@ unitTest(async function intervalSuccess(): Promise<void> {
assertEquals(count, 1); assertEquals(count, 1);
// Similar false async leaking alarm. // Similar false async leaking alarm.
// Force next round of polling. // Force next round of polling.
await waitForMs(0); await delay(0);
}); });
unitTest(async function intervalCancelSuccess(): Promise<void> { unitTest(async function intervalCancelSuccess(): Promise<void> {
@ -181,7 +178,7 @@ unitTest(async function intervalCancelSuccess(): Promise<void> {
count++; count++;
}, 1); }, 1);
clearInterval(id); clearInterval(id);
await waitForMs(500); await delay(500);
assertEquals(count, 0); assertEquals(count, 0);
}); });
@ -197,7 +194,7 @@ unitTest(async function intervalOrdering(): Promise<void> {
for (let i = 0; i < 10; i++) { for (let i = 0; i < 10; i++) {
timers[i] = setTimeout(onTimeout, 1); timers[i] = setTimeout(onTimeout, 1);
} }
await waitForMs(500); await delay(500);
assertEquals(timeouts, 1); assertEquals(timeouts, 1);
}); });
@ -213,7 +210,7 @@ unitTest(async function fireCallbackImmediatelyWhenDelayOverMaxValue(): Promise<
setTimeout((): void => { setTimeout((): void => {
count++; count++;
}, 2 ** 31); }, 2 ** 31);
await waitForMs(1); await delay(1);
assertEquals(count, 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. // 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. // Certainly less than 10 ops should have been dispatched in next 100 ms.
const { opsDispatched } = Deno.metrics(); const { opsDispatched } = Deno.metrics();
await waitForMs(100); await delay(100);
const opsDispatched_ = Deno.metrics().opsDispatched; const opsDispatched_ = Deno.metrics().opsDispatched;
assert(opsDispatched_ - opsDispatched < 10); assert(opsDispatched_ - opsDispatched < 10);
}); });
@ -459,7 +456,7 @@ unitTest(
const long = 10; const long = 10;
const start = perf.now(); const start = perf.now();
const p = sleepAsync(short).then(() => { const p = delay(short).then(() => {
const after = perf.now(); const after = perf.now();
// pending promises should resolve after the main thread comes out of sleep // pending promises should resolve after the main thread comes out of sleep
assert(after - start >= long); assert(after - start >= long);
@ -478,7 +475,7 @@ unitTest(
const long = 10; const long = 10;
const start = perf.now(); const start = perf.now();
const p = sleepAsync(long).then(() => { const p = delay(long).then(() => {
const after = perf.now(); const after = perf.now();
// sleeping for less than the duration of a promise should have no impact // sleeping for less than the duration of a promise should have no impact
// on the resolution of that promise // on the resolution of that promise
@ -489,9 +486,3 @@ unitTest(
await p; await p;
}, },
); );
function sleepAsync(delay: number): Promise<void> {
return new Promise((resolve) => {
setTimeout(() => resolve(), delay);
});
}