11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ciconst { Readable } = require('stream');
61cb0ef41Sopenharmony_ciconst readline = require('readline');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciconst CONTENT = 'content';
91cb0ef41Sopenharmony_ciconst TOTAL_LINES = 18;
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ci(async () => {
121cb0ef41Sopenharmony_ci  const readable = new Readable({ read() {} });
131cb0ef41Sopenharmony_ci  readable.push(`${CONTENT}\n`.repeat(TOTAL_LINES));
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci  const rli = readline.createInterface({
161cb0ef41Sopenharmony_ci    input: readable,
171cb0ef41Sopenharmony_ci    crlfDelay: Infinity
181cb0ef41Sopenharmony_ci  });
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci  const it = rli[Symbol.asyncIterator]();
211cb0ef41Sopenharmony_ci  const highWaterMark = it.stream.readableHighWaterMark;
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci  // For this test to work, we have to queue up more than the number of
241cb0ef41Sopenharmony_ci  // highWaterMark items in rli. Make sure that is the case.
251cb0ef41Sopenharmony_ci  assert(TOTAL_LINES > highWaterMark);
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci  let iterations = 0;
281cb0ef41Sopenharmony_ci  let readableEnded = false;
291cb0ef41Sopenharmony_ci  for await (const line of it) {
301cb0ef41Sopenharmony_ci    assert.strictEqual(readableEnded, false);
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci    assert.strictEqual(line, CONTENT);
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci    const expectedPaused = TOTAL_LINES - iterations > highWaterMark;
351cb0ef41Sopenharmony_ci    assert.strictEqual(readable.isPaused(), expectedPaused);
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci    iterations += 1;
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci    // We have to end the input stream asynchronously for back pressure to work.
401cb0ef41Sopenharmony_ci    // Only end when we have reached the final line.
411cb0ef41Sopenharmony_ci    if (iterations === TOTAL_LINES) {
421cb0ef41Sopenharmony_ci      readable.push(null);
431cb0ef41Sopenharmony_ci      readableEnded = true;
441cb0ef41Sopenharmony_ci    }
451cb0ef41Sopenharmony_ci  }
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci  assert.strictEqual(iterations, TOTAL_LINES);
481cb0ef41Sopenharmony_ci})().then(common.mustCall());
49