|
| 1 | +'use strict'; |
| 2 | +require('../common'); |
| 3 | +const assert = require('assert'); |
| 4 | +const stream = require('stream'); |
| 5 | +const Writable = stream.Writable; |
| 6 | + |
| 7 | +// Test the buffering behaviour of Writable streams. |
| 8 | +// |
| 9 | +// The call to cork() triggers storing chunks which are flushed |
| 10 | +// on calling end() and the stream subsequently ended. |
| 11 | +// |
| 12 | +// node version target: 0.12 |
| 13 | + |
| 14 | +const expectedChunks = ['please', 'buffer', 'me', 'kindly']; |
| 15 | +var inputChunks = expectedChunks.slice(0); |
| 16 | +var seenChunks = []; |
| 17 | +var seenEnd = false; |
| 18 | + |
| 19 | +var w = new Writable(); |
| 20 | +// lets arrange to store the chunks |
| 21 | +w._write = function(chunk, encoding, cb) { |
| 22 | + // stream end event is not seen before the last write |
| 23 | + assert.ok(!seenEnd); |
| 24 | + // default encoding given none was specified |
| 25 | + assert.equal(encoding, 'buffer'); |
| 26 | + |
| 27 | + seenChunks.push(chunk); |
| 28 | + cb(); |
| 29 | +}; |
| 30 | +// lets record the stream end event |
| 31 | +w.on('finish', () => { |
| 32 | + seenEnd = true; |
| 33 | +}); |
| 34 | + |
| 35 | +function writeChunks(remainingChunks, callback) { |
| 36 | + var writeChunk = remainingChunks.shift(); |
| 37 | + var writeState; |
| 38 | + |
| 39 | + if (writeChunk) { |
| 40 | + setImmediate(() => { |
| 41 | + writeState = w.write(writeChunk); |
| 42 | + // we were not told to stop writing |
| 43 | + assert.ok(writeState); |
| 44 | + |
| 45 | + writeChunks(remainingChunks, callback); |
| 46 | + }); |
| 47 | + } else { |
| 48 | + callback(); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +// do an initial write |
| 53 | +w.write('stuff'); |
| 54 | +// the write was immediate |
| 55 | +assert.equal(seenChunks.length, 1); |
| 56 | +// reset the seen chunks |
| 57 | +seenChunks = []; |
| 58 | + |
| 59 | +// trigger stream buffering |
| 60 | +w.cork(); |
| 61 | + |
| 62 | +// write the bufferedChunks |
| 63 | +writeChunks(inputChunks, () => { |
| 64 | + // should not have seen anything yet |
| 65 | + assert.equal(seenChunks.length, 0); |
| 66 | + |
| 67 | + // trigger flush and ending the stream |
| 68 | + w.end(); |
| 69 | + |
| 70 | + // stream should not ended in current tick |
| 71 | + assert.ok(!seenEnd); |
| 72 | + |
| 73 | + // buffered bytes should be seen in current tick |
| 74 | + assert.equal(seenChunks.length, 4); |
| 75 | + |
| 76 | + // did the chunks match |
| 77 | + for (var i = 0, l = expectedChunks.length; i < l; i++) { |
| 78 | + var seen = seenChunks[i]; |
| 79 | + // there was a chunk |
| 80 | + assert.ok(seen); |
| 81 | + |
| 82 | + var expected = new Buffer(expectedChunks[i]); |
| 83 | + // it was what we expected |
| 84 | + assert.ok(seen.equals(expected)); |
| 85 | + } |
| 86 | + |
| 87 | + setImmediate(() => { |
| 88 | + // stream should have ended in next tick |
| 89 | + assert.ok(seenEnd); |
| 90 | + }); |
| 91 | +}); |
0 commit comments