GeezerVison is showing github.com — stream: fix disparity between buffer and the count · nodejs/node@b6ce918
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: lib/_stream_writable.js +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -429,6 +429,7 @@ function clearBuffer(stream, state) { |
429 | 429 | } else { |
430 | 430 | state.corkedRequestsFree = new CorkedRequest(state); |
431 | 431 | } |
| 432 | + state.bufferedRequestCount = 0; |
432 | 433 | } else { |
433 | 434 | // Slow case, write chunks one-by-one |
434 | 435 | while (entry) { |
@@ -439,6 +440,7 @@ function clearBuffer(stream, state) { |
439 | 440 |
|
440 | 441 | doWrite(stream, state, false, len, chunk, encoding, cb); |
441 | 442 | entry = entry.next; |
| 443 | + state.bufferedRequestCount--; |
442 | 444 | // if we didn't call the onwrite immediately, then |
443 | 445 | // it means that we need to wait until it does. |
444 | 446 | // also, that means that the chunk and cb are currently |
@@ -452,7 +454,6 @@ function clearBuffer(stream, state) { |
452 | 454 | state.lastBufferedRequest = null; |
453 | 455 | } |
454 | 456 |
|
455 | | - state.bufferedRequestCount = 0; |
456 | 457 | state.bufferedRequest = entry; |
457 | 458 | state.bufferProcessing = false; |
458 | 459 | } |
|
Collapse file
Copy file name to clipboard +34Lines changed: 34 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +'use strict'; |
| 2 | +const common = require('../common'); |
| 3 | +const Stream = require('stream'); |
| 4 | +// This test ensures that the _writeableState.bufferedRequestCount and |
| 5 | +// the actual buffered request count are the same |
| 6 | +const assert = require('assert'); |
| 7 | + |
| 8 | +class StreamWritable extends Stream.Writable { |
| 9 | + constructor() { |
| 10 | + super({ objectMode: true }); |
| 11 | + } |
| 12 | + |
| 13 | + // We need a timeout like on the original issue thread |
| 14 | + // otherwise the code will never reach our test case |
| 15 | + // this means this should go on the sequential folder. |
| 16 | + _write(chunk, encoding, cb) { |
| 17 | + setTimeout(cb, common.platformTimeout(10)); |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +const testStream = new StreamWritable(); |
| 22 | +testStream.cork(); |
| 23 | + |
| 24 | +for (let i = 1; i <= 5; i++) { |
| 25 | + testStream.write(i, function() { |
| 26 | + assert.strictEqual( |
| 27 | + testStream._writableState.bufferedRequestCount, |
| 28 | + testStream._writableState.getBuffer().length, |
| 29 | + 'bufferedRequestCount variable is different from the actual length of' + |
| 30 | + ' the buffer'); |
| 31 | + }); |
| 32 | +} |
| 33 | + |
| 34 | +testStream.end(); |