From 86c659ba61dd923439335b240d81b38cab407acc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=88=9A?= Date: Mon, 5 Feb 2018 16:33:42 +0800 Subject: [PATCH] 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 --- ...st-stream-readable-no-unneeded-readable.js | 81 ++++++++++++------- 1 file changed, 52 insertions(+), 29 deletions(-) diff --git a/test/parallel/test-stream-readable-no-unneeded-readable.js b/test/parallel/test-stream-readable-no-unneeded-readable.js index bd3e06e5f7d55c..13ee2b498c4b96 100644 --- a/test/parallel/test-stream-readable-no-unneeded-readable.js +++ b/test/parallel/test-stream-readable-no-unneeded-readable.js @@ -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); +}