Repository navigation
Expand file tree
/
Copy pathmux.test.ts
More file actions
166 lines (153 loc) · 5.61 KB
/
Copy pathmux.test.ts
File metadata and controls
166 lines (153 loc) · 5.61 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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import { expect, test, vi } from "vitest";
import { type Asked, Mux, Refused, type Stream, type StreamEvents, WINDOW } from "./mux.ts";
/** Two ends, with every message delivered in its own turn as a socket would. */
function pair(asked: (asked: Asked) => void) {
const dialler: Mux = new Mux({
dialled: true,
send: (m) => queueMicrotask(() => listener.receive(m)),
asked: () => {},
});
const listener: Mux = new Mux({ dialled: false, send: (m) => queueMicrotask(() => dialler.receive(m)), asked });
return { dialler, listener };
}
function collect(stream: Stream, events: Partial<StreamEvents> = {}) {
const got = { bytes: 0, ended: false, closed: undefined as Error | null | undefined };
stream.listen({
data(bytes) {
got.bytes += bytes.length;
stream.consumed(bytes.length);
},
end() {
got.ended = true;
},
drain() {},
close(error) {
got.closed = error;
},
...events,
});
return got;
}
const settle = () => new Promise((resolve) => setTimeout(resolve, 5));
test("a stream carries far more than its window, and each end can stop sending before the other", async () => {
let far: Stream | undefined;
const { dialler } = pair((asked) => {
far = asked.accept({ hello: asked.to });
});
const near = await dialler.open({ kind: "port", port: 80 });
expect(near.answer).toEqual({ hello: { kind: "port", port: 80 } });
const atFar = collect(far as Stream);
const atNear = collect(near);
near.write(new Uint8Array(WINDOW * 5));
near.end();
await vi.waitFor(() => expect(atFar).toMatchObject({ bytes: WINDOW * 5, ended: true, closed: undefined }));
far?.write(new Uint8Array(1000));
far?.end();
await vi.waitFor(() => expect(atNear).toMatchObject({ bytes: 1000, ended: true, closed: null }));
expect(atFar.closed).toBeNull();
});
test("a reader that takes nothing holds its own writer up, and no other stream", async () => {
const streams: Stream[] = [];
const { dialler } = pair((asked) => streams.push(asked.accept()));
const slow = await dialler.open("slow");
const quick = await dialler.open("quick");
let held = 0;
collect(streams[0], { data: (bytes) => (held += bytes.length) });
const atQuick = collect(streams[1]);
slow.write(new Uint8Array(WINDOW * 2));
quick.write(new Uint8Array(WINDOW * 2));
await vi.waitFor(() => expect(atQuick.bytes).toBe(WINDOW * 2));
expect(held).toBe(WINDOW);
expect(slow.buffered).toBe(WINDOW);
streams[0].consumed(WINDOW);
await vi.waitFor(() => expect(held).toBe(WINDOW * 2));
expect(slow.buffered).toBe(0);
});
test("a refusal says why, and a channel that closes ends what is open and what is asked for", async () => {
const asks: Asked[] = [];
const { dialler, listener } = pair((asked) => asks.push(asked));
const refused = dialler.open("no");
await settle();
asks[0].refuse("node2 is off.");
await expect(refused).rejects.toEqual(new Refused("node2 is off."));
const opening = dialler.open("yes");
await settle();
const far = collect(asks[1].accept());
const near = collect(await opening);
const waiting = dialler.open("never answered");
dialler.close();
listener.close();
await expect(waiting).rejects.toThrow("The link closed.");
expect(near.closed).toBeInstanceOf(Error);
expect(far.closed).toBeInstanceOf(Error);
await expect(dialler.open("late")).rejects.toThrow("The link is closed.");
});
test("what is given up before it is answered is dropped at the far end", async () => {
const asks: Asked[] = [];
const { dialler } = pair((asked) => asks.push(asked));
const giveUp = new AbortController();
const opening = dialler.open("slow", giveUp.signal);
await settle();
giveUp.abort(new Error("Cancelled."));
await expect(opening).rejects.toThrow("Cancelled.");
await settle();
const far = collect(asks[0].accept());
expect(far.closed).toBeInstanceOf(Error);
});
test("bytes that come before anyone listens are kept in order", async () => {
let far: Stream | undefined;
const { dialler } = pair((asked) => {
far = asked.accept();
});
const near = await dialler.open("x");
near.write(Uint8Array.of(1));
near.write(Uint8Array.of(2));
near.end();
await settle();
const seen: string[] = [];
far?.listen({
data: (bytes) => seen.push(`data ${[...bytes]}`),
end: () => seen.push("end"),
drain() {},
close: () => seen.push("close"),
});
expect(seen).toEqual(["data 1", "data 2", "end"]);
});
test("malformed JSON in OPEN refuses the stream instead of throwing", () => {
const sent: Uint8Array[] = [];
const asks: Asked[] = [];
const mux = new Mux({
dialled: false,
send: (m) => sent.push(m),
asked: (a) => asks.push(a),
});
const payload = new TextEncoder().encode("{bad json");
const frame = new Uint8Array(5 + payload.length);
frame[0] = 1;
new DataView(frame.buffer).setUint32(1, 7, true);
frame.set(payload, 5);
mux.receive(frame);
expect(asks).toEqual([]);
expect(sent).toHaveLength(1);
expect(sent[0][0]).toBe(3);
expect(new DataView(sent[0].buffer).getUint32(1, true)).toBe(7);
expect(new TextDecoder().decode(sent[0].subarray(5))).toBe("Malformed stream request.");
});
test("malformed JSON in OPENED rejects the opening promise and resets the stream", async () => {
const sent: Uint8Array[] = [];
const mux = new Mux({
dialled: true,
send: (m) => sent.push(m),
asked: () => {},
});
const opening = mux.open({ kind: "port", port: 80 });
const payload = new TextEncoder().encode("{bad json");
const frame = new Uint8Array(5 + payload.length);
frame[0] = 2;
new DataView(frame.buffer).setUint32(1, 1, true);
frame.set(payload, 5);
mux.receive(frame);
await expect(opening).rejects.toThrow("Malformed stream reply.");
expect(sent.at(-1)?.[0]).toBe(7);
expect(new DataView((sent.at(-1) as Uint8Array).buffer).getUint32(1, true)).toBe(1);
});