Skip to content

Commit 79b4588

Browse files
addaleaxMyles Borins
authored and
Myles Borins
committed
test: add tests for some stream.Readable uses
* test: check invalid chunk error for readable.push Test that passing invalid chunks to readable.push() in non-object mode throw errors. * test: add simple object mode + decoder stream test * test: add test for readable stream lacking _read Check that using a readable stream without a _read method will throw an error. * test: add basic test for piping to multiple dests Add a simple test for piping and unpiping from a readable stream to multiple writable streams. PR-URL: #7260 Reviewed-By: Matteo Collina <[email protected]>
1 parent b95e5d7 commit 79b4588

4 files changed

+90
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict';
2+
require('../common');
3+
const stream = require('stream');
4+
const assert = require('assert');
5+
6+
const readable = new stream.Readable({
7+
read: () => {},
8+
encoding: 'utf16le',
9+
objectMode: true
10+
});
11+
12+
readable.push(Buffer.from('abc', 'utf16le'));
13+
readable.push(Buffer.from('def', 'utf16le'));
14+
readable.push(null);
15+
16+
// Without object mode, these would be concatenated into a single chunk.
17+
assert.strictEqual(readable.read(), 'abc');
18+
assert.strictEqual(readable.read(), 'def');
19+
assert.strictEqual(readable.read(), null);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
'use strict';
2+
const common = require('../common');
3+
const stream = require('stream');
4+
const assert = require('assert');
5+
6+
const readable = new stream.Readable({
7+
read: () => {}
8+
});
9+
10+
const writables = [];
11+
12+
for (let i = 0; i < 5; i++) {
13+
const target = new stream.Writable({
14+
write: common.mustCall((chunk, encoding, callback) => {
15+
target.output.push(chunk);
16+
callback();
17+
}, 1)
18+
});
19+
target.output = [];
20+
21+
target.on('pipe', common.mustCall(() => {}));
22+
readable.pipe(target);
23+
24+
25+
writables.push(target);
26+
}
27+
28+
const input = Buffer.from([1, 2, 3, 4, 5]);
29+
30+
readable.push(input);
31+
32+
// The pipe() calls will postpone emission of the 'resume' event using nextTick,
33+
// so no data will be available to the writable streams until then.
34+
process.nextTick(common.mustCall(() => {
35+
for (const target of writables) {
36+
assert.deepStrictEqual(target.output, [input]);
37+
38+
target.on('unpipe', common.mustCall(() => {}));
39+
readable.unpipe(target);
40+
}
41+
42+
readable.push('something else'); // This does not get through.
43+
readable.push(null);
44+
readable.resume(); // Make sure the 'end' event gets emitted.
45+
}));
46+
47+
readable.on('end', common.mustCall(() => {
48+
for (const target of writables) {
49+
assert.deepStrictEqual(target.output, [input]);
50+
}
51+
}));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
require('../common');
3+
const stream = require('stream');
4+
const assert = require('assert');
5+
6+
const readable = new stream.Readable({
7+
read: () => {}
8+
});
9+
10+
assert.throws(() => readable.push([]), /Invalid non-string\/buffer chunk/);
11+
assert.throws(() => readable.push({}), /Invalid non-string\/buffer chunk/);
12+
assert.throws(() => readable.push(0), /Invalid non-string\/buffer chunk/);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict';
2+
require('../common');
3+
const stream = require('stream');
4+
const assert = require('assert');
5+
6+
const readable = new stream.Readable();
7+
8+
assert.throws(() => readable.read(), /not implemented/);

0 commit comments

Comments
 (0)