diff options
author | Mathias Buus <mathiasbuus@gmail.com> | 2018-02-03 10:29:43 +0100 |
---|---|---|
committer | Matteo Collina <hello@matteocollina.com> | 2018-02-06 09:54:48 +0100 |
commit | 563fff2938110065589a3dc1b34692670206e4e4 (patch) | |
tree | 28bc10da4ac24ab779f5d4d1775f0daa3d715367 /test/parallel/test-stream-readable-pause-and-resume.js | |
parent | bc51428bf8311f307ef38085d4567d4ba3be3cb0 (diff) | |
download | android-node-v8-563fff2938110065589a3dc1b34692670206e4e4.tar.gz android-node-v8-563fff2938110065589a3dc1b34692670206e4e4.tar.bz2 android-node-v8-563fff2938110065589a3dc1b34692670206e4e4.zip |
stream: defer readable and flow when sync
PR-URL: https://github.com/nodejs/node/pull/18515
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'test/parallel/test-stream-readable-pause-and-resume.js')
-rw-r--r-- | test/parallel/test-stream-readable-pause-and-resume.js | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/test/parallel/test-stream-readable-pause-and-resume.js b/test/parallel/test-stream-readable-pause-and-resume.js new file mode 100644 index 0000000000..505327e247 --- /dev/null +++ b/test/parallel/test-stream-readable-pause-and-resume.js @@ -0,0 +1,40 @@ +'use strict'; + +const { Readable } = require('stream'); +const common = require('../common'); + +let ticks = 18; +let expectedData = 19; + +const rs = new Readable({ + objectMode: true, + read: () => { + if (ticks-- > 0) + return process.nextTick(() => rs.push({})); + rs.push({}); + rs.push(null); + } +}); + +rs.on('end', common.mustCall()); +readAndPause(); + +function readAndPause() { + // Does a on(data) -> pause -> wait -> resume -> on(data) ... loop. + // Expects on(data) to never fire if the stream is paused. + const ondata = common.mustCall((data) => { + rs.pause(); + + expectedData--; + if (expectedData <= 0) + return; + + setImmediate(function() { + rs.removeListener('data', ondata); + readAndPause(); + rs.resume(); + }); + }, 1); // only call ondata once + + rs.on('data', ondata); +} |