diff options
author | 陈刚 <chengang07@meituan.com> | 2018-02-05 16:33:42 +0800 |
---|---|---|
committer | Matteo Collina <hello@matteocollina.com> | 2018-02-08 16:31:47 +0100 |
commit | 86c659ba61dd923439335b240d81b38cab407acc (patch) | |
tree | 2bc21fc3e29007266ba104a6668caf4110dd5751 /test/parallel/test-stream-readable-no-unneeded-readable.js | |
parent | 8204b0f9c6dbbdba1ca4120698a7f87ca1c9d91c (diff) | |
download | android-node-v8-86c659ba61dd923439335b240d81b38cab407acc.tar.gz android-node-v8-86c659ba61dd923439335b240d81b38cab407acc.tar.bz2 android-node-v8-86c659ba61dd923439335b240d81b38cab407acc.zip |
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>
Diffstat (limited to 'test/parallel/test-stream-readable-no-unneeded-readable.js')
-rw-r--r-- | test/parallel/test-stream-readable-no-unneeded-readable.js | 81 |
1 files 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 bd3e06e5f7..13ee2b498c 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); +} |