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 end() and the stream subsequently ended.
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  // Stream end event is not seen before the last write.
231cb0ef41Sopenharmony_ci  assert.ok(!seenEnd);
241cb0ef41Sopenharmony_ci  // Default encoding given none was specified.
251cb0ef41Sopenharmony_ci  assert.strictEqual(encoding, 'buffer');
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci  seenChunks.push(chunk);
281cb0ef41Sopenharmony_ci  cb();
291cb0ef41Sopenharmony_ci};
301cb0ef41Sopenharmony_ci// Let's record the stream end event.
311cb0ef41Sopenharmony_ciw.on('finish', () => {
321cb0ef41Sopenharmony_ci  seenEnd = true;
331cb0ef41Sopenharmony_ci});
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_cifunction writeChunks(remainingChunks, callback) {
361cb0ef41Sopenharmony_ci  const writeChunk = remainingChunks.shift();
371cb0ef41Sopenharmony_ci  let writeState;
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci  if (writeChunk) {
401cb0ef41Sopenharmony_ci    setImmediate(() => {
411cb0ef41Sopenharmony_ci      writeState = w.write(writeChunk);
421cb0ef41Sopenharmony_ci      // We were not told to stop writing.
431cb0ef41Sopenharmony_ci      assert.ok(writeState);
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci      writeChunks(remainingChunks, callback);
461cb0ef41Sopenharmony_ci    });
471cb0ef41Sopenharmony_ci  } else {
481cb0ef41Sopenharmony_ci    callback();
491cb0ef41Sopenharmony_ci  }
501cb0ef41Sopenharmony_ci}
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci// Do an initial write.
531cb0ef41Sopenharmony_ciw.write('stuff');
541cb0ef41Sopenharmony_ci// The write was immediate.
551cb0ef41Sopenharmony_ciassert.strictEqual(seenChunks.length, 1);
561cb0ef41Sopenharmony_ci// Reset the seen chunks.
571cb0ef41Sopenharmony_ciseenChunks = [];
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci// Trigger stream buffering.
601cb0ef41Sopenharmony_ciw.cork();
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci// Write the bufferedChunks.
631cb0ef41Sopenharmony_ciwriteChunks(inputChunks, () => {
641cb0ef41Sopenharmony_ci  // Should not have seen anything yet.
651cb0ef41Sopenharmony_ci  assert.strictEqual(seenChunks.length, 0);
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ci  // Trigger flush and ending the stream.
681cb0ef41Sopenharmony_ci  w.end();
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ci  // Stream should not ended in current tick.
711cb0ef41Sopenharmony_ci  assert.ok(!seenEnd);
721cb0ef41Sopenharmony_ci
731cb0ef41Sopenharmony_ci  // Buffered bytes should be seen in current tick.
741cb0ef41Sopenharmony_ci  assert.strictEqual(seenChunks.length, 4);
751cb0ef41Sopenharmony_ci
761cb0ef41Sopenharmony_ci  // Did the chunks match.
771cb0ef41Sopenharmony_ci  for (let i = 0, l = expectedChunks.length; i < l; i++) {
781cb0ef41Sopenharmony_ci    const seen = seenChunks[i];
791cb0ef41Sopenharmony_ci    // There was a chunk.
801cb0ef41Sopenharmony_ci    assert.ok(seen);
811cb0ef41Sopenharmony_ci
821cb0ef41Sopenharmony_ci    const expected = Buffer.from(expectedChunks[i]);
831cb0ef41Sopenharmony_ci    // It was what we expected.
841cb0ef41Sopenharmony_ci    assert.ok(seen.equals(expected));
851cb0ef41Sopenharmony_ci  }
861cb0ef41Sopenharmony_ci
871cb0ef41Sopenharmony_ci  setImmediate(() => {
881cb0ef41Sopenharmony_ci    // Stream should have ended in next tick.
891cb0ef41Sopenharmony_ci    assert.ok(seenEnd);
901cb0ef41Sopenharmony_ci  });
911cb0ef41Sopenharmony_ci});
92