node/test/parallel/test-timers-unrefd-interval-still-fires.js
Michael Cornacchia 788541b40c test: fix race condition in unrefd interval test
Rely more on timers implementation rather than arbitrary timeouts.

Refs: https://github.com/nodejs/node/issues/1781
PR-URL: https://github.com/nodejs/node/pull/3550

Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
2015-12-05 15:47:46 +11:00

28 lines
729 B
JavaScript

'use strict';
/*
* This test is a regression test for joyent/node#8900.
*/
const common = require('../common');
const assert = require('assert');
const TEST_DURATION = common.platformTimeout(100);
const N = 5;
var nbIntervalFired = 0;
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) {
clearInterval(timer);
timer._onTimeout = () => {
throw new Error('Unrefd interval fired after being cleared.');
};
setImmediate(() => clearTimeout(keepOpen));
}
}, 1);
timer.unref();