11cb0ef41Sopenharmony_ci// Copyright Joyent, Inc. and other Node contributors.
21cb0ef41Sopenharmony_ci//
31cb0ef41Sopenharmony_ci// Permission is hereby granted, free of charge, to any person obtaining a
41cb0ef41Sopenharmony_ci// copy of this software and associated documentation files (the
51cb0ef41Sopenharmony_ci// "Software"), to deal in the Software without restriction, including
61cb0ef41Sopenharmony_ci// without limitation the rights to use, copy, modify, merge, publish,
71cb0ef41Sopenharmony_ci// distribute, sublicense, and/or sell copies of the Software, and to permit
81cb0ef41Sopenharmony_ci// persons to whom the Software is furnished to do so, subject to the
91cb0ef41Sopenharmony_ci// following conditions:
101cb0ef41Sopenharmony_ci//
111cb0ef41Sopenharmony_ci// The above copyright notice and this permission notice shall be included
121cb0ef41Sopenharmony_ci// in all copies or substantial portions of the Software.
131cb0ef41Sopenharmony_ci//
141cb0ef41Sopenharmony_ci// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
151cb0ef41Sopenharmony_ci// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
161cb0ef41Sopenharmony_ci// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
171cb0ef41Sopenharmony_ci// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
181cb0ef41Sopenharmony_ci// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
191cb0ef41Sopenharmony_ci// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
201cb0ef41Sopenharmony_ci// USE OR OTHER DEALINGS IN THE SOFTWARE.
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci'use strict';
231cb0ef41Sopenharmony_ciconst common = require('../common');
241cb0ef41Sopenharmony_ciconst assert = require('assert');
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci// This test verifies that:
271cb0ef41Sopenharmony_ci// 1. unshift() does not cause colliding _read() calls.
281cb0ef41Sopenharmony_ci// 2. unshift() after the 'end' event is an error, but after the EOF
291cb0ef41Sopenharmony_ci//    signalling null, it is ok, and just creates a new readable chunk.
301cb0ef41Sopenharmony_ci// 3. push() after the EOF signaling null is an error.
311cb0ef41Sopenharmony_ci// 4. _read() is not called after pushing the EOF null chunk.
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ciconst stream = require('stream');
341cb0ef41Sopenharmony_ciconst hwm = 10;
351cb0ef41Sopenharmony_ciconst r = stream.Readable({ highWaterMark: hwm, autoDestroy: false });
361cb0ef41Sopenharmony_ciconst chunks = 10;
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ciconst data = Buffer.allocUnsafe(chunks * hwm + Math.ceil(hwm / 2));
391cb0ef41Sopenharmony_cifor (let i = 0; i < data.length; i++) {
401cb0ef41Sopenharmony_ci  const c = 'asdf'.charCodeAt(i % 4);
411cb0ef41Sopenharmony_ci  data[i] = c;
421cb0ef41Sopenharmony_ci}
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_cilet pos = 0;
451cb0ef41Sopenharmony_cilet pushedNull = false;
461cb0ef41Sopenharmony_cir._read = function(n) {
471cb0ef41Sopenharmony_ci  assert(!pushedNull, '_read after null push');
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci  // Every third chunk is fast
501cb0ef41Sopenharmony_ci  push(!(chunks % 3));
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci  function push(fast) {
531cb0ef41Sopenharmony_ci    assert(!pushedNull, 'push() after null push');
541cb0ef41Sopenharmony_ci    const c = pos >= data.length ? null : data.slice(pos, pos + n);
551cb0ef41Sopenharmony_ci    pushedNull = c === null;
561cb0ef41Sopenharmony_ci    if (fast) {
571cb0ef41Sopenharmony_ci      pos += n;
581cb0ef41Sopenharmony_ci      r.push(c);
591cb0ef41Sopenharmony_ci      if (c === null) pushError();
601cb0ef41Sopenharmony_ci    } else {
611cb0ef41Sopenharmony_ci      setTimeout(function() {
621cb0ef41Sopenharmony_ci        pos += n;
631cb0ef41Sopenharmony_ci        r.push(c);
641cb0ef41Sopenharmony_ci        if (c === null) pushError();
651cb0ef41Sopenharmony_ci      }, 1);
661cb0ef41Sopenharmony_ci    }
671cb0ef41Sopenharmony_ci  }
681cb0ef41Sopenharmony_ci};
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_cifunction pushError() {
711cb0ef41Sopenharmony_ci  r.unshift(Buffer.allocUnsafe(1));
721cb0ef41Sopenharmony_ci  w.end();
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_ci  assert.throws(() => {
751cb0ef41Sopenharmony_ci    r.push(Buffer.allocUnsafe(1));
761cb0ef41Sopenharmony_ci  }, {
771cb0ef41Sopenharmony_ci    code: 'ERR_STREAM_PUSH_AFTER_EOF',
781cb0ef41Sopenharmony_ci    name: 'Error',
791cb0ef41Sopenharmony_ci    message: 'stream.push() after EOF'
801cb0ef41Sopenharmony_ci  });
811cb0ef41Sopenharmony_ci}
821cb0ef41Sopenharmony_ci
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_ciconst w = stream.Writable();
851cb0ef41Sopenharmony_ciconst written = [];
861cb0ef41Sopenharmony_ciw._write = function(chunk, encoding, cb) {
871cb0ef41Sopenharmony_ci  written.push(chunk.toString());
881cb0ef41Sopenharmony_ci  cb();
891cb0ef41Sopenharmony_ci};
901cb0ef41Sopenharmony_ci
911cb0ef41Sopenharmony_cir.on('end', common.mustNotCall());
921cb0ef41Sopenharmony_ci
931cb0ef41Sopenharmony_cir.on('readable', function() {
941cb0ef41Sopenharmony_ci  let chunk;
951cb0ef41Sopenharmony_ci  while (null !== (chunk = r.read(10))) {
961cb0ef41Sopenharmony_ci    w.write(chunk);
971cb0ef41Sopenharmony_ci    if (chunk.length > 4)
981cb0ef41Sopenharmony_ci      r.unshift(Buffer.from('1234'));
991cb0ef41Sopenharmony_ci  }
1001cb0ef41Sopenharmony_ci});
1011cb0ef41Sopenharmony_ci
1021cb0ef41Sopenharmony_ciw.on('finish', common.mustCall(function() {
1031cb0ef41Sopenharmony_ci  // Each chunk should start with 1234, and then be asfdasdfasdf...
1041cb0ef41Sopenharmony_ci  // The first got pulled out before the first unshift('1234'), so it's
1051cb0ef41Sopenharmony_ci  // lacking that piece.
1061cb0ef41Sopenharmony_ci  assert.strictEqual(written[0], 'asdfasdfas');
1071cb0ef41Sopenharmony_ci  let asdf = 'd';
1081cb0ef41Sopenharmony_ci  console.error(`0: ${written[0]}`);
1091cb0ef41Sopenharmony_ci  for (let i = 1; i < written.length; i++) {
1101cb0ef41Sopenharmony_ci    console.error(`${i.toString(32)}: ${written[i]}`);
1111cb0ef41Sopenharmony_ci    assert.strictEqual(written[i].slice(0, 4), '1234');
1121cb0ef41Sopenharmony_ci    for (let j = 4; j < written[i].length; j++) {
1131cb0ef41Sopenharmony_ci      const c = written[i].charAt(j);
1141cb0ef41Sopenharmony_ci      assert.strictEqual(c, asdf);
1151cb0ef41Sopenharmony_ci      switch (asdf) {
1161cb0ef41Sopenharmony_ci        case 'a': asdf = 's'; break;
1171cb0ef41Sopenharmony_ci        case 's': asdf = 'd'; break;
1181cb0ef41Sopenharmony_ci        case 'd': asdf = 'f'; break;
1191cb0ef41Sopenharmony_ci        case 'f': asdf = 'a'; break;
1201cb0ef41Sopenharmony_ci      }
1211cb0ef41Sopenharmony_ci    }
1221cb0ef41Sopenharmony_ci  }
1231cb0ef41Sopenharmony_ci}));
1241cb0ef41Sopenharmony_ci
1251cb0ef41Sopenharmony_ciprocess.on('exit', function() {
1261cb0ef41Sopenharmony_ci  assert.strictEqual(written.length, 18);
1271cb0ef41Sopenharmony_ci  console.log('ok');
1281cb0ef41Sopenharmony_ci});
129