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

deadline_test.ts
View Documentation
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
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.import { assertEquals, assertRejects } from "../assert/mod.ts";import { delay } from "./delay.ts";import { deadline, DeadlineError } from "./deadline.ts";
Deno.test("deadline() returns fulfilled promise", async () => { const controller = new AbortController(); const { signal } = controller; const p = delay(100, { signal }) .catch(() => {}) .then(() => "Hello"); const result = await deadline(p, 1000); assertEquals(result, "Hello"); controller.abort();});
Deno.test("deadline() throws DeadlineError", async () => { const controller = new AbortController(); const { signal } = controller; const p = delay(1000, { signal }) .catch(() => {}) .then(() => "Hello"); await assertRejects(async () => { await deadline(p, 100); }, DeadlineError); controller.abort();});
Deno.test("deadline() throws when promise is rejected", async () => { const controller = new AbortController(); const { signal } = controller; const p = delay(100, { signal }) .catch(() => {}) .then(() => Promise.reject(new Error("booom"))); await assertRejects( async () => { await deadline(p, 1000); }, Error, "booom", ); controller.abort();});
Deno.test("deadline() handles non-aborted signal", async () => { const controller = new AbortController(); const { signal } = controller; const p = delay(100, { signal }) .catch(() => {}) .then(() => "Hello"); const abort = new AbortController(); const result = await deadline(p, 1000, { signal: abort.signal }); assertEquals(result, "Hello"); controller.abort();});
Deno.test("deadline() handles aborted signal after delay", async () => { const controller = new AbortController(); const { signal } = controller; const p = delay(100, { signal }) .catch(() => {}) .then(() => "Hello"); const abort = new AbortController(); const promise = deadline(p, 100, { signal: abort.signal }); abort.abort(); await assertRejects(async () => { await promise; }, DeadlineError); controller.abort();});
Deno.test("deadline() handles already aborted signal", async () => { const controller = new AbortController(); const { signal } = controller; const p = delay(100, { signal }) .catch(() => {}) .then(() => "Hello"); const abort = new AbortController(); abort.abort(); await assertRejects(async () => { await deadline(p, 100, { signal: abort.signal }); }, DeadlineError); controller.abort();});
std

Version Info

Tagged at
6 months ago