11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ciconst { Readable } = require('stream');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciconst MAX = 42;
81cb0ef41Sopenharmony_ciconst BATCH = 10;
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci{
111cb0ef41Sopenharmony_ci  const readable = new Readable({
121cb0ef41Sopenharmony_ci    objectMode: true,
131cb0ef41Sopenharmony_ci    read: common.mustCall(function() {
141cb0ef41Sopenharmony_ci      console.log('>> READ');
151cb0ef41Sopenharmony_ci      fetchData((err, data) => {
161cb0ef41Sopenharmony_ci        if (err) {
171cb0ef41Sopenharmony_ci          this.destroy(err);
181cb0ef41Sopenharmony_ci          return;
191cb0ef41Sopenharmony_ci        }
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci        if (data.length === 0) {
221cb0ef41Sopenharmony_ci          console.log('pushing null');
231cb0ef41Sopenharmony_ci          this.push(null);
241cb0ef41Sopenharmony_ci          return;
251cb0ef41Sopenharmony_ci        }
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci        console.log('pushing');
281cb0ef41Sopenharmony_ci        data.forEach((d) => this.push(d));
291cb0ef41Sopenharmony_ci      });
301cb0ef41Sopenharmony_ci    }, Math.floor(MAX / BATCH) + 2)
311cb0ef41Sopenharmony_ci  });
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci  let i = 0;
341cb0ef41Sopenharmony_ci  function fetchData(cb) {
351cb0ef41Sopenharmony_ci    if (i > MAX) {
361cb0ef41Sopenharmony_ci      setTimeout(cb, 10, null, []);
371cb0ef41Sopenharmony_ci    } else {
381cb0ef41Sopenharmony_ci      const array = [];
391cb0ef41Sopenharmony_ci      const max = i + BATCH;
401cb0ef41Sopenharmony_ci      for (; i < max; i++) {
411cb0ef41Sopenharmony_ci        array.push(i);
421cb0ef41Sopenharmony_ci      }
431cb0ef41Sopenharmony_ci      setTimeout(cb, 10, null, array);
441cb0ef41Sopenharmony_ci    }
451cb0ef41Sopenharmony_ci  }
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci  readable.on('readable', () => {
481cb0ef41Sopenharmony_ci    let data;
491cb0ef41Sopenharmony_ci    console.log('readable emitted');
501cb0ef41Sopenharmony_ci    while ((data = readable.read()) !== null) {
511cb0ef41Sopenharmony_ci      console.log(data);
521cb0ef41Sopenharmony_ci    }
531cb0ef41Sopenharmony_ci  });
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci  readable.on('end', common.mustCall(() => {
561cb0ef41Sopenharmony_ci    assert.strictEqual(i, (Math.floor(MAX / BATCH) + 1) * BATCH);
571cb0ef41Sopenharmony_ci  }));
581cb0ef41Sopenharmony_ci}
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ci{
611cb0ef41Sopenharmony_ci  const readable = new Readable({
621cb0ef41Sopenharmony_ci    objectMode: true,
631cb0ef41Sopenharmony_ci    read: common.mustCall(function() {
641cb0ef41Sopenharmony_ci      console.log('>> READ');
651cb0ef41Sopenharmony_ci      fetchData((err, data) => {
661cb0ef41Sopenharmony_ci        if (err) {
671cb0ef41Sopenharmony_ci          this.destroy(err);
681cb0ef41Sopenharmony_ci          return;
691cb0ef41Sopenharmony_ci        }
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_ci        if (data.length === 0) {
721cb0ef41Sopenharmony_ci          console.log('pushing null');
731cb0ef41Sopenharmony_ci          this.push(null);
741cb0ef41Sopenharmony_ci          return;
751cb0ef41Sopenharmony_ci        }
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_ci        console.log('pushing');
781cb0ef41Sopenharmony_ci        data.forEach((d) => this.push(d));
791cb0ef41Sopenharmony_ci      });
801cb0ef41Sopenharmony_ci    }, Math.floor(MAX / BATCH) + 2)
811cb0ef41Sopenharmony_ci  });
821cb0ef41Sopenharmony_ci
831cb0ef41Sopenharmony_ci  let i = 0;
841cb0ef41Sopenharmony_ci  function fetchData(cb) {
851cb0ef41Sopenharmony_ci    if (i > MAX) {
861cb0ef41Sopenharmony_ci      setTimeout(cb, 10, null, []);
871cb0ef41Sopenharmony_ci    } else {
881cb0ef41Sopenharmony_ci      const array = [];
891cb0ef41Sopenharmony_ci      const max = i + BATCH;
901cb0ef41Sopenharmony_ci      for (; i < max; i++) {
911cb0ef41Sopenharmony_ci        array.push(i);
921cb0ef41Sopenharmony_ci      }
931cb0ef41Sopenharmony_ci      setTimeout(cb, 10, null, array);
941cb0ef41Sopenharmony_ci    }
951cb0ef41Sopenharmony_ci  }
961cb0ef41Sopenharmony_ci
971cb0ef41Sopenharmony_ci  readable.on('data', (data) => {
981cb0ef41Sopenharmony_ci    console.log('data emitted', data);
991cb0ef41Sopenharmony_ci  });
1001cb0ef41Sopenharmony_ci
1011cb0ef41Sopenharmony_ci  readable.on('end', common.mustCall(() => {
1021cb0ef41Sopenharmony_ci    assert.strictEqual(i, (Math.floor(MAX / BATCH) + 1) * BATCH);
1031cb0ef41Sopenharmony_ci  }));
1041cb0ef41Sopenharmony_ci}
1051cb0ef41Sopenharmony_ci
1061cb0ef41Sopenharmony_ci{
1071cb0ef41Sopenharmony_ci  const readable = new Readable({
1081cb0ef41Sopenharmony_ci    objectMode: true,
1091cb0ef41Sopenharmony_ci    read: common.mustCall(function() {
1101cb0ef41Sopenharmony_ci      console.log('>> READ');
1111cb0ef41Sopenharmony_ci      fetchData((err, data) => {
1121cb0ef41Sopenharmony_ci        if (err) {
1131cb0ef41Sopenharmony_ci          this.destroy(err);
1141cb0ef41Sopenharmony_ci          return;
1151cb0ef41Sopenharmony_ci        }
1161cb0ef41Sopenharmony_ci
1171cb0ef41Sopenharmony_ci        console.log('pushing');
1181cb0ef41Sopenharmony_ci        data.forEach((d) => this.push(d));
1191cb0ef41Sopenharmony_ci
1201cb0ef41Sopenharmony_ci        if (data[BATCH - 1] >= MAX) {
1211cb0ef41Sopenharmony_ci          console.log('pushing null');
1221cb0ef41Sopenharmony_ci          this.push(null);
1231cb0ef41Sopenharmony_ci        }
1241cb0ef41Sopenharmony_ci      });
1251cb0ef41Sopenharmony_ci    }, Math.floor(MAX / BATCH) + 1)
1261cb0ef41Sopenharmony_ci  });
1271cb0ef41Sopenharmony_ci
1281cb0ef41Sopenharmony_ci  let i = 0;
1291cb0ef41Sopenharmony_ci  function fetchData(cb) {
1301cb0ef41Sopenharmony_ci    const array = [];
1311cb0ef41Sopenharmony_ci    const max = i + BATCH;
1321cb0ef41Sopenharmony_ci    for (; i < max; i++) {
1331cb0ef41Sopenharmony_ci      array.push(i);
1341cb0ef41Sopenharmony_ci    }
1351cb0ef41Sopenharmony_ci    setTimeout(cb, 10, null, array);
1361cb0ef41Sopenharmony_ci  }
1371cb0ef41Sopenharmony_ci
1381cb0ef41Sopenharmony_ci  readable.on('data', (data) => {
1391cb0ef41Sopenharmony_ci    console.log('data emitted', data);
1401cb0ef41Sopenharmony_ci  });
1411cb0ef41Sopenharmony_ci
1421cb0ef41Sopenharmony_ci  readable.on('end', common.mustCall(() => {
1431cb0ef41Sopenharmony_ci    assert.strictEqual(i, (Math.floor(MAX / BATCH) + 1) * BATCH);
1441cb0ef41Sopenharmony_ci  }));
1451cb0ef41Sopenharmony_ci}
1461cb0ef41Sopenharmony_ci
1471cb0ef41Sopenharmony_ci{
1481cb0ef41Sopenharmony_ci  const readable = new Readable({
1491cb0ef41Sopenharmony_ci    objectMode: true,
1501cb0ef41Sopenharmony_ci    read: common.mustNotCall()
1511cb0ef41Sopenharmony_ci  });
1521cb0ef41Sopenharmony_ci
1531cb0ef41Sopenharmony_ci  readable.on('data', common.mustNotCall());
1541cb0ef41Sopenharmony_ci
1551cb0ef41Sopenharmony_ci  readable.push(null);
1561cb0ef41Sopenharmony_ci
1571cb0ef41Sopenharmony_ci  let nextTickPassed = false;
1581cb0ef41Sopenharmony_ci  process.nextTick(() => {
1591cb0ef41Sopenharmony_ci    nextTickPassed = true;
1601cb0ef41Sopenharmony_ci  });
1611cb0ef41Sopenharmony_ci
1621cb0ef41Sopenharmony_ci  readable.on('end', common.mustCall(() => {
1631cb0ef41Sopenharmony_ci    assert.strictEqual(nextTickPassed, true);
1641cb0ef41Sopenharmony_ci  }));
1651cb0ef41Sopenharmony_ci}
1661cb0ef41Sopenharmony_ci
1671cb0ef41Sopenharmony_ci{
1681cb0ef41Sopenharmony_ci  const readable = new Readable({
1691cb0ef41Sopenharmony_ci    objectMode: true,
1701cb0ef41Sopenharmony_ci    read: common.mustCall()
1711cb0ef41Sopenharmony_ci  });
1721cb0ef41Sopenharmony_ci
1731cb0ef41Sopenharmony_ci  readable.on('data', (data) => {
1741cb0ef41Sopenharmony_ci    console.log('data emitted', data);
1751cb0ef41Sopenharmony_ci  });
1761cb0ef41Sopenharmony_ci
1771cb0ef41Sopenharmony_ci  readable.on('end', common.mustCall());
1781cb0ef41Sopenharmony_ci
1791cb0ef41Sopenharmony_ci  setImmediate(() => {
1801cb0ef41Sopenharmony_ci    readable.push('aaa');
1811cb0ef41Sopenharmony_ci    readable.push(null);
1821cb0ef41Sopenharmony_ci  });
1831cb0ef41Sopenharmony_ci}
184