-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notify_test.ts
131 lines (115 loc) · 4.25 KB
/
notify_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
import { test } from "@cross/test";
import { delay } from "@std/async/delay";
import { assertEquals, assertRejects, assertThrows } from "@std/assert";
import { flushPromises } from "./flush_promises.ts";
import { peekPromiseState } from "./peek_promise_state.ts";
import { Notify } from "./notify.ts";
test("Notify 'notify' wakes up a single waiter", async () => {
const notify = new Notify();
const waiter1 = notify.notified();
const waiter2 = notify.notified();
assertEquals(notify.waiterCount, 2);
notify.notify();
await flushPromises();
assertEquals(notify.waiterCount, 1);
assertEquals(await peekPromiseState(waiter1), "fulfilled");
assertEquals(await peekPromiseState(waiter2), "pending");
notify.notify();
await flushPromises();
assertEquals(notify.waiterCount, 0);
assertEquals(await peekPromiseState(waiter1), "fulfilled");
assertEquals(await peekPromiseState(waiter2), "fulfilled");
});
test("Notify 'notify' wakes up a multiple waiters", async () => {
const notify = new Notify();
const waiter1 = notify.notified();
const waiter2 = notify.notified();
const waiter3 = notify.notified();
const waiter4 = notify.notified();
const waiter5 = notify.notified();
assertEquals(notify.waiterCount, 5);
notify.notify(2);
await flushPromises();
assertEquals(notify.waiterCount, 3);
assertEquals(await peekPromiseState(waiter1), "fulfilled");
assertEquals(await peekPromiseState(waiter2), "fulfilled");
assertEquals(await peekPromiseState(waiter3), "pending");
assertEquals(await peekPromiseState(waiter4), "pending");
assertEquals(await peekPromiseState(waiter5), "pending");
notify.notify(2);
await flushPromises();
assertEquals(notify.waiterCount, 1);
assertEquals(await peekPromiseState(waiter1), "fulfilled");
assertEquals(await peekPromiseState(waiter2), "fulfilled");
assertEquals(await peekPromiseState(waiter3), "fulfilled");
assertEquals(await peekPromiseState(waiter4), "fulfilled");
assertEquals(await peekPromiseState(waiter5), "pending");
notify.notify(2);
await flushPromises();
assertEquals(notify.waiterCount, 0);
assertEquals(await peekPromiseState(waiter1), "fulfilled");
assertEquals(await peekPromiseState(waiter2), "fulfilled");
assertEquals(await peekPromiseState(waiter3), "fulfilled");
assertEquals(await peekPromiseState(waiter4), "fulfilled");
assertEquals(await peekPromiseState(waiter5), "fulfilled");
});
test("Notify 'notifyAll' wakes up all waiters", async () => {
const notify = new Notify();
const waiter1 = notify.notified();
const waiter2 = notify.notified();
assertEquals(notify.waiterCount, 2);
notify.notifyAll();
await flushPromises();
assertEquals(notify.waiterCount, 0);
assertEquals(await peekPromiseState(waiter1), "fulfilled");
assertEquals(await peekPromiseState(waiter2), "fulfilled");
});
test(
"Notify 'notified' with non-aborted signal",
async () => {
const controller = new AbortController();
const notify = new Notify();
const waiter = notify.notified({ signal: controller.signal });
assertEquals(await peekPromiseState(waiter), "pending");
},
);
test(
"Notify 'notified' with signal aborted after delay",
async () => {
const controller = new AbortController();
const notify = new Notify();
const reason = new Error("Aborted");
delay(100).then(() => controller.abort(reason));
await assertRejects(
() => notify.notified({ signal: controller.signal }),
Error,
"Aborted",
);
},
);
test(
"Notify 'notified' with already aborted signal",
async () => {
const controller = new AbortController();
const notify = new Notify();
const reason = new Error("Aborted");
controller.abort(reason);
await assertRejects(
() => notify.notified({ signal: controller.signal }),
Error,
"Aborted",
);
},
);
test(
"Notify 'notify' throws RangeError if size is not a positive safe integer",
() => {
const notify = new Notify();
assertThrows(() => notify.notify(NaN), RangeError);
assertThrows(() => notify.notify(Infinity), RangeError);
assertThrows(() => notify.notify(-Infinity), RangeError);
assertThrows(() => notify.notify(-1), RangeError);
assertThrows(() => notify.notify(1.1), RangeError);
assertThrows(() => notify.notify(0), RangeError);
},
);