| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,6 +1,7 @@ | |||
| 1 | 1 | 'use strict'; | |
| 2 | 2 | const common = require('../common'); | |
| 3 | 3 | const stream = require('stream'); | |
| 4 | + const assert = require('assert'); | ||
| 4 | 5 | ||
| 5 | 6 | // A consumer stream with a very low highWaterMark, which starts in a state | |
| 6 | 7 | // where it buffers the chunk it receives rather than indicating that they | |
@@ -26,6 +27,11 @@ const readable = new stream.Readable({ | |||
| 26 | 27 | readable.pipe(writable); | |
| 27 | 28 | ||
| 28 | 29 | readable.once('pause', common.mustCall(() => { | |
| 30 | + assert.strictEqual( | ||
| 31 | + readable._readableState.awaitDrain, | ||
| 32 | + 1, | ||
| 33 | + 'awaitDrain doesn\'t increase' | ||
| 34 | + ); | ||
| 29 | 35 | // First pause, resume manually. The next write() to writable will still | |
| 30 | 36 | // return false, because chunks are still being buffered, so it will increase | |
| 31 | 37 | // the awaitDrain counter again. | |
@@ -34,6 +40,11 @@ readable.once('pause', common.mustCall(() => { | |||
| 34 | 40 | })); | |
| 35 | 41 | ||
| 36 | 42 | readable.once('pause', common.mustCall(() => { | |
| 43 | + assert.strictEqual( | ||
| 44 | + readable._readableState.awaitDrain, | ||
| 45 | + 1, | ||
| 46 | + '.resume() does not reset counter' | ||
| 47 | + ); | ||
| 37 | 48 | // Second pause, handle all chunks from now on. Once all callbacks that | |
| 38 | 49 | // are currently queued up are handled, the awaitDrain drain counter should | |
| 39 | 50 | // 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. | |||
| 50 | 61 | readable.push(null); | |
| 51 | 62 | ||
| 52 | 63 | writable.on('finish', common.mustCall(() => { | |
| 64 | + assert.strictEqual( | ||
| 65 | + readable._readableState.awaitDrain, | ||
| 66 | + 0, | ||
| 67 | + 'awaitDrain not 0 after all chunks are written' | ||
| 68 | + ); | ||
| 53 | 69 | // Everything okay, all chunks were written. | |
| 54 | 70 | })); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,16 +1,34 @@ | |||
| 1 | 1 | 'use strict'; | |
| 2 | 2 | const common = require('../common'); | |
| 3 | 3 | 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 | + ]; | ||
| 4 | 11 | ||
| 5 | 12 | // A writable stream which pushes data onto the stream which pipes into it, | |
| 6 | 13 | // but only the first time it's written to. Since it's not paused at this time, | |
| 7 | 14 | // a second write will occur. If the pipe increases awaitDrain twice, we'll | |
| 8 | 15 | // never get subsequent chunks because 'drain' is only emitted once. | |
| 9 | 16 | const writable = new stream.Writable({ | |
| 10 | - write: common.mustCall((chunk, encoding, cb) => { | ||
| 17 | + write: common.mustCall(function(chunk, encoding, cb) { | ||
| 11 | 18 | 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'); | ||
| 13 | 25 | } | |
| 26 | + | ||
| 27 | + assert.strictEqual( | ||
| 28 | + awaitDrainStates.shift(), | ||
| 29 | + readable._readableState.awaitDrain, | ||
| 30 | + 'State variable awaitDrain is not correct.' | ||
| 31 | + ); | ||
| 14 | 32 | cb(); | |
| 15 | 33 | }, 3) | |
| 16 | 34 | }); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,12 +1,14 @@ | |||
| 1 | 1 | 'use strict'; | |
| 2 | 2 | const common = require('../common'); | |
| 3 | 3 | const stream = require('stream'); | |
| 4 | + const assert = require('assert'); | ||
| 4 | 5 | ||
| 5 | 6 | // This is very similar to test-stream-pipe-cleanup-pause.js. | |
| 6 | 7 | ||
| 7 | 8 | const reader = new stream.Readable(); | |
| 8 | 9 | const writer1 = new stream.Writable(); | |
| 9 | 10 | const writer2 = new stream.Writable(); | |
| 11 | + const writer3 = new stream.Writable(); | ||
| 10 | 12 | ||
| 11 | 13 | // 560000 is chosen here because it is larger than the (default) highWaterMark | |
| 12 | 14 | // and will cause `.write()` to return false | |
@@ -19,7 +21,10 @@ writer1._write = common.mustCall(function(chunk, encoding, cb) { | |||
| 19 | 21 | this.emit('chunk-received'); | |
| 20 | 22 | cb(); | |
| 21 | 23 | }, 1); | |
| 24 | + | ||
| 22 | 25 | writer1.once('chunk-received', function() { | |
| 26 | + assert.strictEqual(reader._readableState.awaitDrain, 0, | ||
| 27 | + 'initial value is not 0'); | ||
| 23 | 28 | setImmediate(function() { | |
| 24 | 29 | // This one should *not* get through to writer1 because writer2 is not | |
| 25 | 30 | // "done" processing. | |
@@ -29,12 +34,26 @@ writer1.once('chunk-received', function() { | |||
| 29 | 34 | ||
| 30 | 35 | // A "slow" consumer: | |
| 31 | 36 | 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 | + ); | ||
| 32 | 41 | // 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); | ||
| 33 | 45 | ||
| 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. | ||
| 34 | 52 | // This should be called exactly once, since the first .write() call | |
| 35 | 53 | // will return false. | |
| 36 | 54 | }, 1); | |
| 37 | 55 | ||
| 38 | 56 | reader.pipe(writer1); | |
| 39 | 57 | reader.pipe(writer2); | |
| 58 | + reader.pipe(writer3); | ||
| 40 | 59 | reader.push(buffer); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments