2018-10-26 17:53:04 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const common = require('../common');
|
|
|
|
const fs = require('fs');
|
2019-12-07 14:20:02 +01:00
|
|
|
const { once } = require('events');
|
2018-10-26 17:53:04 -07:00
|
|
|
const readline = require('readline');
|
|
|
|
const assert = require('assert');
|
|
|
|
|
|
|
|
const tmpdir = require('../common/tmpdir');
|
|
|
|
tmpdir.refresh();
|
|
|
|
|
2023-08-15 22:45:34 +09:00
|
|
|
const filename = tmpdir.resolve('test.txt');
|
2018-10-26 17:53:04 -07:00
|
|
|
|
|
|
|
const testContents = [
|
|
|
|
'',
|
|
|
|
'\n',
|
|
|
|
'line 1',
|
|
|
|
'line 1\nline 2 南越国是前203年至前111年存在于岭南地区的一个国家\nline 3\ntrailing',
|
2021-03-26 08:51:08 -07:00
|
|
|
'line 1\nline 2\nline 3 ends with newline\n',
|
2018-10-26 17:53:04 -07:00
|
|
|
];
|
|
|
|
|
|
|
|
async function testSimpleDestroy() {
|
|
|
|
for (const fileContent of testContents) {
|
|
|
|
fs.writeFileSync(filename, fileContent);
|
|
|
|
|
|
|
|
const readable = fs.createReadStream(filename);
|
|
|
|
const rli = readline.createInterface({
|
|
|
|
input: readable,
|
|
|
|
crlfDelay: Infinity
|
|
|
|
});
|
|
|
|
|
|
|
|
const iteratedLines = [];
|
|
|
|
for await (const k of rli) {
|
|
|
|
iteratedLines.push(k);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
const expectedLines = fileContent.split('\n');
|
|
|
|
if (expectedLines[expectedLines.length - 1] === '') {
|
|
|
|
expectedLines.pop();
|
|
|
|
}
|
|
|
|
expectedLines.splice(1);
|
|
|
|
|
|
|
|
assert.deepStrictEqual(iteratedLines, expectedLines);
|
2019-07-30 07:50:21 +01:00
|
|
|
|
|
|
|
rli.close();
|
|
|
|
readable.destroy();
|
2019-12-07 14:20:02 +01:00
|
|
|
|
|
|
|
await once(readable, 'close');
|
2018-10-26 17:53:04 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function testMutualDestroy() {
|
|
|
|
for (const fileContent of testContents) {
|
|
|
|
fs.writeFileSync(filename, fileContent);
|
|
|
|
|
|
|
|
const readable = fs.createReadStream(filename);
|
|
|
|
const rli = readline.createInterface({
|
|
|
|
input: readable,
|
|
|
|
crlfDelay: Infinity
|
|
|
|
});
|
|
|
|
|
|
|
|
const expectedLines = fileContent.split('\n');
|
|
|
|
if (expectedLines[expectedLines.length - 1] === '') {
|
|
|
|
expectedLines.pop();
|
|
|
|
}
|
|
|
|
expectedLines.splice(2);
|
|
|
|
|
|
|
|
const iteratedLines = [];
|
|
|
|
for await (const k of rli) {
|
|
|
|
iteratedLines.push(k);
|
|
|
|
for await (const l of rli) {
|
|
|
|
iteratedLines.push(l);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
assert.deepStrictEqual(iteratedLines, expectedLines);
|
2020-06-23 23:08:14 +02:00
|
|
|
break;
|
2018-10-26 17:53:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
assert.deepStrictEqual(iteratedLines, expectedLines);
|
2019-07-30 07:50:21 +01:00
|
|
|
|
|
|
|
rli.close();
|
|
|
|
readable.destroy();
|
2019-12-07 14:20:02 +01:00
|
|
|
|
|
|
|
await once(readable, 'close');
|
2018-10-26 17:53:04 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
testSimpleDestroy().then(testMutualDestroy).then(common.mustCall());
|