Skip to content

Commit 96babb2

Browse files
shmugaitaloacasas
authored andcommitted
test: tests for _readableStream.awaitDrain
Fixes: #8684 PR-URL: #8914 Reviewed-By: Matteo Collina <[email protected]>
1 parent 74f9cc9 commit 96babb2

3 files changed

+55
-2
lines changed

test/parallel/test-stream-pipe-await-drain-manual-resume.js

+16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22
const common = require('../common');
33
const stream = require('stream');
4+
const assert = require('assert');
45

56
// A consumer stream with a very low highWaterMark, which starts in a state
67
// where it buffers the chunk it receives rather than indicating that they
@@ -26,6 +27,11 @@ const readable = new stream.Readable({
2627
readable.pipe(writable);
2728

2829
readable.once('pause', common.mustCall(() => {
30+
assert.strictEqual(
31+
readable._readableState.awaitDrain,
32+
1,
33+
'awaitDrain doesn\'t increase'
34+
);
2935
// First pause, resume manually. The next write() to writable will still
3036
// return false, because chunks are still being buffered, so it will increase
3137
// the awaitDrain counter again.
@@ -34,6 +40,11 @@ readable.once('pause', common.mustCall(() => {
3440
}));
3541

3642
readable.once('pause', common.mustCall(() => {
43+
assert.strictEqual(
44+
readable._readableState.awaitDrain,
45+
1,
46+
'.resume() does not reset counter'
47+
);
3748
// Second pause, handle all chunks from now on. Once all callbacks that
3849
// are currently queued up are handled, the awaitDrain drain counter should
3950
// fall back to 0 and all chunks that are pending on the readable side
@@ -50,5 +61,10 @@ readable.push(Buffer.alloc(100)); // Should get through to the writable.
5061
readable.push(null);
5162

5263
writable.on('finish', common.mustCall(() => {
64+
assert.strictEqual(
65+
readable._readableState.awaitDrain,
66+
0,
67+
'awaitDrain not 0 after all chunks are written'
68+
);
5369
// Everything okay, all chunks were written.
5470
}));

test/parallel/test-stream-pipe-await-drain-push-while-write.js

+20-2
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,34 @@
11
'use strict';
22
const common = require('../common');
33
const stream = require('stream');
4+
const assert = require('assert');
5+
6+
const awaitDrainStates = [
7+
1, // after first chunk before callback
8+
1, // after second chunk before callback
9+
0 // resolving chunk pushed after first chunk, awaitDrain is decreased
10+
];
411

512
// A writable stream which pushes data onto the stream which pipes into it,
613
// but only the first time it's written to. Since it's not paused at this time,
714
// a second write will occur. If the pipe increases awaitDrain twice, we'll
815
// never get subsequent chunks because 'drain' is only emitted once.
916
const writable = new stream.Writable({
10-
write: common.mustCall((chunk, encoding, cb) => {
17+
write: common.mustCall(function(chunk, encoding, cb) {
1118
if (chunk.length === 32 * 1024) { // first chunk
12-
readable.push(new Buffer(33 * 1024)); // above hwm
19+
const beforePush = readable._readableState.awaitDrain;
20+
readable.push(new Buffer(34 * 1024)); // above hwm
21+
// We should check if awaitDrain counter is increased.
22+
const afterPush = readable._readableState.awaitDrain;
23+
assert.strictEqual(afterPush - beforePush, 1,
24+
'Counter is not increased for awaitDrain');
1325
}
26+
27+
assert.strictEqual(
28+
awaitDrainStates.shift(),
29+
readable._readableState.awaitDrain,
30+
'State variable awaitDrain is not correct.'
31+
);
1432
cb();
1533
}, 3)
1634
});

test/parallel/test-stream-pipe-await-drain.js

+19
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
'use strict';
22
const common = require('../common');
33
const stream = require('stream');
4+
const assert = require('assert');
45

56
// This is very similar to test-stream-pipe-cleanup-pause.js.
67

78
const reader = new stream.Readable();
89
const writer1 = new stream.Writable();
910
const writer2 = new stream.Writable();
11+
const writer3 = new stream.Writable();
1012

1113
// 560000 is chosen here because it is larger than the (default) highWaterMark
1214
// and will cause `.write()` to return false
@@ -19,7 +21,10 @@ writer1._write = common.mustCall(function(chunk, encoding, cb) {
1921
this.emit('chunk-received');
2022
cb();
2123
}, 1);
24+
2225
writer1.once('chunk-received', function() {
26+
assert.strictEqual(reader._readableState.awaitDrain, 0,
27+
'initial value is not 0');
2328
setImmediate(function() {
2429
// This one should *not* get through to writer1 because writer2 is not
2530
// "done" processing.
@@ -29,12 +34,26 @@ writer1.once('chunk-received', function() {
2934

3035
// A "slow" consumer:
3136
writer2._write = common.mustCall(function(chunk, encoding, cb) {
37+
assert.strictEqual(
38+
reader._readableState.awaitDrain, 1,
39+
'awaitDrain isn\'t 1 after first push'
40+
);
3241
// Not calling cb here to "simulate" slow stream.
42+
// This should be called exactly once, since the first .write() call
43+
// will return false.
44+
}, 1);
3345

46+
writer3._write = common.mustCall(function(chunk, encoding, cb) {
47+
assert.strictEqual(
48+
reader._readableState.awaitDrain, 2,
49+
'awaitDrain isn\'t 2 after second push'
50+
);
51+
// Not calling cb here to "simulate" slow stream.
3452
// This should be called exactly once, since the first .write() call
3553
// will return false.
3654
}, 1);
3755

3856
reader.pipe(writer1);
3957
reader.pipe(writer2);
58+
reader.pipe(writer3);
4059
reader.push(buffer);

0 commit comments

Comments
 (0)