1'use strict'; 2 3require('../common'); 4const assert = require('assert'); 5const fs = require('fs'); 6const fixtures = require('../common/fixtures'); 7 8let openCount = 0; 9const _fsopen = fs.open; 10const _fsclose = fs.close; 11 12const loopCount = 50; 13const totalCheck = 50; 14const emptyTxt = fixtures.path('empty.txt'); 15 16fs.open = function() { 17 openCount++; 18 return _fsopen.apply(null, arguments); 19}; 20 21fs.close = function() { 22 openCount--; 23 return _fsclose.apply(null, arguments); 24}; 25 26function testLeak(endFn, callback) { 27 console.log(`testing for leaks from fs.createReadStream().${endFn}()...`); 28 29 let i = 0; 30 let check = 0; 31 32 function checkFunction() { 33 if (openCount !== 0 && check < totalCheck) { 34 check++; 35 setTimeout(checkFunction, 100); 36 return; 37 } 38 39 assert.strictEqual( 40 openCount, 41 0, 42 `no leaked file descriptors using ${endFn}() (got ${openCount})` 43 ); 44 45 openCount = 0; 46 callback && setTimeout(callback, 100); 47 } 48 49 setInterval(function() { 50 const s = fs.createReadStream(emptyTxt); 51 s[endFn](); 52 53 if (++i === loopCount) { 54 clearTimeout(this); 55 setTimeout(checkFunction, 100); 56 } 57 }, 2); 58} 59 60testLeak('close', function() { 61 testLeak('destroy'); 62}); 63