Skip to content

Commit 28ffd59

Browse files
joyeecheungitaloacasas
authored andcommitted
stream, test: test _readableState.emittedReadable
Part of #8683, increase coverage of the internal state machine of streams. PR-URL: #10249 See: #8683 See: #10230 Reviewed-By: Matteo Collina <[email protected]>
1 parent fb297cb commit 28ffd59

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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: () => {}
8+
});
9+
10+
// Initialized to false.
11+
assert.strictEqual(readable._readableState.emittedReadable, false);
12+
13+
readable.on('readable', common.mustCall(() => {
14+
// emittedReadable should be true when the readable event is emitted
15+
assert.strictEqual(readable._readableState.emittedReadable, true);
16+
readable.read();
17+
// emittedReadable is reset to false during read()
18+
assert.strictEqual(readable._readableState.emittedReadable, false);
19+
}, 4));
20+
21+
// When the first readable listener is just attached,
22+
// emittedReadable should be false
23+
assert.strictEqual(readable._readableState.emittedReadable, false);
24+
25+
// Each one of these should trigger a readable event.
26+
process.nextTick(common.mustCall(() => {
27+
readable.push('foo');
28+
}));
29+
process.nextTick(common.mustCall(() => {
30+
readable.push('bar');
31+
}));
32+
process.nextTick(common.mustCall(() => {
33+
readable.push('quo');
34+
}));
35+
process.nextTick(common.mustCall(() => {
36+
readable.push(null);
37+
}));
38+
39+
const noRead = new Readable({
40+
read: () => {}
41+
});
42+
43+
noRead.on('readable', common.mustCall(() => {
44+
// emittedReadable should be true when the readable event is emitted
45+
assert.strictEqual(noRead._readableState.emittedReadable, true);
46+
noRead.read(0);
47+
// emittedReadable is not reset during read(0)
48+
assert.strictEqual(noRead._readableState.emittedReadable, true);
49+
}));
50+
51+
noRead.push('foo');
52+
noRead.push(null);
53+
54+
const flowing = new Readable({
55+
read: () => {}
56+
});
57+
58+
flowing.on('data', common.mustCall(() => {
59+
// When in flowing mode, emittedReadable is always false.
60+
assert.strictEqual(flowing._readableState.emittedReadable, false);
61+
flowing.read();
62+
assert.strictEqual(flowing._readableState.emittedReadable, false);
63+
}, 3));
64+
65+
flowing.push('foooo');
66+
flowing.push('bar');
67+
flowing.push('quo');
68+
process.nextTick(common.mustCall(() => {
69+
flowing.push(null);
70+
}));

0 commit comments

Comments
 (0)