-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
channel_test.ts
62 lines (58 loc) · 1.62 KB
/
channel_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
import { test } from "@cross/test";
import { assertEquals, assertRejects } from "@std/assert";
import { deadline } from "./_testutil.ts";
import { provide } from "./provide.ts";
import { pop } from "./pop.ts";
import { push } from "./push.ts";
import { collect } from "./collect.ts";
import { channel } from "./channel.ts";
await test(
"channel pushing data to the writer makes it available to the reader",
async () => {
const { reader, writer } = channel<number>();
await provide(writer, [1, 2, 3]);
assertEquals(await collect(reader), [1, 2, 3]);
},
);
await test(
"channel the reader waits for the writer to push data",
async () => {
const { reader, writer } = channel<number>();
const waiter = pop(reader);
await assertRejects(
() => deadline(waiter, 100),
DOMException,
"Signal timed out.",
);
await push(writer, 1);
assertEquals(await deadline(waiter, 100), 1);
},
);
await test(
"channel the reader is canceled when the writer is closed",
async () => {
const { reader, writer } = channel<number>();
const waiter = collect(reader);
await assertRejects(
() => deadline(waiter, 100),
DOMException,
"Signal timed out.",
);
await push(writer, 1);
await assertRejects(
() => deadline(waiter, 100),
DOMException,
"Signal timed out.",
);
writer.close();
assertEquals(await deadline(waiter, 100), [1]);
},
);
await test(
"channel closing the writer with already canceled reader does not throw an error",
() => {
const { reader, writer } = channel<number>();
reader.cancel();
writer.close();
},
);