Skip to content

Commit e2bf250

Browse files
addaleaxevanlucas
authored andcommitted
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 <matteo.collina@gmail.com>
1 parent 6aa179b commit e2bf250

4 files changed

Lines changed: 90 additions & 0 deletions
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict';
2+
require('../common');
3+
conststream=require('stream');
4+
constassert=require('assert');
5+
6+
constreadable=newstream.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);
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
'use strict';
2+
constcommon=require('../common');
3+
conststream=require('stream');
4+
constassert=require('assert');
5+
6+
constreadable=newstream.Readable({
7+
read: ()=>{}
8+
});
9+
10+
constwritables=[];
11+
12+
for(leti=0;i<5;i++){
13+
consttarget=newstream.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+
constinput=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(consttargetofwritables){
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(consttargetofwritables){
49+
assert.deepStrictEqual(target.output,[input]);
50+
}
51+
}));
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
require('../common');
3+
conststream=require('stream');
4+
constassert=require('assert');
5+
6+
constreadable=newstream.Readable({
7+
read: ()=>{}
8+
});
9+
10+
assert.throws(()=>readable.push([]),/Invalidnon-string\/bufferchunk/);
11+
assert.throws(()=>readable.push({}),/Invalidnon-string\/bufferchunk/);
12+
assert.throws(()=>readable.push(0),/Invalidnon-string\/bufferchunk/);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict';
2+
require('../common');
3+
conststream=require('stream');
4+
constassert=require('assert');
5+
6+
constreadable=newstream.Readable();
7+
8+
assert.throws(()=>readable.read(),/notimplemented/);

0 commit comments

Comments
 (0)