Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 544920f

Browse filesBrowse files
chmlnMylesBorins
authored andcommitted
test: stream readableState readingMore state
PR-URL: #9868 Refs: #8685 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Italo A. Casas <me@italoacasas.com>
1 parent d8b902f commit 544920f
Copy full SHA for 544920f

File tree

Expand file treeCollapse file tree

1 file changed

+65
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+65
-0
lines changed
Open diff view settings
Collapse file
+65Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const Readable = require('stream').Readable;
5+
6+
const readable = new Readable({
7+
read(size) {}
8+
});
9+
10+
const state = readable._readableState;
11+
12+
// Starting off with false initially.
13+
assert.strictEqual(state.reading, false);
14+
assert.strictEqual(state.readingMore, false);
15+
16+
readable.on('data', common.mustCall((data) => {
17+
// while in a flowing state, should try to read more.
18+
if (state.flowing)
19+
assert.strictEqual(state.readingMore, true);
20+
21+
// reading as long as we've not ended
22+
assert.strictEqual(state.reading, !state.ended);
23+
}, 2));
24+
25+
function onStreamEnd() {
26+
// End of stream; state.reading is false
27+
// And so should be readingMore.
28+
assert.strictEqual(state.readingMore, false);
29+
assert.strictEqual(state.reading, false);
30+
}
31+
32+
readable.on('readable', common.mustCall(() => {
33+
// 'readable' always gets called before 'end'
34+
// since 'end' hasn't been emitted, more data could be incoming
35+
assert.strictEqual(state.readingMore, true);
36+
37+
// if the stream has ended, we shouldn't be reading
38+
assert.strictEqual(state.ended, !state.reading);
39+
40+
if (readable.read() === null) // reached end of stream
41+
process.nextTick(common.mustCall(onStreamEnd, 1));
42+
}, 2));
43+
44+
readable.on('end', common.mustCall(onStreamEnd));
45+
46+
readable.push('pushed');
47+
48+
// stop emitting 'data' events
49+
readable.pause();
50+
51+
// read() should only be called while operating in paused mode
52+
readable.read(6);
53+
54+
// reading
55+
assert.strictEqual(state.reading, true);
56+
assert.strictEqual(state.readingMore, true);
57+
58+
// resume emitting 'data' events
59+
readable.resume();
60+
61+
// add chunk to front
62+
readable.unshift('unshifted');
63+
64+
// end
65+
readable.push(null);

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.