11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst fs = require('fs');
51cb0ef41Sopenharmony_ciconst { once } = require('events');
61cb0ef41Sopenharmony_ciconst { join } = require('path');
71cb0ef41Sopenharmony_ciconst readline = require('readline');
81cb0ef41Sopenharmony_ciconst assert = require('assert');
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir');
111cb0ef41Sopenharmony_citmpdir.refresh();
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciconst filename = join(tmpdir.path, 'test.txt');
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ciconst testContents = [
161cb0ef41Sopenharmony_ci  '',
171cb0ef41Sopenharmony_ci  '\n',
181cb0ef41Sopenharmony_ci  'line 1',
191cb0ef41Sopenharmony_ci  'line 1\nline 2 南越国是前203年至前111年存在于岭南地区的一个国家\nline 3\ntrailing',
201cb0ef41Sopenharmony_ci  'line 1\nline 2\nline 3 ends with newline\n',
211cb0ef41Sopenharmony_ci];
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ciasync function testSimpleDestroy() {
241cb0ef41Sopenharmony_ci  for (const fileContent of testContents) {
251cb0ef41Sopenharmony_ci    fs.writeFileSync(filename, fileContent);
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci    const readable = fs.createReadStream(filename);
281cb0ef41Sopenharmony_ci    const rli = readline.createInterface({
291cb0ef41Sopenharmony_ci      input: readable,
301cb0ef41Sopenharmony_ci      crlfDelay: Infinity
311cb0ef41Sopenharmony_ci    });
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci    const iteratedLines = [];
341cb0ef41Sopenharmony_ci    for await (const k of rli) {
351cb0ef41Sopenharmony_ci      iteratedLines.push(k);
361cb0ef41Sopenharmony_ci      break;
371cb0ef41Sopenharmony_ci    }
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci    const expectedLines = fileContent.split('\n');
401cb0ef41Sopenharmony_ci    if (expectedLines[expectedLines.length - 1] === '') {
411cb0ef41Sopenharmony_ci      expectedLines.pop();
421cb0ef41Sopenharmony_ci    }
431cb0ef41Sopenharmony_ci    expectedLines.splice(1);
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci    assert.deepStrictEqual(iteratedLines, expectedLines);
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci    rli.close();
481cb0ef41Sopenharmony_ci    readable.destroy();
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci    await once(readable, 'close');
511cb0ef41Sopenharmony_ci  }
521cb0ef41Sopenharmony_ci}
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ciasync function testMutualDestroy() {
551cb0ef41Sopenharmony_ci  for (const fileContent of testContents) {
561cb0ef41Sopenharmony_ci    fs.writeFileSync(filename, fileContent);
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci    const readable = fs.createReadStream(filename);
591cb0ef41Sopenharmony_ci    const rli = readline.createInterface({
601cb0ef41Sopenharmony_ci      input: readable,
611cb0ef41Sopenharmony_ci      crlfDelay: Infinity
621cb0ef41Sopenharmony_ci    });
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci    const expectedLines = fileContent.split('\n');
651cb0ef41Sopenharmony_ci    if (expectedLines[expectedLines.length - 1] === '') {
661cb0ef41Sopenharmony_ci      expectedLines.pop();
671cb0ef41Sopenharmony_ci    }
681cb0ef41Sopenharmony_ci    expectedLines.splice(2);
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ci    const iteratedLines = [];
711cb0ef41Sopenharmony_ci    for await (const k of rli) {
721cb0ef41Sopenharmony_ci      iteratedLines.push(k);
731cb0ef41Sopenharmony_ci      for await (const l of rli) {
741cb0ef41Sopenharmony_ci        iteratedLines.push(l);
751cb0ef41Sopenharmony_ci        break;
761cb0ef41Sopenharmony_ci      }
771cb0ef41Sopenharmony_ci      assert.deepStrictEqual(iteratedLines, expectedLines);
781cb0ef41Sopenharmony_ci      break;
791cb0ef41Sopenharmony_ci    }
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_ci    assert.deepStrictEqual(iteratedLines, expectedLines);
821cb0ef41Sopenharmony_ci
831cb0ef41Sopenharmony_ci    rli.close();
841cb0ef41Sopenharmony_ci    readable.destroy();
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_ci    await once(readable, 'close');
871cb0ef41Sopenharmony_ci  }
881cb0ef41Sopenharmony_ci}
891cb0ef41Sopenharmony_ci
901cb0ef41Sopenharmony_citestSimpleDestroy().then(testMutualDestroy).then(common.mustCall());
91