-
-
Notifications
You must be signed in to change notification settings - Fork 36.2k
Expand file tree
/
Copy pathtest-stream-iter-broadcast-from.js
More file actions
194 lines (163 loc) Β· 5.8 KB
/
Copy pathtest-stream-iter-broadcast-from.js
File metadata and controls
194 lines (163 loc) Β· 5.8 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
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
// Flags: --experimental-stream-iter
'use strict';
const common = require('../common');
const assert = require('assert');
const { broadcast, Broadcast, from, text } = require('stream/iter');
// =============================================================================
// Broadcast.from
// =============================================================================
async function testBroadcastFromAsyncIterable() {
const source = from('broadcast-from');
const { broadcast: bc } = Broadcast.from(source);
const consumer = bc.push();
const data = await text(consumer);
assert.strictEqual(data, 'broadcast-from');
}
async function testBroadcastFromNonArrayChunks() {
// Source that yields single Uint8Array chunks (not arrays)
const enc = new TextEncoder();
async function* singleChunkSource() {
yield enc.encode('hello');
yield enc.encode(' world');
}
const { broadcast: bc } = Broadcast.from(singleChunkSource());
const consumer = bc.push();
const data = await text(consumer);
assert.strictEqual(data, 'hello world');
}
async function testBroadcastFromStringChunks() {
// Source that yields bare strings (not arrays)
async function* stringSource() {
yield 'foo';
yield 'bar';
}
const { broadcast: bc } = Broadcast.from(stringSource());
const consumer = bc.push();
const data = await text(consumer);
assert.strictEqual(data, 'foobar');
}
async function testBroadcastFromMultipleConsumers() {
const source = from('shared-data');
const { broadcast: bc } = Broadcast.from(source);
const c1 = bc.push();
const c2 = bc.push();
const [data1, data2] = await Promise.all([
text(c1),
text(c2),
]);
assert.strictEqual(data1, 'shared-data');
assert.strictEqual(data2, 'shared-data');
}
// =============================================================================
// AbortSignal
// =============================================================================
async function testAbortSignal() {
const ac = new AbortController();
const { broadcast: bc } = broadcast({ signal: ac.signal });
const consumer = bc.push();
ac.abort();
await assert.rejects(async () => {
// eslint-disable-next-line no-unused-vars
for await (const _ of consumer) {
assert.fail('Should not reach here');
}
}, { name: 'AbortError' });
}
async function testAlreadyAbortedSignal() {
const ac = new AbortController();
ac.abort();
const { broadcast: bc } = broadcast({ signal: ac.signal });
const consumer = bc.push();
await assert.rejects(async () => {
// eslint-disable-next-line no-unused-vars
for await (const _ of consumer) {
assert.fail('Should not reach here');
}
}, { name: 'AbortError' });
}
// =============================================================================
// Broadcast.from() hang fix - cancel while write blocked on backpressure
// =============================================================================
async function testBroadcastFromCancelWhileBlocked() {
// Create a slow async source that blocks between yields
let sourceFinished = false;
async function* slowSource() {
const enc = new TextEncoder();
yield [enc.encode('chunk1')];
// Simulate a long delay - the cancel should unblock this
await new Promise((resolve) => setTimeout(resolve, 10000));
yield [enc.encode('chunk2')];
sourceFinished = true;
}
const { broadcast: bc } = Broadcast.from(slowSource());
const consumer = bc.push();
// Read the first chunk
const iter = consumer[Symbol.asyncIterator]();
const first = await iter.next();
assert.strictEqual(first.done, false);
// Cancel while the source is blocked waiting to yield the next chunk
bc.cancel();
// The iteration should complete (not hang)
const next = await iter.next();
assert.strictEqual(next.done, true);
// Source should NOT have finished (we cancelled before chunk2)
assert.strictEqual(sourceFinished, false);
}
// =============================================================================
// Source error propagation via Broadcast.from()
// =============================================================================
async function testBroadcastFromSourceError() {
async function* failingSource() {
yield [new TextEncoder().encode('a')];
throw new Error('broadcast source boom');
}
const { broadcast: bc } = Broadcast.from(failingSource());
const consumer = bc.push();
await assert.rejects(async () => {
// eslint-disable-next-line no-unused-vars
for await (const _ of consumer) { /* consume */ }
}, { message: 'broadcast source boom' });
}
// =============================================================================
// Protocol validation
// =============================================================================
function testBroadcastProtocolReturnsNull() {
const obj = {
[Symbol.for('Stream.broadcastProtocol')]() { return null; },
};
assert.throws(
() => Broadcast.from(obj),
{ code: 'ERR_INVALID_RETURN_VALUE' },
);
}
function testBroadcastProtocolReturnsString() {
const obj = {
[Symbol.for('Stream.broadcastProtocol')]() { return 'bad'; },
};
assert.throws(
() => Broadcast.from(obj),
{ code: 'ERR_INVALID_RETURN_VALUE' },
);
}
function testBroadcastProtocolReturnsUndefined() {
const obj = {
[Symbol.for('Stream.broadcastProtocol')]() { },
};
assert.throws(
() => Broadcast.from(obj),
{ code: 'ERR_INVALID_RETURN_VALUE' },
);
}
Promise.all([
testBroadcastFromAsyncIterable(),
testBroadcastFromNonArrayChunks(),
testBroadcastFromStringChunks(),
testBroadcastFromMultipleConsumers(),
testAbortSignal(),
testAlreadyAbortedSignal(),
testBroadcastFromCancelWhileBlocked(),
testBroadcastFromSourceError(),
testBroadcastProtocolReturnsNull(),
testBroadcastProtocolReturnsString(),
testBroadcastProtocolReturnsUndefined(),
]).then(common.mustCall());