Skip to content

Commit

Permalink
stream: add failing pause -> resume -> pause test
Browse files Browse the repository at this point in the history
  • Loading branch information
mafintosh committed Feb 3, 2018
1 parent 1f480f6 commit 591a1c5
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions test/parallel/test-stream-readable-pause-and-resume.js
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 591a1c5

Please sign in to comment.