forked from bytecodealliance/jco
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfuture.test.js
More file actions
72 lines (53 loc) · 2.01 KB
/
Copy pathfuture.test.js
File metadata and controls
72 lines (53 loc) · 2.01 KB
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
import { describe, test, expect } from "vitest";
describe("Node.js Preview3 canon future", () => {
test("Simple Future", async () => {
const { future } = await import("@bytecodealliance/preview3-shim/future");
const { tx, rx } = future();
const message = "Hello world!";
await tx.write(message);
// first read yields the message
const value = await rx.read();
expect(value).toBe(message);
// subsequent reads yield null
const noValue = await rx.read();
expect(noValue).toBeNull();
});
test("close resolves to null", async () => {
const { future } = await import("@bytecodealliance/preview3-shim/future");
const { tx, rx } = future();
await tx.close();
const value = await rx.read();
expect(value).toBeNull();
});
test("abort rejects the reader", async () => {
const { future } = await import("@bytecodealliance/preview3-shim/future");
const { tx, rx } = future();
const err = new Error("aborted");
await tx.abort(err);
await expect(rx.read()).rejects.toThrow("aborted");
});
test("reader has one shot semantics", async () => {
const { future } = await import("@bytecodealliance/preview3-shim/future");
const { tx, rx } = future();
await tx.write("thenable");
expect(await rx).toBe("thenable");
expect(await rx.read()).toBeNull();
});
test("writer cannot write twice", async () => {
const { future } = await import("@bytecodealliance/preview3-shim/future");
const { tx, rx } = future();
await tx.write("first");
await expect(tx.write("second")).rejects.toThrow();
// reader still gets the first value
const value = await rx.read();
expect(value).toBe("first");
});
test("intoPromise returns the underlying promise", async () => {
const { future } = await import("@bytecodealliance/preview3-shim/future");
const { tx, rx } = future();
await tx.write(42);
const promise = rx.intoPromise();
expect(promise).toBeInstanceOf(Promise);
expect(await promise).toBe(42);
});
});