deno.land / std@0.224.0 / async / delay_test.ts
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.import { delay } from "./delay.ts";import { assert, assertEquals, assertInstanceOf, assertRejects, assertStrictEquals,} from "../assert/mod.ts";import { assertSpyCalls, stub } from "../testing/mock.ts";
// https://dom.spec.whatwg.org/#interface-AbortSignalfunction assertIsDefaultAbortReason(reason: unknown) { assertInstanceOf(reason, DOMException); assertStrictEquals(reason.name, "AbortError");}
Deno.test("delay()", async () => { const start = new Date(); const delayedPromise = delay(100); const result = await delayedPromise; const diff = new Date().getTime() - start.getTime(); assert(result === undefined); assert(diff >= 100);});
Deno.test("delay() handles abort", async () => { const start = new Date(); const abort = new AbortController(); const { signal } = abort; const delayedPromise = delay(100, { signal }); setTimeout(() => abort.abort(), 0); const cause = await assertRejects(() => delayedPromise); const diff = new Date().getTime() - start.getTime(); assert(diff < 100); assertIsDefaultAbortReason(cause);});
Deno.test("delay() checks abort reason", async (ctx) => { async function assertRejectsReason(reason: unknown) { const start = new Date(); const abort = new AbortController(); const { signal } = abort; const delayedPromise = delay(100, { signal }); setTimeout(() => abort.abort(reason), 0); const cause = await assertRejects(() => delayedPromise); const diff = new Date().getTime() - start.getTime(); assert(diff < 100); assertStrictEquals(cause, reason); }
await ctx.step("not-undefined values", async () => { await Promise.all([ null, new Error("Timeout cancelled"), new DOMException("Timeout cancelled", "AbortError"), new DOMException("The signal has been aborted", "AbortError"), ].map(assertRejectsReason)); });
await ctx.step("undefined", async () => { const start = new Date(); const abort = new AbortController(); const { signal } = abort; const delayedPromise = delay(100, { signal }); setTimeout(() => abort.abort(), 0); const cause = await assertRejects(() => delayedPromise); const diff = new Date().getTime() - start.getTime(); assert(diff < 100); assertIsDefaultAbortReason(cause); });});
Deno.test("delay() handles non-aborted signal", async () => { const start = new Date(); const abort = new AbortController(); const { signal } = abort; const delayedPromise = delay(100, { signal }); const result = await delayedPromise; const diff = new Date().getTime() - start.getTime(); assert(result === undefined); assert(diff >= 100);});
Deno.test("delay() handles aborted signal after delay", async () => { const start = new Date(); const abort = new AbortController(); const { signal } = abort; const delayedPromise = delay(100, { signal }); const result = await delayedPromise; abort.abort(); const diff = new Date().getTime() - start.getTime(); assert(result === undefined); assert(diff >= 100);});
Deno.test("delay() handles already aborted signal", async () => { const start = new Date(); const abort = new AbortController(); abort.abort(); const { signal } = abort; const delayedPromise = delay(100, { signal }); const cause = await assertRejects(() => delayedPromise); const diff = new Date().getTime() - start.getTime(); assert(diff < 100); assertIsDefaultAbortReason(cause);});
Deno.test("delay() handles persitent option", async () => { using unrefTimer = stub(Deno, "unrefTimer"); await delay(100, { persistent: false }); assertSpyCalls(unrefTimer, 1);});
Deno.test("delay() handles persistent option with reference error", async () => { using unrefTimer = stub(Deno, "unrefTimer", () => { throw new ReferenceError(); }); await delay(100, { persistent: false }); assertSpyCalls(unrefTimer, 1);});
Deno.test({ name: "delay() handles persistent option with error", async fn() { using unrefTimer = stub(Deno, "unrefTimer", () => { throw new Error("Error!"); }); try { await delay(100, { persistent: false }); } catch (e) { assert(e instanceof Error); assertEquals(e.message, "Error!"); assertSpyCalls(unrefTimer, 1); } }, sanitizeResources: false, sanitizeOps: false,});
Version Info