11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_cirequire('../common'); 31cb0ef41Sopenharmony_ciconst assert = require('assert'); 41cb0ef41Sopenharmony_ciconst stream = require('stream'); 51cb0ef41Sopenharmony_ciconst Writable = stream.Writable; 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_ci// Test the buffering behavior of Writable streams. 81cb0ef41Sopenharmony_ci// 91cb0ef41Sopenharmony_ci// The call to cork() triggers storing chunks which are flushed 101cb0ef41Sopenharmony_ci// on calling uncork() in the same tick. 111cb0ef41Sopenharmony_ci// 121cb0ef41Sopenharmony_ci// node version target: 0.12 131cb0ef41Sopenharmony_ci 141cb0ef41Sopenharmony_ciconst expectedChunks = ['please', 'buffer', 'me', 'kindly']; 151cb0ef41Sopenharmony_ciconst inputChunks = expectedChunks.slice(0); 161cb0ef41Sopenharmony_cilet seenChunks = []; 171cb0ef41Sopenharmony_cilet seenEnd = false; 181cb0ef41Sopenharmony_ci 191cb0ef41Sopenharmony_ciconst w = new Writable(); 201cb0ef41Sopenharmony_ci// Let's arrange to store the chunks. 211cb0ef41Sopenharmony_ciw._write = function(chunk, encoding, cb) { 221cb0ef41Sopenharmony_ci // Default encoding given none was specified. 231cb0ef41Sopenharmony_ci assert.strictEqual(encoding, 'buffer'); 241cb0ef41Sopenharmony_ci 251cb0ef41Sopenharmony_ci seenChunks.push(chunk); 261cb0ef41Sopenharmony_ci cb(); 271cb0ef41Sopenharmony_ci}; 281cb0ef41Sopenharmony_ci// Let's record the stream end event. 291cb0ef41Sopenharmony_ciw.on('finish', () => { 301cb0ef41Sopenharmony_ci seenEnd = true; 311cb0ef41Sopenharmony_ci}); 321cb0ef41Sopenharmony_ci 331cb0ef41Sopenharmony_cifunction writeChunks(remainingChunks, callback) { 341cb0ef41Sopenharmony_ci const writeChunk = remainingChunks.shift(); 351cb0ef41Sopenharmony_ci let writeState; 361cb0ef41Sopenharmony_ci 371cb0ef41Sopenharmony_ci if (writeChunk) { 381cb0ef41Sopenharmony_ci setImmediate(() => { 391cb0ef41Sopenharmony_ci writeState = w.write(writeChunk); 401cb0ef41Sopenharmony_ci // We were not told to stop writing. 411cb0ef41Sopenharmony_ci assert.ok(writeState); 421cb0ef41Sopenharmony_ci 431cb0ef41Sopenharmony_ci writeChunks(remainingChunks, callback); 441cb0ef41Sopenharmony_ci }); 451cb0ef41Sopenharmony_ci } else { 461cb0ef41Sopenharmony_ci callback(); 471cb0ef41Sopenharmony_ci } 481cb0ef41Sopenharmony_ci} 491cb0ef41Sopenharmony_ci 501cb0ef41Sopenharmony_ci// Do an initial write. 511cb0ef41Sopenharmony_ciw.write('stuff'); 521cb0ef41Sopenharmony_ci// The write was immediate. 531cb0ef41Sopenharmony_ciassert.strictEqual(seenChunks.length, 1); 541cb0ef41Sopenharmony_ci// Reset the chunks seen so far. 551cb0ef41Sopenharmony_ciseenChunks = []; 561cb0ef41Sopenharmony_ci 571cb0ef41Sopenharmony_ci// Trigger stream buffering. 581cb0ef41Sopenharmony_ciw.cork(); 591cb0ef41Sopenharmony_ci 601cb0ef41Sopenharmony_ci// Write the bufferedChunks. 611cb0ef41Sopenharmony_ciwriteChunks(inputChunks, () => { 621cb0ef41Sopenharmony_ci // Should not have seen anything yet. 631cb0ef41Sopenharmony_ci assert.strictEqual(seenChunks.length, 0); 641cb0ef41Sopenharmony_ci 651cb0ef41Sopenharmony_ci // Trigger writing out the buffer. 661cb0ef41Sopenharmony_ci w.uncork(); 671cb0ef41Sopenharmony_ci 681cb0ef41Sopenharmony_ci // Buffered bytes should be seen in current tick. 691cb0ef41Sopenharmony_ci assert.strictEqual(seenChunks.length, 4); 701cb0ef41Sopenharmony_ci 711cb0ef41Sopenharmony_ci // Did the chunks match. 721cb0ef41Sopenharmony_ci for (let i = 0, l = expectedChunks.length; i < l; i++) { 731cb0ef41Sopenharmony_ci const seen = seenChunks[i]; 741cb0ef41Sopenharmony_ci // There was a chunk. 751cb0ef41Sopenharmony_ci assert.ok(seen); 761cb0ef41Sopenharmony_ci 771cb0ef41Sopenharmony_ci const expected = Buffer.from(expectedChunks[i]); 781cb0ef41Sopenharmony_ci // It was what we expected. 791cb0ef41Sopenharmony_ci assert.ok(seen.equals(expected)); 801cb0ef41Sopenharmony_ci } 811cb0ef41Sopenharmony_ci 821cb0ef41Sopenharmony_ci setImmediate(() => { 831cb0ef41Sopenharmony_ci // The stream should not have been ended. 841cb0ef41Sopenharmony_ci assert.ok(!seenEnd); 851cb0ef41Sopenharmony_ci }); 861cb0ef41Sopenharmony_ci}); 87