stream: add a test case for the underlying cause.

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: https://github.com/nodejs/node/pull/18372

PR-URL: https://github.com/nodejs/node/pull/18575
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
陈刚 2018-02-05 16:33:42 +08:00 committed by Matteo Collina
parent 8204b0f9c6
commit 86c659ba61

View File

@ -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
});
},
});
wrapper.resume();
wrapper.once('end', common.mustCall());
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));
}
},
});
test(r);
}