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_ci// Test that having a bunch of streams piping in parallel
241cb0ef41Sopenharmony_ci// doesn't break anything.
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_cirequire('../common');
271cb0ef41Sopenharmony_ciconst assert = require('assert');
281cb0ef41Sopenharmony_ciconst Stream = require('stream').Stream;
291cb0ef41Sopenharmony_ciconst rr = [];
301cb0ef41Sopenharmony_ciconst ww = [];
311cb0ef41Sopenharmony_ciconst cnt = 100;
321cb0ef41Sopenharmony_ciconst chunks = 1000;
331cb0ef41Sopenharmony_ciconst chunkSize = 250;
341cb0ef41Sopenharmony_ciconst data = Buffer.allocUnsafe(chunkSize);
351cb0ef41Sopenharmony_cilet wclosed = 0;
361cb0ef41Sopenharmony_cilet rclosed = 0;
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_cifunction FakeStream() {
391cb0ef41Sopenharmony_ci  Stream.apply(this);
401cb0ef41Sopenharmony_ci  this.wait = false;
411cb0ef41Sopenharmony_ci  this.writable = true;
421cb0ef41Sopenharmony_ci  this.readable = true;
431cb0ef41Sopenharmony_ci}
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ciFakeStream.prototype = Object.create(Stream.prototype);
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ciFakeStream.prototype.write = function(chunk) {
481cb0ef41Sopenharmony_ci  console.error(this.ID, 'write', this.wait);
491cb0ef41Sopenharmony_ci  if (this.wait) {
501cb0ef41Sopenharmony_ci    process.nextTick(this.emit.bind(this, 'drain'));
511cb0ef41Sopenharmony_ci  }
521cb0ef41Sopenharmony_ci  this.wait = !this.wait;
531cb0ef41Sopenharmony_ci  return this.wait;
541cb0ef41Sopenharmony_ci};
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ciFakeStream.prototype.end = function() {
571cb0ef41Sopenharmony_ci  this.emit('end');
581cb0ef41Sopenharmony_ci  process.nextTick(this.close.bind(this));
591cb0ef41Sopenharmony_ci};
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci// noop - closes happen automatically on end.
621cb0ef41Sopenharmony_ciFakeStream.prototype.close = function() {
631cb0ef41Sopenharmony_ci  this.emit('close');
641cb0ef41Sopenharmony_ci};
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ci// Expect all streams to close properly.
681cb0ef41Sopenharmony_ciprocess.on('exit', function() {
691cb0ef41Sopenharmony_ci  assert.strictEqual(wclosed, cnt);
701cb0ef41Sopenharmony_ci  assert.strictEqual(rclosed, cnt);
711cb0ef41Sopenharmony_ci});
721cb0ef41Sopenharmony_ci
731cb0ef41Sopenharmony_cifor (let i = 0; i < chunkSize; i++) {
741cb0ef41Sopenharmony_ci  data[i] = i;
751cb0ef41Sopenharmony_ci}
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_cifor (let i = 0; i < cnt; i++) {
781cb0ef41Sopenharmony_ci  const r = new FakeStream();
791cb0ef41Sopenharmony_ci  r.on('close', function() {
801cb0ef41Sopenharmony_ci    console.error(this.ID, 'read close');
811cb0ef41Sopenharmony_ci    rclosed++;
821cb0ef41Sopenharmony_ci  });
831cb0ef41Sopenharmony_ci  rr.push(r);
841cb0ef41Sopenharmony_ci
851cb0ef41Sopenharmony_ci  const w = new FakeStream();
861cb0ef41Sopenharmony_ci  w.on('close', function() {
871cb0ef41Sopenharmony_ci    console.error(this.ID, 'write close');
881cb0ef41Sopenharmony_ci    wclosed++;
891cb0ef41Sopenharmony_ci  });
901cb0ef41Sopenharmony_ci  ww.push(w);
911cb0ef41Sopenharmony_ci
921cb0ef41Sopenharmony_ci  r.ID = w.ID = i;
931cb0ef41Sopenharmony_ci  r.pipe(w);
941cb0ef41Sopenharmony_ci}
951cb0ef41Sopenharmony_ci
961cb0ef41Sopenharmony_ci// Now start passing through data.
971cb0ef41Sopenharmony_ci// Simulate a relatively fast async stream.
981cb0ef41Sopenharmony_cirr.forEach(function(r) {
991cb0ef41Sopenharmony_ci  let cnt = chunks;
1001cb0ef41Sopenharmony_ci  let paused = false;
1011cb0ef41Sopenharmony_ci
1021cb0ef41Sopenharmony_ci  r.on('pause', function() {
1031cb0ef41Sopenharmony_ci    paused = true;
1041cb0ef41Sopenharmony_ci  });
1051cb0ef41Sopenharmony_ci
1061cb0ef41Sopenharmony_ci  r.on('resume', function() {
1071cb0ef41Sopenharmony_ci    paused = false;
1081cb0ef41Sopenharmony_ci    step();
1091cb0ef41Sopenharmony_ci  });
1101cb0ef41Sopenharmony_ci
1111cb0ef41Sopenharmony_ci  function step() {
1121cb0ef41Sopenharmony_ci    r.emit('data', data);
1131cb0ef41Sopenharmony_ci    if (--cnt === 0) {
1141cb0ef41Sopenharmony_ci      r.end();
1151cb0ef41Sopenharmony_ci      return;
1161cb0ef41Sopenharmony_ci    }
1171cb0ef41Sopenharmony_ci    if (paused) return;
1181cb0ef41Sopenharmony_ci    process.nextTick(step);
1191cb0ef41Sopenharmony_ci  }
1201cb0ef41Sopenharmony_ci
1211cb0ef41Sopenharmony_ci  process.nextTick(step);
1221cb0ef41Sopenharmony_ci});
123