11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst {
51cb0ef41Sopenharmony_ci  Stream,
61cb0ef41Sopenharmony_ci  Readable,
71cb0ef41Sopenharmony_ci  Transform,
81cb0ef41Sopenharmony_ci  PassThrough,
91cb0ef41Sopenharmony_ci  pipeline
101cb0ef41Sopenharmony_ci} = require('stream');
111cb0ef41Sopenharmony_ciconst assert = require('assert');
121cb0ef41Sopenharmony_ciconst http = require('http');
131cb0ef41Sopenharmony_ciconst fs = require('fs');
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ciasync function tests() {
161cb0ef41Sopenharmony_ci  {
171cb0ef41Sopenharmony_ci    // v1 stream
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci    const stream = new Stream();
201cb0ef41Sopenharmony_ci    stream.destroy = common.mustCall();
211cb0ef41Sopenharmony_ci    process.nextTick(() => {
221cb0ef41Sopenharmony_ci      stream.emit('data', 'hello');
231cb0ef41Sopenharmony_ci      stream.emit('data', 'world');
241cb0ef41Sopenharmony_ci      stream.emit('end');
251cb0ef41Sopenharmony_ci    });
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci    let res = '';
281cb0ef41Sopenharmony_ci    stream[Symbol.asyncIterator] = Readable.prototype[Symbol.asyncIterator];
291cb0ef41Sopenharmony_ci    for await (const d of stream) {
301cb0ef41Sopenharmony_ci      res += d;
311cb0ef41Sopenharmony_ci    }
321cb0ef41Sopenharmony_ci    assert.strictEqual(res, 'helloworld');
331cb0ef41Sopenharmony_ci  }
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci  {
361cb0ef41Sopenharmony_ci    // v1 stream error
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci    const stream = new Stream();
391cb0ef41Sopenharmony_ci    stream.close = common.mustCall();
401cb0ef41Sopenharmony_ci    process.nextTick(() => {
411cb0ef41Sopenharmony_ci      stream.emit('data', 0);
421cb0ef41Sopenharmony_ci      stream.emit('data', 1);
431cb0ef41Sopenharmony_ci      stream.emit('error', new Error('asd'));
441cb0ef41Sopenharmony_ci    });
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci    const iter = Readable.prototype[Symbol.asyncIterator].call(stream);
471cb0ef41Sopenharmony_ci    await iter.next()
481cb0ef41Sopenharmony_ci      .then(common.mustNotCall())
491cb0ef41Sopenharmony_ci      .catch(common.mustCall((err) => {
501cb0ef41Sopenharmony_ci        assert.strictEqual(err.message, 'asd');
511cb0ef41Sopenharmony_ci      }));
521cb0ef41Sopenharmony_ci  }
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci  {
551cb0ef41Sopenharmony_ci    // Non standard stream cleanup
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci    const readable = new Readable({ autoDestroy: false, read() {} });
581cb0ef41Sopenharmony_ci    readable.push('asd');
591cb0ef41Sopenharmony_ci    readable.push('asd');
601cb0ef41Sopenharmony_ci    readable.destroy = null;
611cb0ef41Sopenharmony_ci    readable.close = common.mustCall(() => {
621cb0ef41Sopenharmony_ci      readable.emit('close');
631cb0ef41Sopenharmony_ci    });
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ci    await (async () => {
661cb0ef41Sopenharmony_ci      for await (const d of readable) {
671cb0ef41Sopenharmony_ci        return;
681cb0ef41Sopenharmony_ci      }
691cb0ef41Sopenharmony_ci    })();
701cb0ef41Sopenharmony_ci  }
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ci  {
731cb0ef41Sopenharmony_ci    const readable = new Readable({ objectMode: true, read() {} });
741cb0ef41Sopenharmony_ci    readable.push(0);
751cb0ef41Sopenharmony_ci    readable.push(1);
761cb0ef41Sopenharmony_ci    readable.push(null);
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_ci    const iter = readable[Symbol.asyncIterator]();
791cb0ef41Sopenharmony_ci    assert.strictEqual((await iter.next()).value, 0);
801cb0ef41Sopenharmony_ci    for await (const d of iter) {
811cb0ef41Sopenharmony_ci      assert.strictEqual(d, 1);
821cb0ef41Sopenharmony_ci    }
831cb0ef41Sopenharmony_ci  }
841cb0ef41Sopenharmony_ci
851cb0ef41Sopenharmony_ci  {
861cb0ef41Sopenharmony_ci    console.log('read without for..await');
871cb0ef41Sopenharmony_ci    const max = 5;
881cb0ef41Sopenharmony_ci    const readable = new Readable({
891cb0ef41Sopenharmony_ci      objectMode: true,
901cb0ef41Sopenharmony_ci      read() {}
911cb0ef41Sopenharmony_ci    });
921cb0ef41Sopenharmony_ci
931cb0ef41Sopenharmony_ci    const iter = readable[Symbol.asyncIterator]();
941cb0ef41Sopenharmony_ci    assert.strictEqual(iter.stream, readable);
951cb0ef41Sopenharmony_ci    const values = [];
961cb0ef41Sopenharmony_ci    for (let i = 0; i < max; i++) {
971cb0ef41Sopenharmony_ci      values.push(iter.next());
981cb0ef41Sopenharmony_ci    }
991cb0ef41Sopenharmony_ci    Promise.all(values).then(common.mustCall((values) => {
1001cb0ef41Sopenharmony_ci      values.forEach(common.mustCall(
1011cb0ef41Sopenharmony_ci        (item, i) => assert.strictEqual(item.value, 'hello-' + i), 5));
1021cb0ef41Sopenharmony_ci    }));
1031cb0ef41Sopenharmony_ci
1041cb0ef41Sopenharmony_ci    readable.push('hello-0');
1051cb0ef41Sopenharmony_ci    readable.push('hello-1');
1061cb0ef41Sopenharmony_ci    readable.push('hello-2');
1071cb0ef41Sopenharmony_ci    readable.push('hello-3');
1081cb0ef41Sopenharmony_ci    readable.push('hello-4');
1091cb0ef41Sopenharmony_ci    readable.push(null);
1101cb0ef41Sopenharmony_ci
1111cb0ef41Sopenharmony_ci    const last = await iter.next();
1121cb0ef41Sopenharmony_ci    assert.strictEqual(last.done, true);
1131cb0ef41Sopenharmony_ci  }
1141cb0ef41Sopenharmony_ci
1151cb0ef41Sopenharmony_ci  {
1161cb0ef41Sopenharmony_ci    console.log('read without for..await deferred');
1171cb0ef41Sopenharmony_ci    const readable = new Readable({
1181cb0ef41Sopenharmony_ci      objectMode: true,
1191cb0ef41Sopenharmony_ci      read() {}
1201cb0ef41Sopenharmony_ci    });
1211cb0ef41Sopenharmony_ci
1221cb0ef41Sopenharmony_ci    const iter = readable[Symbol.asyncIterator]();
1231cb0ef41Sopenharmony_ci    assert.strictEqual(iter.stream, readable);
1241cb0ef41Sopenharmony_ci    let values = [];
1251cb0ef41Sopenharmony_ci    for (let i = 0; i < 3; i++) {
1261cb0ef41Sopenharmony_ci      values.push(iter.next());
1271cb0ef41Sopenharmony_ci    }
1281cb0ef41Sopenharmony_ci
1291cb0ef41Sopenharmony_ci    readable.push('hello-0');
1301cb0ef41Sopenharmony_ci    readable.push('hello-1');
1311cb0ef41Sopenharmony_ci    readable.push('hello-2');
1321cb0ef41Sopenharmony_ci
1331cb0ef41Sopenharmony_ci    let k = 0;
1341cb0ef41Sopenharmony_ci    const results1 = await Promise.all(values);
1351cb0ef41Sopenharmony_ci    results1.forEach(common.mustCall(
1361cb0ef41Sopenharmony_ci      (item) => assert.strictEqual(item.value, 'hello-' + k++), 3));
1371cb0ef41Sopenharmony_ci
1381cb0ef41Sopenharmony_ci    values = [];
1391cb0ef41Sopenharmony_ci    for (let i = 0; i < 2; i++) {
1401cb0ef41Sopenharmony_ci      values.push(iter.next());
1411cb0ef41Sopenharmony_ci    }
1421cb0ef41Sopenharmony_ci
1431cb0ef41Sopenharmony_ci    readable.push('hello-3');
1441cb0ef41Sopenharmony_ci    readable.push('hello-4');
1451cb0ef41Sopenharmony_ci    readable.push(null);
1461cb0ef41Sopenharmony_ci
1471cb0ef41Sopenharmony_ci    const results2 = await Promise.all(values);
1481cb0ef41Sopenharmony_ci    results2.forEach(common.mustCall(
1491cb0ef41Sopenharmony_ci      (item) => assert.strictEqual(item.value, 'hello-' + k++), 2));
1501cb0ef41Sopenharmony_ci
1511cb0ef41Sopenharmony_ci    const last = await iter.next();
1521cb0ef41Sopenharmony_ci    assert.strictEqual(last.done, true);
1531cb0ef41Sopenharmony_ci  }
1541cb0ef41Sopenharmony_ci
1551cb0ef41Sopenharmony_ci  {
1561cb0ef41Sopenharmony_ci    console.log('read without for..await with errors');
1571cb0ef41Sopenharmony_ci    const max = 3;
1581cb0ef41Sopenharmony_ci    const readable = new Readable({
1591cb0ef41Sopenharmony_ci      objectMode: true,
1601cb0ef41Sopenharmony_ci      read() {}
1611cb0ef41Sopenharmony_ci    });
1621cb0ef41Sopenharmony_ci
1631cb0ef41Sopenharmony_ci    const iter = readable[Symbol.asyncIterator]();
1641cb0ef41Sopenharmony_ci    assert.strictEqual(iter.stream, readable);
1651cb0ef41Sopenharmony_ci    const values = [];
1661cb0ef41Sopenharmony_ci    const errors = [];
1671cb0ef41Sopenharmony_ci    let i;
1681cb0ef41Sopenharmony_ci    for (i = 0; i < max; i++) {
1691cb0ef41Sopenharmony_ci      values.push(iter.next());
1701cb0ef41Sopenharmony_ci    }
1711cb0ef41Sopenharmony_ci    for (i = 0; i < 2; i++) {
1721cb0ef41Sopenharmony_ci      errors.push(iter.next());
1731cb0ef41Sopenharmony_ci    }
1741cb0ef41Sopenharmony_ci
1751cb0ef41Sopenharmony_ci    readable.push('hello-0');
1761cb0ef41Sopenharmony_ci    readable.push('hello-1');
1771cb0ef41Sopenharmony_ci    readable.push('hello-2');
1781cb0ef41Sopenharmony_ci
1791cb0ef41Sopenharmony_ci    const resolved = await Promise.all(values);
1801cb0ef41Sopenharmony_ci
1811cb0ef41Sopenharmony_ci    resolved.forEach(common.mustCall(
1821cb0ef41Sopenharmony_ci      (item, i) => assert.strictEqual(item.value, 'hello-' + i), max));
1831cb0ef41Sopenharmony_ci
1841cb0ef41Sopenharmony_ci    errors.slice(0, 1).forEach((promise) => {
1851cb0ef41Sopenharmony_ci      promise.catch(common.mustCall((err) => {
1861cb0ef41Sopenharmony_ci        assert.strictEqual(err.message, 'kaboom');
1871cb0ef41Sopenharmony_ci      }));
1881cb0ef41Sopenharmony_ci    });
1891cb0ef41Sopenharmony_ci
1901cb0ef41Sopenharmony_ci    errors.slice(1).forEach((promise) => {
1911cb0ef41Sopenharmony_ci      promise.then(common.mustCall(({ done, value }) => {
1921cb0ef41Sopenharmony_ci        assert.strictEqual(done, true);
1931cb0ef41Sopenharmony_ci        assert.strictEqual(value, undefined);
1941cb0ef41Sopenharmony_ci      }));
1951cb0ef41Sopenharmony_ci    });
1961cb0ef41Sopenharmony_ci
1971cb0ef41Sopenharmony_ci    readable.destroy(new Error('kaboom'));
1981cb0ef41Sopenharmony_ci  }
1991cb0ef41Sopenharmony_ci
2001cb0ef41Sopenharmony_ci  {
2011cb0ef41Sopenharmony_ci    console.log('call next() after error');
2021cb0ef41Sopenharmony_ci    const readable = new Readable({
2031cb0ef41Sopenharmony_ci      read() {}
2041cb0ef41Sopenharmony_ci    });
2051cb0ef41Sopenharmony_ci    const iterator = readable[Symbol.asyncIterator]();
2061cb0ef41Sopenharmony_ci
2071cb0ef41Sopenharmony_ci    const err = new Error('kaboom');
2081cb0ef41Sopenharmony_ci    readable.destroy(err);
2091cb0ef41Sopenharmony_ci    await assert.rejects(iterator.next.bind(iterator), err);
2101cb0ef41Sopenharmony_ci  }
2111cb0ef41Sopenharmony_ci
2121cb0ef41Sopenharmony_ci  {
2131cb0ef41Sopenharmony_ci    console.log('read object mode');
2141cb0ef41Sopenharmony_ci    const max = 42;
2151cb0ef41Sopenharmony_ci    let readed = 0;
2161cb0ef41Sopenharmony_ci    let received = 0;
2171cb0ef41Sopenharmony_ci    const readable = new Readable({
2181cb0ef41Sopenharmony_ci      objectMode: true,
2191cb0ef41Sopenharmony_ci      read() {
2201cb0ef41Sopenharmony_ci        this.push('hello');
2211cb0ef41Sopenharmony_ci        if (++readed === max) {
2221cb0ef41Sopenharmony_ci          this.push(null);
2231cb0ef41Sopenharmony_ci        }
2241cb0ef41Sopenharmony_ci      }
2251cb0ef41Sopenharmony_ci    });
2261cb0ef41Sopenharmony_ci
2271cb0ef41Sopenharmony_ci    for await (const k of readable) {
2281cb0ef41Sopenharmony_ci      received++;
2291cb0ef41Sopenharmony_ci      assert.strictEqual(k, 'hello');
2301cb0ef41Sopenharmony_ci    }
2311cb0ef41Sopenharmony_ci
2321cb0ef41Sopenharmony_ci    assert.strictEqual(readed, received);
2331cb0ef41Sopenharmony_ci  }
2341cb0ef41Sopenharmony_ci
2351cb0ef41Sopenharmony_ci  {
2361cb0ef41Sopenharmony_ci    console.log('destroy sync');
2371cb0ef41Sopenharmony_ci    const readable = new Readable({
2381cb0ef41Sopenharmony_ci      objectMode: true,
2391cb0ef41Sopenharmony_ci      read() {
2401cb0ef41Sopenharmony_ci        this.destroy(new Error('kaboom from read'));
2411cb0ef41Sopenharmony_ci      }
2421cb0ef41Sopenharmony_ci    });
2431cb0ef41Sopenharmony_ci
2441cb0ef41Sopenharmony_ci    let err;
2451cb0ef41Sopenharmony_ci    try {
2461cb0ef41Sopenharmony_ci      // eslint-disable-next-line no-unused-vars, no-empty
2471cb0ef41Sopenharmony_ci      for await (const k of readable) { }
2481cb0ef41Sopenharmony_ci    } catch (e) {
2491cb0ef41Sopenharmony_ci      err = e;
2501cb0ef41Sopenharmony_ci    }
2511cb0ef41Sopenharmony_ci    assert.strictEqual(err.message, 'kaboom from read');
2521cb0ef41Sopenharmony_ci  }
2531cb0ef41Sopenharmony_ci
2541cb0ef41Sopenharmony_ci  {
2551cb0ef41Sopenharmony_ci    console.log('destroy async');
2561cb0ef41Sopenharmony_ci    const readable = new Readable({
2571cb0ef41Sopenharmony_ci      objectMode: true,
2581cb0ef41Sopenharmony_ci      read() {
2591cb0ef41Sopenharmony_ci        if (!this.pushed) {
2601cb0ef41Sopenharmony_ci          this.push('hello');
2611cb0ef41Sopenharmony_ci          this.pushed = true;
2621cb0ef41Sopenharmony_ci
2631cb0ef41Sopenharmony_ci          setImmediate(() => {
2641cb0ef41Sopenharmony_ci            this.destroy(new Error('kaboom'));
2651cb0ef41Sopenharmony_ci          });
2661cb0ef41Sopenharmony_ci        }
2671cb0ef41Sopenharmony_ci      }
2681cb0ef41Sopenharmony_ci    });
2691cb0ef41Sopenharmony_ci
2701cb0ef41Sopenharmony_ci    let received = 0;
2711cb0ef41Sopenharmony_ci
2721cb0ef41Sopenharmony_ci    let err = null;
2731cb0ef41Sopenharmony_ci    try {
2741cb0ef41Sopenharmony_ci      // eslint-disable-next-line no-unused-vars
2751cb0ef41Sopenharmony_ci      for await (const k of readable) {
2761cb0ef41Sopenharmony_ci        received++;
2771cb0ef41Sopenharmony_ci      }
2781cb0ef41Sopenharmony_ci    } catch (e) {
2791cb0ef41Sopenharmony_ci      err = e;
2801cb0ef41Sopenharmony_ci    }
2811cb0ef41Sopenharmony_ci
2821cb0ef41Sopenharmony_ci    assert.strictEqual(err.message, 'kaboom');
2831cb0ef41Sopenharmony_ci    assert.strictEqual(received, 1);
2841cb0ef41Sopenharmony_ci  }
2851cb0ef41Sopenharmony_ci
2861cb0ef41Sopenharmony_ci  {
2871cb0ef41Sopenharmony_ci    console.log('destroyed by throw');
2881cb0ef41Sopenharmony_ci    const readable = new Readable({
2891cb0ef41Sopenharmony_ci      objectMode: true,
2901cb0ef41Sopenharmony_ci      read() {
2911cb0ef41Sopenharmony_ci        this.push('hello');
2921cb0ef41Sopenharmony_ci      }
2931cb0ef41Sopenharmony_ci    });
2941cb0ef41Sopenharmony_ci
2951cb0ef41Sopenharmony_ci    let err = null;
2961cb0ef41Sopenharmony_ci    try {
2971cb0ef41Sopenharmony_ci      for await (const k of readable) {
2981cb0ef41Sopenharmony_ci        assert.strictEqual(k, 'hello');
2991cb0ef41Sopenharmony_ci        throw new Error('kaboom');
3001cb0ef41Sopenharmony_ci      }
3011cb0ef41Sopenharmony_ci    } catch (e) {
3021cb0ef41Sopenharmony_ci      err = e;
3031cb0ef41Sopenharmony_ci    }
3041cb0ef41Sopenharmony_ci
3051cb0ef41Sopenharmony_ci    assert.strictEqual(err.message, 'kaboom');
3061cb0ef41Sopenharmony_ci    assert.strictEqual(readable.destroyed, true);
3071cb0ef41Sopenharmony_ci  }
3081cb0ef41Sopenharmony_ci
3091cb0ef41Sopenharmony_ci  {
3101cb0ef41Sopenharmony_ci    console.log('destroyed sync after push');
3111cb0ef41Sopenharmony_ci    const readable = new Readable({
3121cb0ef41Sopenharmony_ci      objectMode: true,
3131cb0ef41Sopenharmony_ci      read() {
3141cb0ef41Sopenharmony_ci        this.push('hello');
3151cb0ef41Sopenharmony_ci        this.destroy(new Error('kaboom'));
3161cb0ef41Sopenharmony_ci      }
3171cb0ef41Sopenharmony_ci    });
3181cb0ef41Sopenharmony_ci
3191cb0ef41Sopenharmony_ci    let received = 0;
3201cb0ef41Sopenharmony_ci
3211cb0ef41Sopenharmony_ci    let err = null;
3221cb0ef41Sopenharmony_ci    try {
3231cb0ef41Sopenharmony_ci      for await (const k of readable) {
3241cb0ef41Sopenharmony_ci        assert.strictEqual(k, 'hello');
3251cb0ef41Sopenharmony_ci        received++;
3261cb0ef41Sopenharmony_ci      }
3271cb0ef41Sopenharmony_ci    } catch (e) {
3281cb0ef41Sopenharmony_ci      err = e;
3291cb0ef41Sopenharmony_ci    }
3301cb0ef41Sopenharmony_ci
3311cb0ef41Sopenharmony_ci    assert.strictEqual(err.message, 'kaboom');
3321cb0ef41Sopenharmony_ci    assert.strictEqual(received, 1);
3331cb0ef41Sopenharmony_ci  }
3341cb0ef41Sopenharmony_ci
3351cb0ef41Sopenharmony_ci  {
3361cb0ef41Sopenharmony_ci    console.log('destroyed will not deadlock');
3371cb0ef41Sopenharmony_ci    const readable = new Readable();
3381cb0ef41Sopenharmony_ci    readable.destroy();
3391cb0ef41Sopenharmony_ci    process.nextTick(async () => {
3401cb0ef41Sopenharmony_ci      readable.on('close', common.mustNotCall());
3411cb0ef41Sopenharmony_ci      let received = 0;
3421cb0ef41Sopenharmony_ci      let err = null;
3431cb0ef41Sopenharmony_ci      try {
3441cb0ef41Sopenharmony_ci        for await (const k of readable) {
3451cb0ef41Sopenharmony_ci          // Just make linting pass. This should never run.
3461cb0ef41Sopenharmony_ci          assert.strictEqual(k, 'hello');
3471cb0ef41Sopenharmony_ci          received++;
3481cb0ef41Sopenharmony_ci        }
3491cb0ef41Sopenharmony_ci      } catch (_err) {
3501cb0ef41Sopenharmony_ci        err = _err;
3511cb0ef41Sopenharmony_ci      }
3521cb0ef41Sopenharmony_ci      assert.strictEqual(err.code, 'ERR_STREAM_PREMATURE_CLOSE');
3531cb0ef41Sopenharmony_ci      assert.strictEqual(received, 0);
3541cb0ef41Sopenharmony_ci    });
3551cb0ef41Sopenharmony_ci  }
3561cb0ef41Sopenharmony_ci
3571cb0ef41Sopenharmony_ci  {
3581cb0ef41Sopenharmony_ci    console.log('push async');
3591cb0ef41Sopenharmony_ci    const max = 42;
3601cb0ef41Sopenharmony_ci    let readed = 0;
3611cb0ef41Sopenharmony_ci    let received = 0;
3621cb0ef41Sopenharmony_ci    const readable = new Readable({
3631cb0ef41Sopenharmony_ci      objectMode: true,
3641cb0ef41Sopenharmony_ci      read() {
3651cb0ef41Sopenharmony_ci        setImmediate(() => {
3661cb0ef41Sopenharmony_ci          this.push('hello');
3671cb0ef41Sopenharmony_ci          if (++readed === max) {
3681cb0ef41Sopenharmony_ci            this.push(null);
3691cb0ef41Sopenharmony_ci          }
3701cb0ef41Sopenharmony_ci        });
3711cb0ef41Sopenharmony_ci      }
3721cb0ef41Sopenharmony_ci    });
3731cb0ef41Sopenharmony_ci
3741cb0ef41Sopenharmony_ci    for await (const k of readable) {
3751cb0ef41Sopenharmony_ci      received++;
3761cb0ef41Sopenharmony_ci      assert.strictEqual(k, 'hello');
3771cb0ef41Sopenharmony_ci    }
3781cb0ef41Sopenharmony_ci
3791cb0ef41Sopenharmony_ci    assert.strictEqual(readed, received);
3801cb0ef41Sopenharmony_ci  }
3811cb0ef41Sopenharmony_ci
3821cb0ef41Sopenharmony_ci  {
3831cb0ef41Sopenharmony_ci    console.log('push binary async');
3841cb0ef41Sopenharmony_ci    const max = 42;
3851cb0ef41Sopenharmony_ci    let readed = 0;
3861cb0ef41Sopenharmony_ci    const readable = new Readable({
3871cb0ef41Sopenharmony_ci      read() {
3881cb0ef41Sopenharmony_ci        setImmediate(() => {
3891cb0ef41Sopenharmony_ci          this.push('hello');
3901cb0ef41Sopenharmony_ci          if (++readed === max) {
3911cb0ef41Sopenharmony_ci            this.push(null);
3921cb0ef41Sopenharmony_ci          }
3931cb0ef41Sopenharmony_ci        });
3941cb0ef41Sopenharmony_ci      }
3951cb0ef41Sopenharmony_ci    });
3961cb0ef41Sopenharmony_ci
3971cb0ef41Sopenharmony_ci    let expected = '';
3981cb0ef41Sopenharmony_ci    readable.setEncoding('utf8');
3991cb0ef41Sopenharmony_ci    readable.pause();
4001cb0ef41Sopenharmony_ci    readable.on('data', (chunk) => {
4011cb0ef41Sopenharmony_ci      expected += chunk;
4021cb0ef41Sopenharmony_ci    });
4031cb0ef41Sopenharmony_ci
4041cb0ef41Sopenharmony_ci    let data = '';
4051cb0ef41Sopenharmony_ci    for await (const k of readable) {
4061cb0ef41Sopenharmony_ci      data += k;
4071cb0ef41Sopenharmony_ci    }
4081cb0ef41Sopenharmony_ci
4091cb0ef41Sopenharmony_ci    assert.strictEqual(data, expected);
4101cb0ef41Sopenharmony_ci  }
4111cb0ef41Sopenharmony_ci
4121cb0ef41Sopenharmony_ci  {
4131cb0ef41Sopenharmony_ci    console.log('.next() on destroyed stream');
4141cb0ef41Sopenharmony_ci    const readable = new Readable({
4151cb0ef41Sopenharmony_ci      read() {
4161cb0ef41Sopenharmony_ci        // no-op
4171cb0ef41Sopenharmony_ci      }
4181cb0ef41Sopenharmony_ci    });
4191cb0ef41Sopenharmony_ci
4201cb0ef41Sopenharmony_ci    readable.destroy();
4211cb0ef41Sopenharmony_ci
4221cb0ef41Sopenharmony_ci    const it = await readable[Symbol.asyncIterator]();
4231cb0ef41Sopenharmony_ci    const next = it.next();
4241cb0ef41Sopenharmony_ci    next
4251cb0ef41Sopenharmony_ci      .then(common.mustNotCall())
4261cb0ef41Sopenharmony_ci      .catch(common.mustCall((err) => {
4271cb0ef41Sopenharmony_ci        assert.strictEqual(err.code, 'ERR_STREAM_PREMATURE_CLOSE');
4281cb0ef41Sopenharmony_ci      }));
4291cb0ef41Sopenharmony_ci  }
4301cb0ef41Sopenharmony_ci
4311cb0ef41Sopenharmony_ci  {
4321cb0ef41Sopenharmony_ci    console.log('.next() on pipelined stream');
4331cb0ef41Sopenharmony_ci    const readable = new Readable({
4341cb0ef41Sopenharmony_ci      read() {
4351cb0ef41Sopenharmony_ci        // no-op
4361cb0ef41Sopenharmony_ci      }
4371cb0ef41Sopenharmony_ci    });
4381cb0ef41Sopenharmony_ci
4391cb0ef41Sopenharmony_ci    const passthrough = new PassThrough();
4401cb0ef41Sopenharmony_ci    const err = new Error('kaboom');
4411cb0ef41Sopenharmony_ci    pipeline(readable, passthrough, common.mustCall((e) => {
4421cb0ef41Sopenharmony_ci      assert.strictEqual(e, err);
4431cb0ef41Sopenharmony_ci    }));
4441cb0ef41Sopenharmony_ci    readable.destroy(err);
4451cb0ef41Sopenharmony_ci    await assert.rejects(
4461cb0ef41Sopenharmony_ci      readable[Symbol.asyncIterator]().next(),
4471cb0ef41Sopenharmony_ci      (e) => {
4481cb0ef41Sopenharmony_ci        assert.strictEqual(e, err);
4491cb0ef41Sopenharmony_ci        return true;
4501cb0ef41Sopenharmony_ci      }
4511cb0ef41Sopenharmony_ci    );
4521cb0ef41Sopenharmony_ci  }
4531cb0ef41Sopenharmony_ci
4541cb0ef41Sopenharmony_ci  {
4551cb0ef41Sopenharmony_ci    console.log('iterating on an ended stream completes');
4561cb0ef41Sopenharmony_ci    const r = new Readable({
4571cb0ef41Sopenharmony_ci      objectMode: true,
4581cb0ef41Sopenharmony_ci      read() {
4591cb0ef41Sopenharmony_ci        this.push('asdf');
4601cb0ef41Sopenharmony_ci        this.push('hehe');
4611cb0ef41Sopenharmony_ci        this.push(null);
4621cb0ef41Sopenharmony_ci      }
4631cb0ef41Sopenharmony_ci    });
4641cb0ef41Sopenharmony_ci    // eslint-disable-next-line no-unused-vars, no-empty
4651cb0ef41Sopenharmony_ci    for await (const a of r) { }
4661cb0ef41Sopenharmony_ci    // eslint-disable-next-line no-unused-vars, no-empty
4671cb0ef41Sopenharmony_ci    for await (const b of r) { }
4681cb0ef41Sopenharmony_ci  }
4691cb0ef41Sopenharmony_ci
4701cb0ef41Sopenharmony_ci  {
4711cb0ef41Sopenharmony_ci    console.log('destroy mid-stream errors');
4721cb0ef41Sopenharmony_ci    const r = new Readable({
4731cb0ef41Sopenharmony_ci      objectMode: true,
4741cb0ef41Sopenharmony_ci      read() {
4751cb0ef41Sopenharmony_ci        this.push('asdf');
4761cb0ef41Sopenharmony_ci        this.push('hehe');
4771cb0ef41Sopenharmony_ci      }
4781cb0ef41Sopenharmony_ci    });
4791cb0ef41Sopenharmony_ci
4801cb0ef41Sopenharmony_ci    let err = null;
4811cb0ef41Sopenharmony_ci    try {
4821cb0ef41Sopenharmony_ci      // eslint-disable-next-line no-unused-vars
4831cb0ef41Sopenharmony_ci      for await (const a of r) {
4841cb0ef41Sopenharmony_ci        r.destroy(null);
4851cb0ef41Sopenharmony_ci      }
4861cb0ef41Sopenharmony_ci    } catch (_err) {
4871cb0ef41Sopenharmony_ci      err = _err;
4881cb0ef41Sopenharmony_ci    }
4891cb0ef41Sopenharmony_ci    assert.strictEqual(err.code, 'ERR_STREAM_PREMATURE_CLOSE');
4901cb0ef41Sopenharmony_ci  }
4911cb0ef41Sopenharmony_ci
4921cb0ef41Sopenharmony_ci  {
4931cb0ef41Sopenharmony_ci    console.log('readable side of a transform stream pushes null');
4941cb0ef41Sopenharmony_ci    const transform = new Transform({
4951cb0ef41Sopenharmony_ci      objectMode: true,
4961cb0ef41Sopenharmony_ci      transform: (chunk, enc, cb) => { cb(null, chunk); }
4971cb0ef41Sopenharmony_ci    });
4981cb0ef41Sopenharmony_ci    transform.push(0);
4991cb0ef41Sopenharmony_ci    transform.push(1);
5001cb0ef41Sopenharmony_ci    process.nextTick(() => {
5011cb0ef41Sopenharmony_ci      transform.push(null);
5021cb0ef41Sopenharmony_ci    });
5031cb0ef41Sopenharmony_ci
5041cb0ef41Sopenharmony_ci    const mustReach = [ common.mustCall(), common.mustCall() ];
5051cb0ef41Sopenharmony_ci
5061cb0ef41Sopenharmony_ci    const iter = transform[Symbol.asyncIterator]();
5071cb0ef41Sopenharmony_ci    assert.strictEqual((await iter.next()).value, 0);
5081cb0ef41Sopenharmony_ci
5091cb0ef41Sopenharmony_ci    for await (const d of iter) {
5101cb0ef41Sopenharmony_ci      assert.strictEqual(d, 1);
5111cb0ef41Sopenharmony_ci      mustReach[0]();
5121cb0ef41Sopenharmony_ci    }
5131cb0ef41Sopenharmony_ci    mustReach[1]();
5141cb0ef41Sopenharmony_ci  }
5151cb0ef41Sopenharmony_ci
5161cb0ef41Sopenharmony_ci  {
5171cb0ef41Sopenharmony_ci    console.log('all next promises must be resolved on end');
5181cb0ef41Sopenharmony_ci    const r = new Readable({
5191cb0ef41Sopenharmony_ci      objectMode: true,
5201cb0ef41Sopenharmony_ci      read() {
5211cb0ef41Sopenharmony_ci      }
5221cb0ef41Sopenharmony_ci    });
5231cb0ef41Sopenharmony_ci
5241cb0ef41Sopenharmony_ci    const b = r[Symbol.asyncIterator]();
5251cb0ef41Sopenharmony_ci    const c = b.next();
5261cb0ef41Sopenharmony_ci    const d = b.next();
5271cb0ef41Sopenharmony_ci    r.push(null);
5281cb0ef41Sopenharmony_ci    assert.deepStrictEqual(await c, { done: true, value: undefined });
5291cb0ef41Sopenharmony_ci    assert.deepStrictEqual(await d, { done: true, value: undefined });
5301cb0ef41Sopenharmony_ci  }
5311cb0ef41Sopenharmony_ci
5321cb0ef41Sopenharmony_ci  {
5331cb0ef41Sopenharmony_ci    console.log('all next promises must be rejected on destroy');
5341cb0ef41Sopenharmony_ci    const r = new Readable({
5351cb0ef41Sopenharmony_ci      objectMode: true,
5361cb0ef41Sopenharmony_ci      read() {
5371cb0ef41Sopenharmony_ci      }
5381cb0ef41Sopenharmony_ci    });
5391cb0ef41Sopenharmony_ci
5401cb0ef41Sopenharmony_ci    const b = r[Symbol.asyncIterator]();
5411cb0ef41Sopenharmony_ci    const c = b.next();
5421cb0ef41Sopenharmony_ci    const d = b.next();
5431cb0ef41Sopenharmony_ci    r.destroy();
5441cb0ef41Sopenharmony_ci    c
5451cb0ef41Sopenharmony_ci      .then(common.mustNotCall())
5461cb0ef41Sopenharmony_ci      .catch(common.mustCall((err) => {
5471cb0ef41Sopenharmony_ci        assert.strictEqual(err.code, 'ERR_STREAM_PREMATURE_CLOSE');
5481cb0ef41Sopenharmony_ci      }));
5491cb0ef41Sopenharmony_ci    assert.deepStrictEqual(await d, { done: true, value: undefined });
5501cb0ef41Sopenharmony_ci  }
5511cb0ef41Sopenharmony_ci
5521cb0ef41Sopenharmony_ci  {
5531cb0ef41Sopenharmony_ci    console.log('all next promises must be resolved on destroy with error');
5541cb0ef41Sopenharmony_ci    const r = new Readable({
5551cb0ef41Sopenharmony_ci      objectMode: true,
5561cb0ef41Sopenharmony_ci      read() {
5571cb0ef41Sopenharmony_ci      }
5581cb0ef41Sopenharmony_ci    });
5591cb0ef41Sopenharmony_ci
5601cb0ef41Sopenharmony_ci    const b = r[Symbol.asyncIterator]();
5611cb0ef41Sopenharmony_ci    const c = b.next();
5621cb0ef41Sopenharmony_ci    const d = b.next();
5631cb0ef41Sopenharmony_ci    const err = new Error('kaboom');
5641cb0ef41Sopenharmony_ci    r.destroy(err);
5651cb0ef41Sopenharmony_ci
5661cb0ef41Sopenharmony_ci    await Promise.all([(async () => {
5671cb0ef41Sopenharmony_ci      let e;
5681cb0ef41Sopenharmony_ci      try {
5691cb0ef41Sopenharmony_ci        await c;
5701cb0ef41Sopenharmony_ci      } catch (_e) {
5711cb0ef41Sopenharmony_ci        e = _e;
5721cb0ef41Sopenharmony_ci      }
5731cb0ef41Sopenharmony_ci      assert.strictEqual(e, err);
5741cb0ef41Sopenharmony_ci    })(), (async () => {
5751cb0ef41Sopenharmony_ci      let e;
5761cb0ef41Sopenharmony_ci      let x;
5771cb0ef41Sopenharmony_ci      try {
5781cb0ef41Sopenharmony_ci        x = await d;
5791cb0ef41Sopenharmony_ci      } catch (_e) {
5801cb0ef41Sopenharmony_ci        e = _e;
5811cb0ef41Sopenharmony_ci      }
5821cb0ef41Sopenharmony_ci      assert.strictEqual(e, undefined);
5831cb0ef41Sopenharmony_ci      assert.strictEqual(x.done, true);
5841cb0ef41Sopenharmony_ci      assert.strictEqual(x.value, undefined);
5851cb0ef41Sopenharmony_ci    })()]);
5861cb0ef41Sopenharmony_ci  }
5871cb0ef41Sopenharmony_ci
5881cb0ef41Sopenharmony_ci  {
5891cb0ef41Sopenharmony_ci    const _err = new Error('asd');
5901cb0ef41Sopenharmony_ci    const r = new Readable({
5911cb0ef41Sopenharmony_ci      read() {
5921cb0ef41Sopenharmony_ci      },
5931cb0ef41Sopenharmony_ci      destroy(err, callback) {
5941cb0ef41Sopenharmony_ci        setTimeout(() => callback(_err), 1);
5951cb0ef41Sopenharmony_ci      }
5961cb0ef41Sopenharmony_ci    });
5971cb0ef41Sopenharmony_ci
5981cb0ef41Sopenharmony_ci    r.destroy();
5991cb0ef41Sopenharmony_ci    const it = r[Symbol.asyncIterator]();
6001cb0ef41Sopenharmony_ci    it.next().catch(common.mustCall((err) => {
6011cb0ef41Sopenharmony_ci      assert.strictEqual(err, _err);
6021cb0ef41Sopenharmony_ci    }));
6031cb0ef41Sopenharmony_ci  }
6041cb0ef41Sopenharmony_ci
6051cb0ef41Sopenharmony_ci  {
6061cb0ef41Sopenharmony_ci    // Don't destroy if no auto destroy.
6071cb0ef41Sopenharmony_ci    // https://github.com/nodejs/node/issues/35116
6081cb0ef41Sopenharmony_ci
6091cb0ef41Sopenharmony_ci    const r = new Readable({
6101cb0ef41Sopenharmony_ci      autoDestroy: false,
6111cb0ef41Sopenharmony_ci      read() {
6121cb0ef41Sopenharmony_ci        this.push('asd');
6131cb0ef41Sopenharmony_ci        this.push(null);
6141cb0ef41Sopenharmony_ci      }
6151cb0ef41Sopenharmony_ci    });
6161cb0ef41Sopenharmony_ci
6171cb0ef41Sopenharmony_ci    for await (const chunk of r) { } // eslint-disable-line no-unused-vars, no-empty
6181cb0ef41Sopenharmony_ci    assert.strictEqual(r.destroyed, false);
6191cb0ef41Sopenharmony_ci  }
6201cb0ef41Sopenharmony_ci
6211cb0ef41Sopenharmony_ci  {
6221cb0ef41Sopenharmony_ci    // Destroy if no auto destroy and premature break.
6231cb0ef41Sopenharmony_ci    // https://github.com/nodejs/node/pull/35122/files#r485678318
6241cb0ef41Sopenharmony_ci
6251cb0ef41Sopenharmony_ci    const r = new Readable({
6261cb0ef41Sopenharmony_ci      autoDestroy: false,
6271cb0ef41Sopenharmony_ci      read() {
6281cb0ef41Sopenharmony_ci        this.push('asd');
6291cb0ef41Sopenharmony_ci      }
6301cb0ef41Sopenharmony_ci    });
6311cb0ef41Sopenharmony_ci
6321cb0ef41Sopenharmony_ci    for await (const chunk of r) { // eslint-disable-line no-unused-vars
6331cb0ef41Sopenharmony_ci      break;
6341cb0ef41Sopenharmony_ci    }
6351cb0ef41Sopenharmony_ci    assert.strictEqual(r.destroyed, true);
6361cb0ef41Sopenharmony_ci  }
6371cb0ef41Sopenharmony_ci
6381cb0ef41Sopenharmony_ci  {
6391cb0ef41Sopenharmony_ci    // Don't destroy before 'end'.
6401cb0ef41Sopenharmony_ci
6411cb0ef41Sopenharmony_ci    const r = new Readable({
6421cb0ef41Sopenharmony_ci      read() {
6431cb0ef41Sopenharmony_ci        this.push('asd');
6441cb0ef41Sopenharmony_ci        this.push(null);
6451cb0ef41Sopenharmony_ci      }
6461cb0ef41Sopenharmony_ci    }).on('end', () => {
6471cb0ef41Sopenharmony_ci      assert.strictEqual(r.destroyed, false);
6481cb0ef41Sopenharmony_ci    });
6491cb0ef41Sopenharmony_ci
6501cb0ef41Sopenharmony_ci    for await (const chunk of r) { } // eslint-disable-line no-unused-vars, no-empty
6511cb0ef41Sopenharmony_ci    assert.strictEqual(r.destroyed, true);
6521cb0ef41Sopenharmony_ci  }
6531cb0ef41Sopenharmony_ci}
6541cb0ef41Sopenharmony_ci
6551cb0ef41Sopenharmony_ci{
6561cb0ef41Sopenharmony_ci  // AsyncIterator return should end even when destroy
6571cb0ef41Sopenharmony_ci  // does not implement the callback API.
6581cb0ef41Sopenharmony_ci
6591cb0ef41Sopenharmony_ci  const r = new Readable({
6601cb0ef41Sopenharmony_ci    objectMode: true,
6611cb0ef41Sopenharmony_ci    read() {
6621cb0ef41Sopenharmony_ci    }
6631cb0ef41Sopenharmony_ci  });
6641cb0ef41Sopenharmony_ci
6651cb0ef41Sopenharmony_ci  const originalDestroy = r.destroy;
6661cb0ef41Sopenharmony_ci  r.destroy = (err) => {
6671cb0ef41Sopenharmony_ci    originalDestroy.call(r, err);
6681cb0ef41Sopenharmony_ci  };
6691cb0ef41Sopenharmony_ci  const it = r[Symbol.asyncIterator]();
6701cb0ef41Sopenharmony_ci  const p = it.return();
6711cb0ef41Sopenharmony_ci  r.push(null);
6721cb0ef41Sopenharmony_ci  p.then(common.mustCall());
6731cb0ef41Sopenharmony_ci}
6741cb0ef41Sopenharmony_ci
6751cb0ef41Sopenharmony_ci
6761cb0ef41Sopenharmony_ci{
6771cb0ef41Sopenharmony_ci  // AsyncIterator return should not error with
6781cb0ef41Sopenharmony_ci  // premature close.
6791cb0ef41Sopenharmony_ci
6801cb0ef41Sopenharmony_ci  const r = new Readable({
6811cb0ef41Sopenharmony_ci    objectMode: true,
6821cb0ef41Sopenharmony_ci    read() {
6831cb0ef41Sopenharmony_ci    }
6841cb0ef41Sopenharmony_ci  });
6851cb0ef41Sopenharmony_ci
6861cb0ef41Sopenharmony_ci  const originalDestroy = r.destroy;
6871cb0ef41Sopenharmony_ci  r.destroy = (err) => {
6881cb0ef41Sopenharmony_ci    originalDestroy.call(r, err);
6891cb0ef41Sopenharmony_ci  };
6901cb0ef41Sopenharmony_ci  const it = r[Symbol.asyncIterator]();
6911cb0ef41Sopenharmony_ci  const p = it.return();
6921cb0ef41Sopenharmony_ci  r.emit('close');
6931cb0ef41Sopenharmony_ci  p.then(common.mustCall()).catch(common.mustNotCall());
6941cb0ef41Sopenharmony_ci}
6951cb0ef41Sopenharmony_ci
6961cb0ef41Sopenharmony_ci{
6971cb0ef41Sopenharmony_ci  // AsyncIterator should not finish correctly if destroyed.
6981cb0ef41Sopenharmony_ci
6991cb0ef41Sopenharmony_ci  const r = new Readable({
7001cb0ef41Sopenharmony_ci    objectMode: true,
7011cb0ef41Sopenharmony_ci    read() {
7021cb0ef41Sopenharmony_ci    }
7031cb0ef41Sopenharmony_ci  });
7041cb0ef41Sopenharmony_ci
7051cb0ef41Sopenharmony_ci  r.destroy();
7061cb0ef41Sopenharmony_ci  r.on('close', () => {
7071cb0ef41Sopenharmony_ci    const it = r[Symbol.asyncIterator]();
7081cb0ef41Sopenharmony_ci    const next = it.next();
7091cb0ef41Sopenharmony_ci    next
7101cb0ef41Sopenharmony_ci      .then(common.mustNotCall())
7111cb0ef41Sopenharmony_ci      .catch(common.mustCall((err) => {
7121cb0ef41Sopenharmony_ci        assert.strictEqual(err.code, 'ERR_STREAM_PREMATURE_CLOSE');
7131cb0ef41Sopenharmony_ci      }));
7141cb0ef41Sopenharmony_ci  });
7151cb0ef41Sopenharmony_ci}
7161cb0ef41Sopenharmony_ci
7171cb0ef41Sopenharmony_ci{
7181cb0ef41Sopenharmony_ci  // AsyncIterator should throw if prematurely closed
7191cb0ef41Sopenharmony_ci  // before end has been emitted.
7201cb0ef41Sopenharmony_ci  (async function() {
7211cb0ef41Sopenharmony_ci    const readable = fs.createReadStream(__filename);
7221cb0ef41Sopenharmony_ci
7231cb0ef41Sopenharmony_ci    try {
7241cb0ef41Sopenharmony_ci      // eslint-disable-next-line no-unused-vars
7251cb0ef41Sopenharmony_ci      for await (const chunk of readable) {
7261cb0ef41Sopenharmony_ci        readable.close();
7271cb0ef41Sopenharmony_ci      }
7281cb0ef41Sopenharmony_ci
7291cb0ef41Sopenharmony_ci      assert.fail('should have thrown');
7301cb0ef41Sopenharmony_ci    } catch (err) {
7311cb0ef41Sopenharmony_ci      assert.strictEqual(err.code, 'ERR_STREAM_PREMATURE_CLOSE');
7321cb0ef41Sopenharmony_ci    }
7331cb0ef41Sopenharmony_ci
7341cb0ef41Sopenharmony_ci    assert.ok(readable.destroyed);
7351cb0ef41Sopenharmony_ci  })().then(common.mustCall());
7361cb0ef41Sopenharmony_ci}
7371cb0ef41Sopenharmony_ci
7381cb0ef41Sopenharmony_ci// AsyncIterator non-destroying iterator
7391cb0ef41Sopenharmony_ci{
7401cb0ef41Sopenharmony_ci  function createReadable() {
7411cb0ef41Sopenharmony_ci    return Readable.from((async function* () {
7421cb0ef41Sopenharmony_ci      await Promise.resolve();
7431cb0ef41Sopenharmony_ci      yield 5;
7441cb0ef41Sopenharmony_ci      await Promise.resolve();
7451cb0ef41Sopenharmony_ci      yield 7;
7461cb0ef41Sopenharmony_ci      await Promise.resolve();
7471cb0ef41Sopenharmony_ci    })());
7481cb0ef41Sopenharmony_ci  }
7491cb0ef41Sopenharmony_ci
7501cb0ef41Sopenharmony_ci  // Check explicit destroying on return
7511cb0ef41Sopenharmony_ci  (async function() {
7521cb0ef41Sopenharmony_ci    const readable = createReadable();
7531cb0ef41Sopenharmony_ci    for await (const chunk of readable.iterator({ destroyOnReturn: true })) {
7541cb0ef41Sopenharmony_ci      assert.strictEqual(chunk, 5);
7551cb0ef41Sopenharmony_ci      break;
7561cb0ef41Sopenharmony_ci    }
7571cb0ef41Sopenharmony_ci
7581cb0ef41Sopenharmony_ci    assert.ok(readable.destroyed);
7591cb0ef41Sopenharmony_ci  })().then(common.mustCall());
7601cb0ef41Sopenharmony_ci
7611cb0ef41Sopenharmony_ci  // Check explicit non-destroy with return true
7621cb0ef41Sopenharmony_ci  (async function() {
7631cb0ef41Sopenharmony_ci    const readable = createReadable();
7641cb0ef41Sopenharmony_ci    const opts = { destroyOnReturn: false };
7651cb0ef41Sopenharmony_ci    for await (const chunk of readable.iterator(opts)) {
7661cb0ef41Sopenharmony_ci      assert.strictEqual(chunk, 5);
7671cb0ef41Sopenharmony_ci      break;
7681cb0ef41Sopenharmony_ci    }
7691cb0ef41Sopenharmony_ci
7701cb0ef41Sopenharmony_ci    assert.ok(!readable.destroyed);
7711cb0ef41Sopenharmony_ci
7721cb0ef41Sopenharmony_ci    for await (const chunk of readable.iterator(opts)) {
7731cb0ef41Sopenharmony_ci      assert.strictEqual(chunk, 7);
7741cb0ef41Sopenharmony_ci    }
7751cb0ef41Sopenharmony_ci
7761cb0ef41Sopenharmony_ci    assert.ok(readable.destroyed);
7771cb0ef41Sopenharmony_ci  })().then(common.mustCall());
7781cb0ef41Sopenharmony_ci
7791cb0ef41Sopenharmony_ci  // Check non-object options.
7801cb0ef41Sopenharmony_ci  {
7811cb0ef41Sopenharmony_ci    const readable = createReadable();
7821cb0ef41Sopenharmony_ci    assert.throws(
7831cb0ef41Sopenharmony_ci      () => readable.iterator(42),
7841cb0ef41Sopenharmony_ci      {
7851cb0ef41Sopenharmony_ci        code: 'ERR_INVALID_ARG_TYPE',
7861cb0ef41Sopenharmony_ci        name: 'TypeError',
7871cb0ef41Sopenharmony_ci        message: 'The "options" argument must be of type object. Received ' +
7881cb0ef41Sopenharmony_ci                 'type number (42)',
7891cb0ef41Sopenharmony_ci      }
7901cb0ef41Sopenharmony_ci    );
7911cb0ef41Sopenharmony_ci  }
7921cb0ef41Sopenharmony_ci
7931cb0ef41Sopenharmony_ci  // Check for dangling listeners
7941cb0ef41Sopenharmony_ci  (async function() {
7951cb0ef41Sopenharmony_ci    const readable = createReadable();
7961cb0ef41Sopenharmony_ci    const opts = { destroyOnReturn: false };
7971cb0ef41Sopenharmony_ci    while (readable.readable) {
7981cb0ef41Sopenharmony_ci      // eslint-disable-next-line no-unused-vars
7991cb0ef41Sopenharmony_ci      for await (const chunk of readable.iterator(opts)) {
8001cb0ef41Sopenharmony_ci        break;
8011cb0ef41Sopenharmony_ci      }
8021cb0ef41Sopenharmony_ci    }
8031cb0ef41Sopenharmony_ci
8041cb0ef41Sopenharmony_ci    assert.deepStrictEqual(readable.eventNames(), []);
8051cb0ef41Sopenharmony_ci  })().then(common.mustCall());
8061cb0ef41Sopenharmony_ci}
8071cb0ef41Sopenharmony_ci
8081cb0ef41Sopenharmony_ci{
8091cb0ef41Sopenharmony_ci  let _req;
8101cb0ef41Sopenharmony_ci  const server = http.createServer((request, response) => {
8111cb0ef41Sopenharmony_ci    response.statusCode = 404;
8121cb0ef41Sopenharmony_ci    response.write('never ends');
8131cb0ef41Sopenharmony_ci  });
8141cb0ef41Sopenharmony_ci
8151cb0ef41Sopenharmony_ci  server.listen(() => {
8161cb0ef41Sopenharmony_ci    _req = http.request(`http://localhost:${server.address().port}`)
8171cb0ef41Sopenharmony_ci      .on('response', common.mustCall(async (res) => {
8181cb0ef41Sopenharmony_ci        setTimeout(() => {
8191cb0ef41Sopenharmony_ci          _req.destroy(new Error('something happened'));
8201cb0ef41Sopenharmony_ci        }, 100);
8211cb0ef41Sopenharmony_ci
8221cb0ef41Sopenharmony_ci        res.on('error', common.mustCall());
8231cb0ef41Sopenharmony_ci
8241cb0ef41Sopenharmony_ci        let _err;
8251cb0ef41Sopenharmony_ci        try {
8261cb0ef41Sopenharmony_ci          // eslint-disable-next-line no-unused-vars, no-empty
8271cb0ef41Sopenharmony_ci          for await (const chunk of res) { }
8281cb0ef41Sopenharmony_ci        } catch (err) {
8291cb0ef41Sopenharmony_ci          _err = err;
8301cb0ef41Sopenharmony_ci        }
8311cb0ef41Sopenharmony_ci
8321cb0ef41Sopenharmony_ci        assert.strictEqual(_err.code, 'ECONNRESET');
8331cb0ef41Sopenharmony_ci        server.close();
8341cb0ef41Sopenharmony_ci      }))
8351cb0ef41Sopenharmony_ci      .on('error', common.mustCall())
8361cb0ef41Sopenharmony_ci      .end();
8371cb0ef41Sopenharmony_ci  });
8381cb0ef41Sopenharmony_ci}
8391cb0ef41Sopenharmony_ci
8401cb0ef41Sopenharmony_ci{
8411cb0ef41Sopenharmony_ci  async function getParsedBody(request) {
8421cb0ef41Sopenharmony_ci    let body = '';
8431cb0ef41Sopenharmony_ci
8441cb0ef41Sopenharmony_ci    for await (const data of request) {
8451cb0ef41Sopenharmony_ci      body += data;
8461cb0ef41Sopenharmony_ci    }
8471cb0ef41Sopenharmony_ci
8481cb0ef41Sopenharmony_ci    try {
8491cb0ef41Sopenharmony_ci      return JSON.parse(body);
8501cb0ef41Sopenharmony_ci    } catch {
8511cb0ef41Sopenharmony_ci      return {};
8521cb0ef41Sopenharmony_ci    }
8531cb0ef41Sopenharmony_ci  }
8541cb0ef41Sopenharmony_ci
8551cb0ef41Sopenharmony_ci  const str = JSON.stringify({ asd: true });
8561cb0ef41Sopenharmony_ci  const server = http.createServer(async (request, response) => {
8571cb0ef41Sopenharmony_ci    const body = await getParsedBody(request);
8581cb0ef41Sopenharmony_ci    response.statusCode = 200;
8591cb0ef41Sopenharmony_ci    assert.strictEqual(JSON.stringify(body), str);
8601cb0ef41Sopenharmony_ci    response.end(JSON.stringify(body));
8611cb0ef41Sopenharmony_ci  }).listen(() => {
8621cb0ef41Sopenharmony_ci    http
8631cb0ef41Sopenharmony_ci      .request({
8641cb0ef41Sopenharmony_ci        method: 'POST',
8651cb0ef41Sopenharmony_ci        hostname: 'localhost',
8661cb0ef41Sopenharmony_ci        port: server.address().port,
8671cb0ef41Sopenharmony_ci      })
8681cb0ef41Sopenharmony_ci      .end(str)
8691cb0ef41Sopenharmony_ci      .on('response', async (res) => {
8701cb0ef41Sopenharmony_ci        let body = '';
8711cb0ef41Sopenharmony_ci        for await (const chunk of res) {
8721cb0ef41Sopenharmony_ci          body += chunk;
8731cb0ef41Sopenharmony_ci        }
8741cb0ef41Sopenharmony_ci        assert.strictEqual(body, str);
8751cb0ef41Sopenharmony_ci        server.close();
8761cb0ef41Sopenharmony_ci      });
8771cb0ef41Sopenharmony_ci  });
8781cb0ef41Sopenharmony_ci}
8791cb0ef41Sopenharmony_ci
8801cb0ef41Sopenharmony_ci// To avoid missing some tests if a promise does not resolve
8811cb0ef41Sopenharmony_citests().then(common.mustCall());
882