• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright Joyent, Inc. and other Node contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the
5// "Software"), to deal in the Software without restriction, including
6// without limitation the rights to use, copy, modify, merge, publish,
7// distribute, sublicense, and/or sell copies of the Software, and to permit
8// persons to whom the Software is furnished to do so, subject to the
9// following conditions:
10//
11// The above copyright notice and this permission notice shall be included
12// in all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22// Refs: https://github.com/nodejs/node-v0.x-archive/issues/814
23
24'use strict';
25// Flags: --expose_gc
26
27require('../common');
28const assert = require('assert');
29
30const fs = require('fs');
31const tmpdir = require('../common/tmpdir');
32const testFileName = require('path').join(tmpdir.path, 'GH-814_test.txt');
33const testFD = fs.openSync(testFileName, 'w');
34console.error(`${testFileName}\n`);
35
36
37const tailProc = require('child_process').spawn('tail', ['-f', testFileName]);
38tailProc.stdout.on('data', tailCB);
39
40function tailCB(data) {
41  PASS = !data.toString().includes('.');
42
43  if (!PASS) {
44    console.error('[FAIL]\n DATA -> ');
45    console.error(data);
46    console.error('\n');
47    throw new Error('Buffers GC test -> FAIL');
48  }
49}
50
51
52let PASS = true;
53const bufPool = [];
54const kBufSize = 16 * 1024 * 1024;
55const neverWrittenBuffer = newBuffer(kBufSize, 0x2e); // 0x2e === '.'
56
57const timeToQuit = Date.now() + 5e3; // Test should last no more than this.
58writer();
59
60function writer() {
61
62  if (PASS) {
63    if (Date.now() > timeToQuit) {
64      setTimeout(function() {
65        process.kill(tailProc.pid);
66        console.error('\nBuffers GC test -> PASS (OK)\n');
67      }, 555);
68    } else {
69      fs.write(testFD, newBuffer(kBufSize, 0x61), 0, kBufSize, -1, writerCB);
70      global.gc();
71      global.gc();
72      global.gc();
73      global.gc();
74      global.gc();
75      global.gc();
76      const nuBuf = Buffer.allocUnsafe(kBufSize);
77      neverWrittenBuffer.copy(nuBuf);
78      if (bufPool.push(nuBuf) > 100) {
79        bufPool.length = 0;
80      }
81      process.nextTick(writer);
82    }
83  }
84
85}
86
87function writerCB(err, written) {
88  assert.ifError(err);
89}
90
91
92// ******************* UTILITIES
93
94
95function newBuffer(size, value) {
96  const buffer = Buffer.allocUnsafe(size);
97  while (size--) {
98    buffer[size] = value;
99  }
100  buffer[buffer.length - 1] = 0x0d;
101  buffer[buffer.length - 1] = 0x0a;
102  return buffer;
103}
104