11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst assert = require('assert');
41cb0ef41Sopenharmony_ciconst Readable = require('stream').Readable;
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst readable = new Readable({
71cb0ef41Sopenharmony_ci  read: () => {}
81cb0ef41Sopenharmony_ci});
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci// Initialized to false.
111cb0ef41Sopenharmony_ciassert.strictEqual(readable._readableState.emittedReadable, false);
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciconst expected = [Buffer.from('foobar'), Buffer.from('quo'), null];
141cb0ef41Sopenharmony_cireadable.on('readable', common.mustCall(() => {
151cb0ef41Sopenharmony_ci  // emittedReadable should be true when the readable event is emitted
161cb0ef41Sopenharmony_ci  assert.strictEqual(readable._readableState.emittedReadable, true);
171cb0ef41Sopenharmony_ci  assert.deepStrictEqual(readable.read(), expected.shift());
181cb0ef41Sopenharmony_ci  // emittedReadable is reset to false during read()
191cb0ef41Sopenharmony_ci  assert.strictEqual(readable._readableState.emittedReadable, false);
201cb0ef41Sopenharmony_ci}, 3));
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci// When the first readable listener is just attached,
231cb0ef41Sopenharmony_ci// emittedReadable should be false
241cb0ef41Sopenharmony_ciassert.strictEqual(readable._readableState.emittedReadable, false);
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci// These trigger a single 'readable', as things are batched up
271cb0ef41Sopenharmony_ciprocess.nextTick(common.mustCall(() => {
281cb0ef41Sopenharmony_ci  readable.push('foo');
291cb0ef41Sopenharmony_ci}));
301cb0ef41Sopenharmony_ciprocess.nextTick(common.mustCall(() => {
311cb0ef41Sopenharmony_ci  readable.push('bar');
321cb0ef41Sopenharmony_ci}));
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci// These triggers two readable events
351cb0ef41Sopenharmony_cisetImmediate(common.mustCall(() => {
361cb0ef41Sopenharmony_ci  readable.push('quo');
371cb0ef41Sopenharmony_ci  process.nextTick(common.mustCall(() => {
381cb0ef41Sopenharmony_ci    readable.push(null);
391cb0ef41Sopenharmony_ci  }));
401cb0ef41Sopenharmony_ci}));
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ciconst noRead = new Readable({
431cb0ef41Sopenharmony_ci  read: () => {}
441cb0ef41Sopenharmony_ci});
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_cinoRead.on('readable', common.mustCall(() => {
471cb0ef41Sopenharmony_ci  // emittedReadable should be true when the readable event is emitted
481cb0ef41Sopenharmony_ci  assert.strictEqual(noRead._readableState.emittedReadable, true);
491cb0ef41Sopenharmony_ci  noRead.read(0);
501cb0ef41Sopenharmony_ci  // emittedReadable is not reset during read(0)
511cb0ef41Sopenharmony_ci  assert.strictEqual(noRead._readableState.emittedReadable, true);
521cb0ef41Sopenharmony_ci}));
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_cinoRead.push('foo');
551cb0ef41Sopenharmony_cinoRead.push(null);
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ciconst flowing = new Readable({
581cb0ef41Sopenharmony_ci  read: () => {}
591cb0ef41Sopenharmony_ci});
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ciflowing.on('data', common.mustCall(() => {
621cb0ef41Sopenharmony_ci  // When in flowing mode, emittedReadable is always false.
631cb0ef41Sopenharmony_ci  assert.strictEqual(flowing._readableState.emittedReadable, false);
641cb0ef41Sopenharmony_ci  flowing.read();
651cb0ef41Sopenharmony_ci  assert.strictEqual(flowing._readableState.emittedReadable, false);
661cb0ef41Sopenharmony_ci}, 3));
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ciflowing.push('foooo');
691cb0ef41Sopenharmony_ciflowing.push('bar');
701cb0ef41Sopenharmony_ciflowing.push('quo');
711cb0ef41Sopenharmony_ciprocess.nextTick(common.mustCall(() => {
721cb0ef41Sopenharmony_ci  flowing.push(null);
731cb0ef41Sopenharmony_ci}));
74