11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ci// Refs: https://github.com/nodejs/node/issues/33940 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ciconst common = require('../common'); 61cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir'); 71cb0ef41Sopenharmony_ciconst fs = require('fs'); 81cb0ef41Sopenharmony_ciconst assert = require('assert'); 91cb0ef41Sopenharmony_ciconst path = require('path'); 101cb0ef41Sopenharmony_ci 111cb0ef41Sopenharmony_citmpdir.refresh(); 121cb0ef41Sopenharmony_ci 131cb0ef41Sopenharmony_ciconst file = path.join(tmpdir.path, '/read_stream_pos_test.txt'); 141cb0ef41Sopenharmony_ci 151cb0ef41Sopenharmony_cifs.writeFileSync(file, ''); 161cb0ef41Sopenharmony_ci 171cb0ef41Sopenharmony_cilet counter = 0; 181cb0ef41Sopenharmony_ci 191cb0ef41Sopenharmony_ciconst writeInterval = setInterval(() => { 201cb0ef41Sopenharmony_ci counter = counter + 1; 211cb0ef41Sopenharmony_ci const line = `hello at ${counter}\n`; 221cb0ef41Sopenharmony_ci fs.writeFileSync(file, line, { flag: 'a' }); 231cb0ef41Sopenharmony_ci}, 1); 241cb0ef41Sopenharmony_ci 251cb0ef41Sopenharmony_ciconst hwm = 10; 261cb0ef41Sopenharmony_cilet bufs = []; 271cb0ef41Sopenharmony_cilet isLow = false; 281cb0ef41Sopenharmony_cilet cur = 0; 291cb0ef41Sopenharmony_cilet stream; 301cb0ef41Sopenharmony_ci 311cb0ef41Sopenharmony_ciconst readInterval = setInterval(() => { 321cb0ef41Sopenharmony_ci if (stream) return; 331cb0ef41Sopenharmony_ci 341cb0ef41Sopenharmony_ci stream = fs.createReadStream(file, { 351cb0ef41Sopenharmony_ci highWaterMark: hwm, 361cb0ef41Sopenharmony_ci start: cur 371cb0ef41Sopenharmony_ci }); 381cb0ef41Sopenharmony_ci stream.on('data', common.mustCallAtLeast((chunk) => { 391cb0ef41Sopenharmony_ci cur += chunk.length; 401cb0ef41Sopenharmony_ci bufs.push(chunk); 411cb0ef41Sopenharmony_ci if (isLow) { 421cb0ef41Sopenharmony_ci const brokenLines = Buffer.concat(bufs).toString() 431cb0ef41Sopenharmony_ci .split('\n') 441cb0ef41Sopenharmony_ci .filter((line) => { 451cb0ef41Sopenharmony_ci const s = 'hello at'.slice(0, line.length); 461cb0ef41Sopenharmony_ci if (line && !line.startsWith(s)) { 471cb0ef41Sopenharmony_ci return true; 481cb0ef41Sopenharmony_ci } 491cb0ef41Sopenharmony_ci return false; 501cb0ef41Sopenharmony_ci }); 511cb0ef41Sopenharmony_ci assert.strictEqual(brokenLines.length, 0); 521cb0ef41Sopenharmony_ci exitTest(); 531cb0ef41Sopenharmony_ci return; 541cb0ef41Sopenharmony_ci } 551cb0ef41Sopenharmony_ci if (chunk.length !== hwm) { 561cb0ef41Sopenharmony_ci isLow = true; 571cb0ef41Sopenharmony_ci } 581cb0ef41Sopenharmony_ci })); 591cb0ef41Sopenharmony_ci stream.on('end', () => { 601cb0ef41Sopenharmony_ci stream = null; 611cb0ef41Sopenharmony_ci isLow = false; 621cb0ef41Sopenharmony_ci bufs = []; 631cb0ef41Sopenharmony_ci }); 641cb0ef41Sopenharmony_ci}, 10); 651cb0ef41Sopenharmony_ci 661cb0ef41Sopenharmony_ci// Time longer than 90 seconds to exit safely 671cb0ef41Sopenharmony_ciconst endTimer = setTimeout(() => { 681cb0ef41Sopenharmony_ci exitTest(); 691cb0ef41Sopenharmony_ci}, 90000); 701cb0ef41Sopenharmony_ci 711cb0ef41Sopenharmony_ciconst exitTest = () => { 721cb0ef41Sopenharmony_ci clearInterval(readInterval); 731cb0ef41Sopenharmony_ci clearInterval(writeInterval); 741cb0ef41Sopenharmony_ci clearTimeout(endTimer); 751cb0ef41Sopenharmony_ci if (stream && !stream.destroyed) { 761cb0ef41Sopenharmony_ci stream.on('close', () => { 771cb0ef41Sopenharmony_ci process.exit(); 781cb0ef41Sopenharmony_ci }); 791cb0ef41Sopenharmony_ci stream.destroy(); 801cb0ef41Sopenharmony_ci } else { 811cb0ef41Sopenharmony_ci process.exit(); 821cb0ef41Sopenharmony_ci } 831cb0ef41Sopenharmony_ci}; 84