test: Fix test-fs-read-stream-fd-leak race cond
authorJunliang Yan <jyan@ca.ibm.com>
Tue, 6 Oct 2015 18:21:13 +0000 (14:21 -0400)
committerJames M Snell <jasnell@gmail.com>
Thu, 8 Oct 2015 21:50:25 +0000 (14:50 -0700)
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>>
test/parallel/test-fs-read-stream-fd-leak.js

index 9bbb631..bac7d5c 100644 (file)
@@ -10,6 +10,7 @@ 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() {
@@ -26,6 +27,19 @@ 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);
@@ -33,12 +47,7 @@ function testLeak(endFn, callback) {
 
     if (++i === loopCount) {
       clearTimeout(this);
-      setTimeout(function() {
-        assert.equal(0, openCount, 'no leaked file descriptors using ' +
-                     endFn + '() (got ' + openCount + ')');
-        openCount = 0;
-        callback && setTimeout(callback, 100);
-      }, 100);
+      setTimeout(checkFunction, 100);
     }
   }, 2);
 }