11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst assert = require('assert');
41cb0ef41Sopenharmony_ciconst Readable = require('stream').Readable;
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ci{
71cb0ef41Sopenharmony_ci  const readable = new Readable({
81cb0ef41Sopenharmony_ci    read(size) {}
91cb0ef41Sopenharmony_ci  });
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ci  const state = readable._readableState;
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ci  // Starting off with false initially.
141cb0ef41Sopenharmony_ci  assert.strictEqual(state.reading, false);
151cb0ef41Sopenharmony_ci  assert.strictEqual(state.readingMore, false);
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ci  readable.on('data', common.mustCall((data) => {
181cb0ef41Sopenharmony_ci    // While in a flowing state with a 'readable' listener
191cb0ef41Sopenharmony_ci    // we should not be reading more
201cb0ef41Sopenharmony_ci    if (readable.readableFlowing)
211cb0ef41Sopenharmony_ci      assert.strictEqual(state.readingMore, true);
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci    // Reading as long as we've not ended
241cb0ef41Sopenharmony_ci    assert.strictEqual(state.reading, !state.ended);
251cb0ef41Sopenharmony_ci  }, 2));
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci  function onStreamEnd() {
281cb0ef41Sopenharmony_ci    // End of stream; state.reading is false
291cb0ef41Sopenharmony_ci    // And so should be readingMore.
301cb0ef41Sopenharmony_ci    assert.strictEqual(state.readingMore, false);
311cb0ef41Sopenharmony_ci    assert.strictEqual(state.reading, false);
321cb0ef41Sopenharmony_ci  }
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci  const expectedReadingMore = [true, true, false];
351cb0ef41Sopenharmony_ci  readable.on('readable', common.mustCall(() => {
361cb0ef41Sopenharmony_ci    // There is only one readingMore scheduled from on('data'),
371cb0ef41Sopenharmony_ci    // after which everything is governed by the .read() call
381cb0ef41Sopenharmony_ci    assert.strictEqual(state.readingMore, expectedReadingMore.shift());
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci    // If the stream has ended, we shouldn't be reading
411cb0ef41Sopenharmony_ci    assert.strictEqual(state.ended, !state.reading);
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci    // Consume all the data
441cb0ef41Sopenharmony_ci    while (readable.read() !== null);
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci    if (expectedReadingMore.length === 0) // Reached end of stream
471cb0ef41Sopenharmony_ci      process.nextTick(common.mustCall(onStreamEnd, 1));
481cb0ef41Sopenharmony_ci  }, 3));
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci  readable.on('end', common.mustCall(onStreamEnd));
511cb0ef41Sopenharmony_ci  readable.push('pushed');
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci  readable.read(6);
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci  // reading
561cb0ef41Sopenharmony_ci  assert.strictEqual(state.reading, true);
571cb0ef41Sopenharmony_ci  assert.strictEqual(state.readingMore, true);
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci  // add chunk to front
601cb0ef41Sopenharmony_ci  readable.unshift('unshifted');
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci  // end
631cb0ef41Sopenharmony_ci  readable.push(null);
641cb0ef41Sopenharmony_ci}
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ci{
671cb0ef41Sopenharmony_ci  const readable = new Readable({
681cb0ef41Sopenharmony_ci    read(size) {}
691cb0ef41Sopenharmony_ci  });
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_ci  const state = readable._readableState;
721cb0ef41Sopenharmony_ci
731cb0ef41Sopenharmony_ci  // Starting off with false initially.
741cb0ef41Sopenharmony_ci  assert.strictEqual(state.reading, false);
751cb0ef41Sopenharmony_ci  assert.strictEqual(state.readingMore, false);
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_ci  readable.on('data', common.mustCall((data) => {
781cb0ef41Sopenharmony_ci    // While in a flowing state without a 'readable' listener
791cb0ef41Sopenharmony_ci    // we should be reading more
801cb0ef41Sopenharmony_ci    if (readable.readableFlowing)
811cb0ef41Sopenharmony_ci      assert.strictEqual(state.readingMore, true);
821cb0ef41Sopenharmony_ci
831cb0ef41Sopenharmony_ci    // Reading as long as we've not ended
841cb0ef41Sopenharmony_ci    assert.strictEqual(state.reading, !state.ended);
851cb0ef41Sopenharmony_ci  }, 2));
861cb0ef41Sopenharmony_ci
871cb0ef41Sopenharmony_ci  function onStreamEnd() {
881cb0ef41Sopenharmony_ci    // End of stream; state.reading is false
891cb0ef41Sopenharmony_ci    // And so should be readingMore.
901cb0ef41Sopenharmony_ci    assert.strictEqual(state.readingMore, false);
911cb0ef41Sopenharmony_ci    assert.strictEqual(state.reading, false);
921cb0ef41Sopenharmony_ci  }
931cb0ef41Sopenharmony_ci
941cb0ef41Sopenharmony_ci  readable.on('end', common.mustCall(onStreamEnd));
951cb0ef41Sopenharmony_ci  readable.push('pushed');
961cb0ef41Sopenharmony_ci
971cb0ef41Sopenharmony_ci  // Stop emitting 'data' events
981cb0ef41Sopenharmony_ci  assert.strictEqual(state.flowing, true);
991cb0ef41Sopenharmony_ci  readable.pause();
1001cb0ef41Sopenharmony_ci
1011cb0ef41Sopenharmony_ci  // paused
1021cb0ef41Sopenharmony_ci  assert.strictEqual(state.reading, false);
1031cb0ef41Sopenharmony_ci  assert.strictEqual(state.flowing, false);
1041cb0ef41Sopenharmony_ci
1051cb0ef41Sopenharmony_ci  readable.resume();
1061cb0ef41Sopenharmony_ci  assert.strictEqual(state.reading, false);
1071cb0ef41Sopenharmony_ci  assert.strictEqual(state.flowing, true);
1081cb0ef41Sopenharmony_ci
1091cb0ef41Sopenharmony_ci  // add chunk to front
1101cb0ef41Sopenharmony_ci  readable.unshift('unshifted');
1111cb0ef41Sopenharmony_ci
1121cb0ef41Sopenharmony_ci  // end
1131cb0ef41Sopenharmony_ci  readable.push(null);
1141cb0ef41Sopenharmony_ci}
1151cb0ef41Sopenharmony_ci
1161cb0ef41Sopenharmony_ci{
1171cb0ef41Sopenharmony_ci  const readable = new Readable({
1181cb0ef41Sopenharmony_ci    read(size) {}
1191cb0ef41Sopenharmony_ci  });
1201cb0ef41Sopenharmony_ci
1211cb0ef41Sopenharmony_ci  const state = readable._readableState;
1221cb0ef41Sopenharmony_ci
1231cb0ef41Sopenharmony_ci  // Starting off with false initially.
1241cb0ef41Sopenharmony_ci  assert.strictEqual(state.reading, false);
1251cb0ef41Sopenharmony_ci  assert.strictEqual(state.readingMore, false);
1261cb0ef41Sopenharmony_ci
1271cb0ef41Sopenharmony_ci  const onReadable = common.mustNotCall();
1281cb0ef41Sopenharmony_ci
1291cb0ef41Sopenharmony_ci  readable.on('readable', onReadable);
1301cb0ef41Sopenharmony_ci
1311cb0ef41Sopenharmony_ci  readable.on('data', common.mustCall((data) => {
1321cb0ef41Sopenharmony_ci    // Reading as long as we've not ended
1331cb0ef41Sopenharmony_ci    assert.strictEqual(state.reading, !state.ended);
1341cb0ef41Sopenharmony_ci  }, 2));
1351cb0ef41Sopenharmony_ci
1361cb0ef41Sopenharmony_ci  readable.removeListener('readable', onReadable);
1371cb0ef41Sopenharmony_ci
1381cb0ef41Sopenharmony_ci  function onStreamEnd() {
1391cb0ef41Sopenharmony_ci    // End of stream; state.reading is false
1401cb0ef41Sopenharmony_ci    // And so should be readingMore.
1411cb0ef41Sopenharmony_ci    assert.strictEqual(state.readingMore, false);
1421cb0ef41Sopenharmony_ci    assert.strictEqual(state.reading, false);
1431cb0ef41Sopenharmony_ci  }
1441cb0ef41Sopenharmony_ci
1451cb0ef41Sopenharmony_ci  readable.on('end', common.mustCall(onStreamEnd));
1461cb0ef41Sopenharmony_ci  readable.push('pushed');
1471cb0ef41Sopenharmony_ci
1481cb0ef41Sopenharmony_ci  // We are still not flowing, we will be resuming in the next tick
1491cb0ef41Sopenharmony_ci  assert.strictEqual(state.flowing, false);
1501cb0ef41Sopenharmony_ci
1511cb0ef41Sopenharmony_ci  // Wait for nextTick, so the readableListener flag resets
1521cb0ef41Sopenharmony_ci  process.nextTick(function() {
1531cb0ef41Sopenharmony_ci    readable.resume();
1541cb0ef41Sopenharmony_ci
1551cb0ef41Sopenharmony_ci    // Stop emitting 'data' events
1561cb0ef41Sopenharmony_ci    assert.strictEqual(state.flowing, true);
1571cb0ef41Sopenharmony_ci    readable.pause();
1581cb0ef41Sopenharmony_ci
1591cb0ef41Sopenharmony_ci    // paused
1601cb0ef41Sopenharmony_ci    assert.strictEqual(state.flowing, false);
1611cb0ef41Sopenharmony_ci
1621cb0ef41Sopenharmony_ci    readable.resume();
1631cb0ef41Sopenharmony_ci    assert.strictEqual(state.flowing, true);
1641cb0ef41Sopenharmony_ci
1651cb0ef41Sopenharmony_ci    // add chunk to front
1661cb0ef41Sopenharmony_ci    readable.unshift('unshifted');
1671cb0ef41Sopenharmony_ci
1681cb0ef41Sopenharmony_ci    // end
1691cb0ef41Sopenharmony_ci    readable.push(null);
1701cb0ef41Sopenharmony_ci  });
1711cb0ef41Sopenharmony_ci}
172