11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst { Readable, PassThrough } = require('stream');
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_cifunction test(r) {
61cb0ef41Sopenharmony_ci  const wrapper = new Readable({
71cb0ef41Sopenharmony_ci    read: () => {
81cb0ef41Sopenharmony_ci      let data = r.read();
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci      if (data) {
111cb0ef41Sopenharmony_ci        wrapper.push(data);
121cb0ef41Sopenharmony_ci        return;
131cb0ef41Sopenharmony_ci      }
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci      r.once('readable', function() {
161cb0ef41Sopenharmony_ci        data = r.read();
171cb0ef41Sopenharmony_ci        if (data) {
181cb0ef41Sopenharmony_ci          wrapper.push(data);
191cb0ef41Sopenharmony_ci        }
201cb0ef41Sopenharmony_ci        // else: the end event should fire
211cb0ef41Sopenharmony_ci      });
221cb0ef41Sopenharmony_ci    },
231cb0ef41Sopenharmony_ci  });
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci  r.once('end', function() {
261cb0ef41Sopenharmony_ci    wrapper.push(null);
271cb0ef41Sopenharmony_ci  });
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci  wrapper.resume();
301cb0ef41Sopenharmony_ci  wrapper.once('end', common.mustCall());
311cb0ef41Sopenharmony_ci}
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci{
341cb0ef41Sopenharmony_ci  const source = new Readable({
351cb0ef41Sopenharmony_ci    read: () => {}
361cb0ef41Sopenharmony_ci  });
371cb0ef41Sopenharmony_ci  source.push('foo');
381cb0ef41Sopenharmony_ci  source.push('bar');
391cb0ef41Sopenharmony_ci  source.push(null);
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci  const pt = source.pipe(new PassThrough());
421cb0ef41Sopenharmony_ci  test(pt);
431cb0ef41Sopenharmony_ci}
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci{
461cb0ef41Sopenharmony_ci  // This is the underlying cause of the above test case.
471cb0ef41Sopenharmony_ci  const pushChunks = ['foo', 'bar'];
481cb0ef41Sopenharmony_ci  const r = new Readable({
491cb0ef41Sopenharmony_ci    read: () => {
501cb0ef41Sopenharmony_ci      const chunk = pushChunks.shift();
511cb0ef41Sopenharmony_ci      if (chunk) {
521cb0ef41Sopenharmony_ci        // synchronous call
531cb0ef41Sopenharmony_ci        r.push(chunk);
541cb0ef41Sopenharmony_ci      } else {
551cb0ef41Sopenharmony_ci        // asynchronous call
561cb0ef41Sopenharmony_ci        process.nextTick(() => r.push(null));
571cb0ef41Sopenharmony_ci      }
581cb0ef41Sopenharmony_ci    },
591cb0ef41Sopenharmony_ci  });
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci  test(r);
621cb0ef41Sopenharmony_ci}
63