| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,91 @@ | |||
| 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 | + }); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,86 @@ | |||
| 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 uncork() in the same tick. | ||
| 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 | + // default encoding given none was specified | ||
| 23 | + assert.equal(encoding, 'buffer'); | ||
| 24 | + | ||
| 25 | + seenChunks.push(chunk); | ||
| 26 | + cb(); | ||
| 27 | + }; | ||
| 28 | + // lets record the stream end event | ||
| 29 | + w.on('finish', () => { | ||
| 30 | + seenEnd = true; | ||
| 31 | + }); | ||
| 32 | + | ||
| 33 | + function writeChunks(remainingChunks, callback) { | ||
| 34 | + var writeChunk = remainingChunks.shift(); | ||
| 35 | + var writeState; | ||
| 36 | + | ||
| 37 | + if (writeChunk) { | ||
| 38 | + setImmediate(() => { | ||
| 39 | + writeState = w.write(writeChunk); | ||
| 40 | + // we were not told to stop writing | ||
| 41 | + assert.ok(writeState); | ||
| 42 | + | ||
| 43 | + writeChunks(remainingChunks, callback); | ||
| 44 | + }); | ||
| 45 | + } else { | ||
| 46 | + callback(); | ||
| 47 | + } | ||
| 48 | + } | ||
| 49 | + | ||
| 50 | + // do an initial write | ||
| 51 | + w.write('stuff'); | ||
| 52 | + // the write was immediate | ||
| 53 | + assert.equal(seenChunks.length, 1); | ||
| 54 | + // reset the chunks seen so far | ||
| 55 | + seenChunks = []; | ||
| 56 | + | ||
| 57 | + // trigger stream buffering | ||
| 58 | + w.cork(); | ||
| 59 | + | ||
| 60 | + // write the bufferedChunks | ||
| 61 | + writeChunks(inputChunks, () => { | ||
| 62 | + // should not have seen anything yet | ||
| 63 | + assert.equal(seenChunks.length, 0); | ||
| 64 | + | ||
| 65 | + // trigger writing out the buffer | ||
| 66 | + w.uncork(); | ||
| 67 | + | ||
| 68 | + // buffered bytes shoud be seen in current tick | ||
| 69 | + assert.equal(seenChunks.length, 4); | ||
| 70 | + | ||
| 71 | + // did the chunks match | ||
| 72 | + for (var i = 0, l = expectedChunks.length; i < l; i++) { | ||
| 73 | + var seen = seenChunks[i]; | ||
| 74 | + // there was a chunk | ||
| 75 | + assert.ok(seen); | ||
| 76 | + | ||
| 77 | + var expected = new Buffer(expectedChunks[i]); | ||
| 78 | + // it was what we expected | ||
| 79 | + assert.ok(seen.equals(expected)); | ||
| 80 | + } | ||
| 81 | + | ||
| 82 | + setImmediate(() => { | ||
| 83 | + // the stream should not have been ended | ||
| 84 | + assert.ok(!seenEnd); | ||
| 85 | + }); | ||
| 86 | + }); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments