11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ci// Regression test for https://github.com/nodejs/node/issues/12718.
51cb0ef41Sopenharmony_ci// Tests that piping a source stream twice to the same destination stream
61cb0ef41Sopenharmony_ci// works, and that a subsequent unpipe() call only removes the pipe *once*.
71cb0ef41Sopenharmony_ciconst assert = require('assert');
81cb0ef41Sopenharmony_ciconst { PassThrough, Writable } = require('stream');
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci{
111cb0ef41Sopenharmony_ci  const passThrough = new PassThrough();
121cb0ef41Sopenharmony_ci  const dest = new Writable({
131cb0ef41Sopenharmony_ci    write: common.mustCall((chunk, encoding, cb) => {
141cb0ef41Sopenharmony_ci      assert.strictEqual(`${chunk}`, 'foobar');
151cb0ef41Sopenharmony_ci      cb();
161cb0ef41Sopenharmony_ci    })
171cb0ef41Sopenharmony_ci  });
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci  passThrough.pipe(dest);
201cb0ef41Sopenharmony_ci  passThrough.pipe(dest);
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci  assert.strictEqual(passThrough._events.data.length, 2);
231cb0ef41Sopenharmony_ci  assert.strictEqual(passThrough._readableState.pipes.length, 2);
241cb0ef41Sopenharmony_ci  assert.strictEqual(passThrough._readableState.pipes[0], dest);
251cb0ef41Sopenharmony_ci  assert.strictEqual(passThrough._readableState.pipes[1], dest);
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci  passThrough.unpipe(dest);
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci  assert.strictEqual(passThrough._events.data.length, 1);
301cb0ef41Sopenharmony_ci  assert.strictEqual(passThrough._readableState.pipes.length, 1);
311cb0ef41Sopenharmony_ci  assert.deepStrictEqual(passThrough._readableState.pipes, [dest]);
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci  passThrough.write('foobar');
341cb0ef41Sopenharmony_ci  passThrough.pipe(dest);
351cb0ef41Sopenharmony_ci}
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci{
381cb0ef41Sopenharmony_ci  const passThrough = new PassThrough();
391cb0ef41Sopenharmony_ci  const dest = new Writable({
401cb0ef41Sopenharmony_ci    write: common.mustCall((chunk, encoding, cb) => {
411cb0ef41Sopenharmony_ci      assert.strictEqual(`${chunk}`, 'foobar');
421cb0ef41Sopenharmony_ci      cb();
431cb0ef41Sopenharmony_ci    }, 2)
441cb0ef41Sopenharmony_ci  });
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci  passThrough.pipe(dest);
471cb0ef41Sopenharmony_ci  passThrough.pipe(dest);
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci  assert.strictEqual(passThrough._events.data.length, 2);
501cb0ef41Sopenharmony_ci  assert.strictEqual(passThrough._readableState.pipes.length, 2);
511cb0ef41Sopenharmony_ci  assert.strictEqual(passThrough._readableState.pipes[0], dest);
521cb0ef41Sopenharmony_ci  assert.strictEqual(passThrough._readableState.pipes[1], dest);
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci  passThrough.write('foobar');
551cb0ef41Sopenharmony_ci}
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci{
581cb0ef41Sopenharmony_ci  const passThrough = new PassThrough();
591cb0ef41Sopenharmony_ci  const dest = new Writable({
601cb0ef41Sopenharmony_ci    write: common.mustNotCall()
611cb0ef41Sopenharmony_ci  });
621cb0ef41Sopenharmony_ci
631cb0ef41Sopenharmony_ci  passThrough.pipe(dest);
641cb0ef41Sopenharmony_ci  passThrough.pipe(dest);
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ci  assert.strictEqual(passThrough._events.data.length, 2);
671cb0ef41Sopenharmony_ci  assert.strictEqual(passThrough._readableState.pipes.length, 2);
681cb0ef41Sopenharmony_ci  assert.strictEqual(passThrough._readableState.pipes[0], dest);
691cb0ef41Sopenharmony_ci  assert.strictEqual(passThrough._readableState.pipes[1], dest);
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_ci  passThrough.unpipe(dest);
721cb0ef41Sopenharmony_ci  passThrough.unpipe(dest);
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_ci  assert.strictEqual(passThrough._events.data, undefined);
751cb0ef41Sopenharmony_ci  assert.strictEqual(passThrough._readableState.pipes.length, 0);
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_ci  passThrough.write('foobar');
781cb0ef41Sopenharmony_ci}
79