forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest-stream-iter-push-basic.js
More file actions
193 lines (155 loc) · 4.65 KB
/
Copy pathtest-stream-iter-push-basic.js
File metadata and controls
193 lines (155 loc) · 4.65 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
// Flags: --experimental-stream-iter
'use strict';
const common = require('../common');
const assert = require('assert');
const { push, text } = require('stream/iter');
async function testBasicWriteRead() {
const { writer, readable } = push();
writer.write('hello');
writer.end();
const data = await text(readable);
assert.strictEqual(data, 'hello');
}
async function testMultipleWrites() {
const { writer, readable } = push({ highWaterMark: 10 });
writer.write('a');
writer.write('b');
writer.write('c');
writer.end();
const data = await text(readable);
assert.strictEqual(data, 'abc');
}
async function testDesiredSize() {
const { writer } = push({ highWaterMark: 3 });
assert.strictEqual(writer.desiredSize, 3);
writer.writeSync('a');
assert.strictEqual(writer.desiredSize, 2);
writer.writeSync('b');
assert.strictEqual(writer.desiredSize, 1);
writer.writeSync('c');
assert.strictEqual(writer.desiredSize, 0);
writer.end();
assert.strictEqual(writer.desiredSize, null);
}
async function testWriterEnd() {
const { writer, readable } = push();
const totalBytes = writer.endSync();
assert.strictEqual(totalBytes, 0);
// Calling endSync again returns byte count (idempotent when closed)
assert.strictEqual(writer.endSync(), 0);
const batches = [];
for await (const batch of readable) {
batches.push(batch);
}
assert.strictEqual(batches.length, 0);
}
async function testWriterFail() {
const { writer, readable } = push();
writer.fail(new Error('test fail'));
await assert.rejects(
async () => {
// eslint-disable-next-line no-unused-vars
for await (const _ of readable) {
assert.fail('Should not reach here');
}
},
{ message: 'test fail' },
);
}
async function testConsumerBreak() {
const { writer, readable } = push({ highWaterMark: 10 });
writer.writeSync('a');
writer.writeSync('b');
writer.writeSync('c');
// Break after first batch
// eslint-disable-next-line no-unused-vars
for await (const _ of readable) {
break;
}
// Writer should now see null desiredSize
assert.strictEqual(writer.desiredSize, null);
}
async function testAbortSignal() {
const ac = new AbortController();
const { readable } = push({ signal: ac.signal });
ac.abort();
await assert.rejects(
async () => {
// eslint-disable-next-line no-unused-vars
for await (const _ of readable) {
assert.fail('Should not reach here');
}
},
{ name: 'AbortError' },
);
}
async function testPreAbortedSignal() {
const { readable } = push({ signal: AbortSignal.abort() });
await assert.rejects(async () => {
// eslint-disable-next-line no-unused-vars
for await (const _ of readable) {
assert.fail('Should not reach here');
}
}, { name: 'AbortError' });
}
async function testConsumerBreakWriteSyncReturnsFalse() {
const { writer, readable } = push({ highWaterMark: 10 });
writer.writeSync('a');
// Break after first batch
// eslint-disable-next-line no-unused-vars
for await (const _ of readable) {
break;
}
// After consumer break, writeSync should return false
assert.strictEqual(writer.writeSync('b'), false);
assert.strictEqual(writer.desiredSize, null);
}
async function testPendingNextSettlesAfterReturn() {
const { readable } = push();
const iter = readable[Symbol.asyncIterator]();
const pendingNext = iter.next();
await iter.return();
const result = await pendingNext;
assert.strictEqual(result.done, true);
assert.strictEqual(result.value, undefined);
}
async function testPushWithTransforms() {
const upper = (chunks) => {
if (chunks === null) return null;
return chunks.map((c) => {
const str = new TextDecoder().decode(c);
return new TextEncoder().encode(str.toUpperCase());
});
};
const { writer, readable } = push(upper);
writer.write('hello');
writer.end();
const data = await text(readable);
assert.strictEqual(data, 'HELLO');
}
async function testInvalidBackpressure() {
assert.throws(() => push({ backpressure: 'banana' }), {
code: 'ERR_INVALID_ARG_VALUE',
});
assert.throws(() => push({ backpressure: '' }), {
code: 'ERR_INVALID_ARG_VALUE',
});
// Valid values should not throw
for (const bp of ['strict', 'block', 'drop-oldest', 'drop-newest']) {
push({ backpressure: bp });
}
}
Promise.all([
testBasicWriteRead(),
testMultipleWrites(),
testDesiredSize(),
testWriterEnd(),
testWriterFail(),
testConsumerBreak(),
testAbortSignal(),
testPreAbortedSignal(),
testConsumerBreakWriteSyncReturnsFalse(),
testPendingNextSettlesAfterReturn(),
testPushWithTransforms(),
testInvalidBackpressure(),
]).then(common.mustCall());