-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wait_group_test.ts
97 lines (88 loc) · 2.3 KB
/
wait_group_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
import { test } from "@cross/test";
import { assertEquals, assertRejects, assertThrows } from "@std/assert";
import { delay } from "@std/async";
import { WaitGroup } from "./wait_group.ts";
import { deadline } from "./_testutil.ts";
test(
"WaitGroup Ensure WaitGroup synchronizes multiple workers",
async () => {
const wg = new WaitGroup();
const workers = [];
const results: string[] = [];
for (let i = 0; i < 5; i++) {
workers.push((async () => {
wg.add(1);
results.push(`before wait ${i}`);
await delay(100);
results.push(`after wait ${i}`);
wg.done();
})());
}
await wg.wait();
assertEquals(results, [
"before wait 0",
"before wait 1",
"before wait 2",
"before wait 3",
"before wait 4",
"after wait 0",
"after wait 1",
"after wait 2",
"after wait 3",
"after wait 4",
]);
},
);
test(
"WaitGroup 'wait' with non-aborted signal",
async () => {
const controller = new AbortController();
const wg = new WaitGroup();
wg.add(1);
await assertRejects(
() => deadline(wg.wait({ signal: controller.signal }), 100),
DOMException,
"Signal timed out.",
);
},
);
test(
"WaitGroup 'wait' with signal aborted after delay",
async () => {
const controller = new AbortController();
const wg = new WaitGroup();
wg.add(1);
const reason = new Error("Aborted");
delay(50).then(() => controller.abort(reason));
await assertRejects(
() => deadline(wg.wait({ signal: controller.signal }), 100),
Error,
"Aborted",
);
},
);
test(
"WaitGroup 'wait' with already aborted signal",
async () => {
const controller = new AbortController();
const wg = new WaitGroup();
wg.add(1);
const reason = new Error("Aborted");
controller.abort(reason);
await assertRejects(
() => deadline(wg.wait({ signal: controller.signal }), 100),
Error,
"Aborted",
);
},
);
test(
"WaitGroup 'add' throws RangeError if delta is not a safe integer",
() => {
const wg = new WaitGroup();
assertThrows(() => wg.add(NaN), RangeError);
assertThrows(() => wg.add(Infinity), RangeError);
assertThrows(() => wg.add(-Infinity), RangeError);
assertThrows(() => wg.add(1.1), RangeError);
},
);