11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciconst common = require('../common'); 41cb0ef41Sopenharmony_ciconst assert = require('assert'); 51cb0ef41Sopenharmony_ciconst { Readable } = require('stream'); 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_cilet ticks = 18; 81cb0ef41Sopenharmony_cilet expectedData = 19; 91cb0ef41Sopenharmony_ci 101cb0ef41Sopenharmony_ciconst rs = new Readable({ 111cb0ef41Sopenharmony_ci objectMode: true, 121cb0ef41Sopenharmony_ci read: () => { 131cb0ef41Sopenharmony_ci if (ticks-- > 0) 141cb0ef41Sopenharmony_ci return process.nextTick(() => rs.push({})); 151cb0ef41Sopenharmony_ci rs.push({}); 161cb0ef41Sopenharmony_ci rs.push(null); 171cb0ef41Sopenharmony_ci } 181cb0ef41Sopenharmony_ci}); 191cb0ef41Sopenharmony_ci 201cb0ef41Sopenharmony_cirs.on('end', common.mustCall()); 211cb0ef41Sopenharmony_cireadAndPause(); 221cb0ef41Sopenharmony_ci 231cb0ef41Sopenharmony_cifunction readAndPause() { 241cb0ef41Sopenharmony_ci // Does a on(data) -> pause -> wait -> resume -> on(data) ... loop. 251cb0ef41Sopenharmony_ci // Expects on(data) to never fire if the stream is paused. 261cb0ef41Sopenharmony_ci const ondata = common.mustCall((data) => { 271cb0ef41Sopenharmony_ci rs.pause(); 281cb0ef41Sopenharmony_ci 291cb0ef41Sopenharmony_ci expectedData--; 301cb0ef41Sopenharmony_ci if (expectedData <= 0) 311cb0ef41Sopenharmony_ci return; 321cb0ef41Sopenharmony_ci 331cb0ef41Sopenharmony_ci setImmediate(function() { 341cb0ef41Sopenharmony_ci rs.removeListener('data', ondata); 351cb0ef41Sopenharmony_ci readAndPause(); 361cb0ef41Sopenharmony_ci rs.resume(); 371cb0ef41Sopenharmony_ci }); 381cb0ef41Sopenharmony_ci }, 1); // Only call ondata once 391cb0ef41Sopenharmony_ci 401cb0ef41Sopenharmony_ci rs.on('data', ondata); 411cb0ef41Sopenharmony_ci} 421cb0ef41Sopenharmony_ci 431cb0ef41Sopenharmony_ci{ 441cb0ef41Sopenharmony_ci const readable = new Readable({ 451cb0ef41Sopenharmony_ci read() {} 461cb0ef41Sopenharmony_ci }); 471cb0ef41Sopenharmony_ci 481cb0ef41Sopenharmony_ci function read() {} 491cb0ef41Sopenharmony_ci 501cb0ef41Sopenharmony_ci readable.setEncoding('utf8'); 511cb0ef41Sopenharmony_ci readable.on('readable', read); 521cb0ef41Sopenharmony_ci readable.removeListener('readable', read); 531cb0ef41Sopenharmony_ci readable.pause(); 541cb0ef41Sopenharmony_ci 551cb0ef41Sopenharmony_ci process.nextTick(function() { 561cb0ef41Sopenharmony_ci assert(readable.isPaused()); 571cb0ef41Sopenharmony_ci }); 581cb0ef41Sopenharmony_ci} 591cb0ef41Sopenharmony_ci 601cb0ef41Sopenharmony_ci{ 611cb0ef41Sopenharmony_ci const { PassThrough } = require('stream'); 621cb0ef41Sopenharmony_ci 631cb0ef41Sopenharmony_ci const source3 = new PassThrough(); 641cb0ef41Sopenharmony_ci const target3 = new PassThrough(); 651cb0ef41Sopenharmony_ci 661cb0ef41Sopenharmony_ci const chunk = Buffer.allocUnsafe(1000); 671cb0ef41Sopenharmony_ci while (target3.write(chunk)); 681cb0ef41Sopenharmony_ci 691cb0ef41Sopenharmony_ci source3.pipe(target3); 701cb0ef41Sopenharmony_ci target3.on('drain', common.mustCall(() => { 711cb0ef41Sopenharmony_ci assert(!source3.isPaused()); 721cb0ef41Sopenharmony_ci })); 731cb0ef41Sopenharmony_ci target3.on('data', () => {}); 741cb0ef41Sopenharmony_ci} 75