Skip to content

Commit

Permalink
test: fix race condition in unrefd interval test
Browse files Browse the repository at this point in the history
Rely more on timers implementation rather than arbitrary timeouts.

Refs: #1781
PR-URL: #3550

Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
  • Loading branch information
mcornac authored and rvagg committed Dec 5, 2015
1 parent e129d83 commit 788541b
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions test/parallel/test-timers-unrefd-interval-still-fires.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,27 @@
/*
* This test is a regression test for joyent/node#8900.
*/
require('../common');
var assert = require('assert');
const common = require('../common');
const assert = require('assert');

var N = 5;
const TEST_DURATION = common.platformTimeout(100);
const N = 5;
var nbIntervalFired = 0;
var timer = setInterval(function() {

const keepOpen = setTimeout(() => {
console.error('[FAIL] Interval fired %d/%d times.', nbIntervalFired, N);
throw new Error('Test timed out. keepOpen was not canceled.');
}, TEST_DURATION);

const timer = setInterval(() => {
++nbIntervalFired;
if (nbIntervalFired === N)
if (nbIntervalFired === N) {
clearInterval(timer);
timer._onTimeout = () => {
throw new Error('Unrefd interval fired after being cleared.');
};
setImmediate(() => clearTimeout(keepOpen));
}
}, 1);

timer.unref();

setTimeout(function onTimeout() {
assert.strictEqual(nbIntervalFired, N);
}, 100);

0 comments on commit 788541b

Please sign in to comment.