11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst stream = require('stream');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst readable = new stream.Readable({
71cb0ef41Sopenharmony_ci  read: () => {}
81cb0ef41Sopenharmony_ci});
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ciconst writables = [];
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_cifor (let i = 0; i < 5; i++) {
131cb0ef41Sopenharmony_ci  const target = new stream.Writable({
141cb0ef41Sopenharmony_ci    write: common.mustCall((chunk, encoding, callback) => {
151cb0ef41Sopenharmony_ci      target.output.push(chunk);
161cb0ef41Sopenharmony_ci      callback();
171cb0ef41Sopenharmony_ci    }, 1)
181cb0ef41Sopenharmony_ci  });
191cb0ef41Sopenharmony_ci  target.output = [];
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci  target.on('pipe', common.mustCall());
221cb0ef41Sopenharmony_ci  readable.pipe(target);
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci  writables.push(target);
261cb0ef41Sopenharmony_ci}
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ciconst input = Buffer.from([1, 2, 3, 4, 5]);
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_cireadable.push(input);
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci// The pipe() calls will postpone emission of the 'resume' event using nextTick,
331cb0ef41Sopenharmony_ci// so no data will be available to the writable streams until then.
341cb0ef41Sopenharmony_ciprocess.nextTick(common.mustCall(() => {
351cb0ef41Sopenharmony_ci  for (const target of writables) {
361cb0ef41Sopenharmony_ci    assert.deepStrictEqual(target.output, [input]);
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci    target.on('unpipe', common.mustCall());
391cb0ef41Sopenharmony_ci    readable.unpipe(target);
401cb0ef41Sopenharmony_ci  }
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci  readable.push('something else'); // This does not get through.
431cb0ef41Sopenharmony_ci  readable.push(null);
441cb0ef41Sopenharmony_ci  readable.resume(); // Make sure the 'end' event gets emitted.
451cb0ef41Sopenharmony_ci}));
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_cireadable.on('end', common.mustCall(() => {
481cb0ef41Sopenharmony_ci  for (const target of writables) {
491cb0ef41Sopenharmony_ci    assert.deepStrictEqual(target.output, [input]);
501cb0ef41Sopenharmony_ci  }
511cb0ef41Sopenharmony_ci}));
52