-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
barrier_test.ts
94 lines (84 loc) · 2.38 KB
/
barrier_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
import { test } from "@cross/test";
import { assertEquals, assertRejects, assertThrows } from "@std/assert";
import { delay } from "@std/async";
import { Barrier } from "./barrier.ts";
import { deadline } from "./_testutil.ts";
test(
"Barrier 'wait' waits until the number of waiters reached the size specified to the barrier",
async () => {
const barrier = new Barrier(5);
const workers = [];
const results: string[] = [];
for (let i = 0; i < 5; i++) {
workers.push((async () => {
results.push(`before wait ${i}`);
await barrier.wait();
results.push(`after wait ${i}`);
})());
}
await Promise.all(workers);
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(
"Barrier 'wait' with non-aborted signal",
async () => {
const controller = new AbortController();
const barrier = new Barrier(2);
await assertRejects(
() => deadline(barrier.wait({ signal: controller.signal }), 100),
DOMException,
"Signal timed out.",
);
},
);
test(
"Barrier 'wait' with signal aborted after delay",
async () => {
const controller = new AbortController();
const barrier = new Barrier(2);
const reason = new Error("Aborted");
delay(50).then(() => controller.abort(reason));
await assertRejects(
() => deadline(barrier.wait({ signal: controller.signal }), 100),
Error,
"Aborted",
);
},
);
test(
"Barrier 'wait' with already aborted signal",
async () => {
const controller = new AbortController();
const barrier = new Barrier(2);
const reason = new Error("Aborted");
controller.abort(reason);
await assertRejects(
() => deadline(barrier.wait({ signal: controller.signal }), 100),
Error,
"Aborted",
);
},
);
test(
"Barrier throws RangeError if size is not a positive safe integer",
() => {
assertThrows(() => new Barrier(NaN), RangeError);
assertThrows(() => new Barrier(Infinity), RangeError);
assertThrows(() => new Barrier(-Infinity), RangeError);
assertThrows(() => new Barrier(-1), RangeError);
assertThrows(() => new Barrier(1.1), RangeError);
assertThrows(() => new Barrier(0), RangeError);
},
);