deno.land / std@0.224.0 / async / delay_test.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// 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,});
std

Version Info

Tagged at
3 weeks ago