From 6de82c69a00a1515dbf4019c4f27bb1f82c508e0 Mon Sep 17 00:00:00 2001 From: Michael Cornacchia Date: Tue, 27 Oct 2015 12:55:09 -0400 Subject: [PATCH] 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 --- ...test-timers-unrefd-interval-still-fires.js | 26 ++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/test/parallel/test-timers-unrefd-interval-still-fires.js b/test/parallel/test-timers-unrefd-interval-still-fires.js index 98bd278b451613..a716c6ed645cbd 100644 --- a/test/parallel/test-timers-unrefd-interval-still-fires.js +++ b/test/parallel/test-timers-unrefd-interval-still-fires.js @@ -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);