• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3// Refs: https://github.com/nodejs/node/issues/33940
4
5const common = require('../common');
6const tmpdir = require('../common/tmpdir');
7const fs = require('fs');
8const assert = require('assert');
9const path = require('path');
10
11tmpdir.refresh();
12
13const file = path.join(tmpdir.path, '/read_stream_pos_test.txt');
14
15fs.writeFileSync(file, '');
16
17let counter = 0;
18
19const writeInterval = setInterval(() => {
20  counter = counter + 1;
21  const line = `hello at ${counter}\n`;
22  fs.writeFileSync(file, line, { flag: 'a' });
23}, 1);
24
25const hwm = 10;
26let bufs = [];
27let isLow = false;
28let cur = 0;
29let stream;
30
31const readInterval = setInterval(() => {
32  if (stream) return;
33
34  stream = fs.createReadStream(file, {
35    highWaterMark: hwm,
36    start: cur
37  });
38  stream.on('data', common.mustCallAtLeast((chunk) => {
39    cur += chunk.length;
40    bufs.push(chunk);
41    if (isLow) {
42      const brokenLines = Buffer.concat(bufs).toString()
43        .split('\n')
44        .filter((line) => {
45          const s = 'hello at'.slice(0, line.length);
46          if (line && !line.startsWith(s)) {
47            return true;
48          }
49          return false;
50        });
51      assert.strictEqual(brokenLines.length, 0);
52      exitTest();
53      return;
54    }
55    if (chunk.length !== hwm) {
56      isLow = true;
57    }
58  }));
59  stream.on('end', () => {
60    stream = null;
61    isLow = false;
62    bufs = [];
63  });
64}, 10);
65
66// Time longer than 90 seconds to exit safely
67const endTimer = setTimeout(() => {
68  exitTest();
69}, 90000);
70
71const exitTest = () => {
72  clearInterval(readInterval);
73  clearInterval(writeInterval);
74  clearTimeout(endTimer);
75  if (stream && !stream.destroyed) {
76    stream.on('close', () => {
77      process.exit();
78    });
79    stream.destroy();
80  } else {
81    process.exit();
82  }
83};
84