11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst assert = require('assert');
41cb0ef41Sopenharmony_ciconst { Readable, Duplex, pipeline } = require('stream');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ci// Test that the callback for pipeline() is called even when the ._destroy()
71cb0ef41Sopenharmony_ci// method of the stream places an .end() request to itself that does not
81cb0ef41Sopenharmony_ci// get processed before the destruction of the stream (i.e. the 'close' event).
91cb0ef41Sopenharmony_ci// Refs: https://github.com/nodejs/node/issues/24456
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciconst readable = new Readable({
121cb0ef41Sopenharmony_ci  read: common.mustCall()
131cb0ef41Sopenharmony_ci});
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ciconst duplex = new Duplex({
161cb0ef41Sopenharmony_ci  write(chunk, enc, cb) {
171cb0ef41Sopenharmony_ci    // Simulate messages queueing up.
181cb0ef41Sopenharmony_ci  },
191cb0ef41Sopenharmony_ci  read() {},
201cb0ef41Sopenharmony_ci  destroy(err, cb) {
211cb0ef41Sopenharmony_ci    // Call end() from inside the destroy() method, like HTTP/2 streams
221cb0ef41Sopenharmony_ci    // do at the time of writing.
231cb0ef41Sopenharmony_ci    this.end();
241cb0ef41Sopenharmony_ci    cb(err);
251cb0ef41Sopenharmony_ci  }
261cb0ef41Sopenharmony_ci});
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ciduplex.on('finished', common.mustNotCall());
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_cipipeline(readable, duplex, common.mustCall((err) => {
311cb0ef41Sopenharmony_ci  assert.strictEqual(err.code, 'ERR_STREAM_PREMATURE_CLOSE');
321cb0ef41Sopenharmony_ci}));
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci// Write one chunk of data, and destroy the stream later.
351cb0ef41Sopenharmony_ci// That should trigger the pipeline destruction.
361cb0ef41Sopenharmony_cireadable.push('foo');
371cb0ef41Sopenharmony_cisetImmediate(() => {
381cb0ef41Sopenharmony_ci  readable.destroy();
391cb0ef41Sopenharmony_ci});
40