Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stream: add a test case for the underlying cause. #18575

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}