test: stream readableState readingMore state
PR-URL: https://github.com/nodejs/node/pull/9868 Refs: https://github.com/nodejs/node/issues/8685 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Italo A. Casas <me@italoacasas.com>
This commit is contained in:
parent
8dbf1aff3a
commit
8621ccc25a
65
test/parallel/test-stream-readable-reading-readingMore.js
Normal file
65
test/parallel/test-stream-readable-reading-readingMore.js
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
'use strict';
|
||||||
|
const common = require('../common');
|
||||||
|
const assert = require('assert');
|
||||||
|
const Readable = require('stream').Readable;
|
||||||
|
|
||||||
|
const readable = new Readable({
|
||||||
|
read(size) {}
|
||||||
|
});
|
||||||
|
|
||||||
|
const state = readable._readableState;
|
||||||
|
|
||||||
|
// Starting off with false initially.
|
||||||
|
assert.strictEqual(state.reading, false);
|
||||||
|
assert.strictEqual(state.readingMore, false);
|
||||||
|
|
||||||
|
readable.on('data', common.mustCall((data) => {
|
||||||
|
// while in a flowing state, should try to read more.
|
||||||
|
if (state.flowing)
|
||||||
|
assert.strictEqual(state.readingMore, true);
|
||||||
|
|
||||||
|
// reading as long as we've not ended
|
||||||
|
assert.strictEqual(state.reading, !state.ended);
|
||||||
|
}, 2));
|
||||||
|
|
||||||
|
function onStreamEnd() {
|
||||||
|
// End of stream; state.reading is false
|
||||||
|
// And so should be readingMore.
|
||||||
|
assert.strictEqual(state.readingMore, false);
|
||||||
|
assert.strictEqual(state.reading, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
readable.on('readable', common.mustCall(() => {
|
||||||
|
// 'readable' always gets called before 'end'
|
||||||
|
// since 'end' hasn't been emitted, more data could be incoming
|
||||||
|
assert.strictEqual(state.readingMore, true);
|
||||||
|
|
||||||
|
// if the stream has ended, we shouldn't be reading
|
||||||
|
assert.strictEqual(state.ended, !state.reading);
|
||||||
|
|
||||||
|
if (readable.read() === null) // reached end of stream
|
||||||
|
process.nextTick(common.mustCall(onStreamEnd, 1));
|
||||||
|
}, 2));
|
||||||
|
|
||||||
|
readable.on('end', common.mustCall(onStreamEnd));
|
||||||
|
|
||||||
|
readable.push('pushed');
|
||||||
|
|
||||||
|
// stop emitting 'data' events
|
||||||
|
readable.pause();
|
||||||
|
|
||||||
|
// read() should only be called while operating in paused mode
|
||||||
|
readable.read(6);
|
||||||
|
|
||||||
|
// reading
|
||||||
|
assert.strictEqual(state.reading, true);
|
||||||
|
assert.strictEqual(state.readingMore, true);
|
||||||
|
|
||||||
|
// resume emitting 'data' events
|
||||||
|
readable.resume();
|
||||||
|
|
||||||
|
// add chunk to front
|
||||||
|
readable.unshift('unshifted');
|
||||||
|
|
||||||
|
// end
|
||||||
|
readable.push(null);
|
Loading…
x
Reference in New Issue
Block a user