Skip to content

Commit

Permalink
stream: add a test case for the underlying cause.
Browse files Browse the repository at this point in the history
The original test case hides the underlying cause by using
`PassThrough`. This change adds a test case for the underlying cause.
This makes it clearer and easier to be understood.

Refs: nodejs#18372

PR-URL: nodejs#18575
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
  • Loading branch information
陈刚 authored and MayaLekova committed May 8, 2018
1 parent e8850ee commit 7513eec
Showing 1 changed file with 52 additions and 29 deletions.
81 changes: 52 additions & 29 deletions test/parallel/test-stream-readable-no-unneeded-readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,61 @@
const common = require('../common');
const { Readable, PassThrough } = require('stream');

const source = new Readable({
read: () => {}
});
function test(r) {
const wrapper = new Readable({
read: () => {
let data = r.read();

source.push('foo');
source.push('bar');
source.push(null);

const pt = source.pipe(new PassThrough());

const wrapper = new Readable({
read: () => {
let data = pt.read();

if (data) {
wrapper.push(data);
return;
}

pt.once('readable', function() {
data = pt.read();
if (data) {
wrapper.push(data);
return;
}
// else the end event should fire
});
}
});

pt.once('end', function() {
wrapper.push(null);
});
r.once('readable', function() {
data = r.read();
if (data) {
wrapper.push(data);
}
// else the end event should fire
});
},
});

r.once('end', function() {
wrapper.push(null);
});

wrapper.resume();
wrapper.once('end', common.mustCall());
}

{
const source = new Readable({
read: () => {}
});
source.push('foo');
source.push('bar');
source.push(null);

const pt = source.pipe(new PassThrough());
test(pt);
}

{
// This is the underlying cause of the above test case.
const pushChunks = ['foo', 'bar'];
const r = new Readable({
read: () => {
const chunk = pushChunks.shift();
if (chunk) {
// synchronous call
r.push(chunk);
} else {
// asynchronous call
process.nextTick(() => r.push(null));
}
},
});

wrapper.resume();
wrapper.once('end', common.mustCall());
test(r);
}

0 comments on commit 7513eec

Please sign in to comment.