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 tmpdir = require('../common/tmpdir'); 31 32function newBuffer(size, value) { 33 const buffer = Buffer.allocUnsafe(size); 34 while (size--) { 35 buffer[size] = value; 36 } 37 buffer[buffer.length - 1] = 0x0a; 38 return buffer; 39} 40 41const fs = require('fs'); 42const testFileName = require('path').join(tmpdir.path, 'GH-814_testFile.txt'); 43const testFileFD = fs.openSync(testFileName, 'w'); 44console.log(testFileName); 45 46 47const kBufSize = 128 * 1024; 48let PASS = true; 49const neverWrittenBuffer = newBuffer(kBufSize, 0x2e); // 0x2e === '.' 50const bufPool = []; 51 52 53const tail = require('child_process').spawn('tail', ['-f', testFileName]); 54tail.stdout.on('data', tailCB); 55 56function tailCB(data) { 57 PASS = !data.toString().includes('.'); 58} 59 60 61const timeToQuit = Date.now() + 8e3; // Test during no more than this seconds. 62(function main() { 63 64 if (PASS) { 65 fs.write(testFileFD, newBuffer(kBufSize, 0x61), 0, kBufSize, -1, cb); 66 global.gc(); 67 const nuBuf = Buffer.allocUnsafe(kBufSize); 68 neverWrittenBuffer.copy(nuBuf); 69 if (bufPool.push(nuBuf) > 100) { 70 bufPool.length = 0; 71 } 72 } else { 73 throw new Error("Buffer GC'ed test -> FAIL"); 74 } 75 76 if (Date.now() < timeToQuit) { 77 process.nextTick(main); 78 } else { 79 tail.kill(); 80 console.log("Buffer GC'ed test -> PASS (OK)"); 81 } 82 83})(); 84 85 86function cb(err, written) { 87 assert.ifError(err); 88} 89