-
Notifications
You must be signed in to change notification settings - Fork 1
/
chan.test.ts
218 lines (208 loc) · 6.94 KB
/
chan.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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import { Chan, ClosedChanError } from './chan.js';
import { setTimeout } from 'node:timers/promises';
async function toArray<T>(iter: AsyncIterable<T>): Promise<T[]> {
const result: T[] = [];
for await (const item of iter) {
result.push(item);
}
return result;
}
describe('Chan', () => {
it('allows to push, close and read', async () => {
const ch = new Chan<number>(Number.MAX_SAFE_INTEGER);
await ch.send(1);
await ch.send(2);
ch.close();
expect(await toArray(ch)).toEqual([1, 2]);
});
it('allows to push slower than read simultaneously', async () => {
const itemsCount = 100;
const ch = new Chan<number>(5);
async function pusher() {
const result: number[] = [];
for (let i = 0; i < itemsCount; i++) {
await setTimeout(10);
await ch.send(i);
result.push(i);
}
ch.close();
return result;
}
async function reader() {
const result: number[] = [];
for await (const item of ch) {
result.push(item);
}
return result;
}
const [pushResult, readResult] = await Promise.all([
pusher(),
reader(),
]);
expect(readResult).toEqual(pushResult);
expect(ch.stat).toEqual({
// because we have only 1 reader and it is quicker than the writer
readers: { peakLength: 1 },
writers: { peakLength: 0 },
data: { peakLength: 0 },
});
});
it('allows to push quicker than read simultaneously', async () => {
const itemsCount = 100;
const ch = new Chan<number>(5);
async function pusher() {
const result: number[] = [];
for (let i = 0; i < itemsCount; i++) {
await ch.send(i);
result.push(i);
}
ch.close();
return result;
}
async function reader() {
const result: number[] = [];
for await (const item of ch) {
await setTimeout(10);
result.push(item);
}
return result;
}
const [pushResult, readResult] = await Promise.all([
pusher(),
reader(),
]);
expect(readResult).toEqual(pushResult);
expect(ch.stat).toEqual({
readers: { peakLength: 0 },
writers: { peakLength: 1 },
data: { peakLength: 5 },
});
});
it('accumulates up to bufferSize items on late read case', async () => {
const ch = new Chan<number>(5);
async function writer() {
const result: number[] = [];
for (let i = 0; i < 100; i++) {
// console.log('sending', i);
await ch.send(i);
// console.log('sent', i);
result.push(i);
}
ch.close();
return result;
}
async function reader() {
await setTimeout(100);
const result: number[] = [];
for await (const item of ch) {
result.push(item);
}
return result;
}
const [pushResult, readResult] = await Promise.all([
writer(),
reader(),
]);
expect(readResult).toEqual(pushResult);
expect(ch.stat).toEqual({
readers: { peakLength: 0 },
// because we have only 1 writer and it is quicker than the reader
writers: { peakLength: 1 },
// because the writer had enough items to fill the buffer
data: { peakLength: 5 },
});
});
it('supports 1 writer and multiple readers', async () => {
const ch = new Chan<number>(5);
const readers: Promise<number[]>[] = [];
for (let i = 0; i < 10; i++) {
readers.push(
(async () => {
const result: number[] = [];
for await (const item of ch) {
result.push(item);
}
return result;
})()
);
}
async function writer() {
const result: number[] = [];
for (let i = 0; i < 100; i++) {
await ch.send(i);
result.push(i);
}
ch.close();
return result;
}
const [writerResult, readersResult] = await Promise.all([
writer(),
Promise.all(readers),
]);
expect(ch.stat.data.peakLength).toEqual(0);
expect(ch.stat.writers.peakLength).toEqual(0);
expect(ch.stat.readers.peakLength).toEqual(10);
const normalizedReadersResult = readersResult
.flat()
.sort((a, b) => a - b);
expect(normalizedReadersResult).toEqual(writerResult);
});
it('supports multiple writers and one reader', async () => {
const ch = new Chan<number>();
async function read() {
const readResult: number[] = [];
for await (const item of ch) {
readResult.push(item);
}
return readResult;
}
const writers: Promise<number[]>[] = [];
for (let i = 0; i < 10; i++) {
writers.push(
(async () => {
const results: number[] = [];
for (let j = 0; j < 100; j++) {
await ch.send(j);
results.push(j);
}
return results;
})()
);
}
const [readResult, writersResult] = await Promise.all([
read(),
Promise.all(writers).then(async (r) => {
// close the channel after all writers are done
ch.close();
return r;
}),
]);
expect(readResult.length).toEqual(1000);
const normalizedWritersResult = writersResult
.flat()
.sort((a, b) => a - b);
expect(normalizedWritersResult).toEqual(readResult);
});
it('rejects when closed', async () => {
const ch = new Chan<number>();
ch.close();
await expect(ch.send(1)).rejects.toThrow(ClosedChanError);
});
describe('recv', () => {
it('receives data', async () => {
const ch = new Chan<number>();
// Schedule a send in the future.
(async () => {
await setTimeout(0);
ch.send(1);
})();
// Wait for the send to complete.
const result = await ch.recv();
expect(result).toEqual([1, true]);
ch.close();
// Attempt to receive from a closed channel.
const result2 = await ch.recv();
expect(result2).toEqual([undefined, false]);
});
});
});