0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/test/parallel/test-fs-read-stream-fd-leak.js
Junliang Yan 8aa589c069 test: Fix test-fs-read-stream-fd-leak race cond
Fix intermittent test failure on slower machines.
Gives test longer time to complete but checks
at regular intervals so that the test only
runs longer on slower machines or in the failure
case.

PR-URL: https://github.com/nodejs/node/pull/3218
Fixes: https://github.com/nodejs/node/issues/3215
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: James Snell <jasnell@gmail.com>>
2015-10-08 16:31:16 -04:00

58 lines
1.2 KiB
JavaScript

'use strict';
var common = require('../common');
var assert = require('assert');
var fs = require('fs');
var path = require('path');
var openCount = 0;
var _fsopen = fs.open;
var _fsclose = fs.close;
var loopCount = 50;
var totalCheck = 50;
var emptyTxt = path.join(common.fixturesDir, 'empty.txt');
fs.open = function() {
openCount++;
return _fsopen.apply(null, arguments);
};
fs.close = function() {
openCount--;
return _fsclose.apply(null, arguments);
};
function testLeak(endFn, callback) {
console.log('testing for leaks from fs.createReadStream().%s()...', endFn);
var i = 0;
var check = 0;
var checkFunction = function() {
if (openCount != 0 && check < totalCheck) {
check++;
setTimeout(checkFunction, 100);
return;
}
assert.equal(0, openCount, 'no leaked file descriptors using ' +
endFn + '() (got ' + openCount + ')');
openCount = 0;
callback && setTimeout(callback, 100);
};
setInterval(function() {
var s = fs.createReadStream(emptyTxt);
s[endFn]();
if (++i === loopCount) {
clearTimeout(this);
setTimeout(checkFunction, 100);
}
}, 2);
}
testLeak('close', function() {
testLeak('destroy');
});