Skip to content

Commit 86c659b

Browse files
陈刚mcollina
authored andcommitted
stream: add a test case for the underlying cause.
The original test case hides the underlying cause by using `PassThrough`. This change adds a test case for the underlying cause. This makes it clearer and easier to be understood. Refs: #18372 PR-URL: #18575 Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent 8204b0f commit 86c659b

File tree

1 file changed

+52
-29
lines changed

1 file changed

+52
-29
lines changed

test/parallel/test-stream-readable-no-unneeded-readable.js

Lines changed: 52 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,61 @@
22
const common = require('../common');
33
const { Readable, PassThrough } = require('stream');
44

5-
const source = new Readable({
6-
read: () => {}
7-
});
5+
function test(r) {
6+
const wrapper = new Readable({
7+
read: () => {
8+
let data = r.read();
89

9-
source.push('foo');
10-
source.push('bar');
11-
source.push(null);
12-
13-
const pt = source.pipe(new PassThrough());
14-
15-
const wrapper = new Readable({
16-
read: () => {
17-
let data = pt.read();
18-
19-
if (data) {
20-
wrapper.push(data);
21-
return;
22-
}
23-
24-
pt.once('readable', function() {
25-
data = pt.read();
2610
if (data) {
2711
wrapper.push(data);
12+
return;
2813
}
29-
// else the end event should fire
30-
});
31-
}
32-
});
3314

34-
pt.once('end', function() {
35-
wrapper.push(null);
36-
});
15+
r.once('readable', function() {
16+
data = r.read();
17+
if (data) {
18+
wrapper.push(data);
19+
}
20+
// else the end event should fire
21+
});
22+
},
23+
});
24+
25+
r.once('end', function() {
26+
wrapper.push(null);
27+
});
28+
29+
wrapper.resume();
30+
wrapper.once('end', common.mustCall());
31+
}
32+
33+
{
34+
const source = new Readable({
35+
read: () => {}
36+
});
37+
source.push('foo');
38+
source.push('bar');
39+
source.push(null);
40+
41+
const pt = source.pipe(new PassThrough());
42+
test(pt);
43+
}
44+
45+
{
46+
// This is the underlying cause of the above test case.
47+
const pushChunks = ['foo', 'bar'];
48+
const r = new Readable({
49+
read: () => {
50+
const chunk = pushChunks.shift();
51+
if (chunk) {
52+
// synchronous call
53+
r.push(chunk);
54+
} else {
55+
// asynchronous call
56+
process.nextTick(() => r.push(null));
57+
}
58+
},
59+
});
3760

38-
wrapper.resume();
39-
wrapper.once('end', common.mustCall());
61+
test(r);
62+
}

0 commit comments

Comments
 (0)