Skip to content

Commit 788541b

Browse files
mcornacrvagg
authored andcommitted
test: fix race condition in unrefd interval test
Rely more on timers implementation rather than arbitrary timeouts. Refs: #1781 PR-URL: #3550 Reviewed-By: Jeremiah Senkpiel <[email protected]>
1 parent e129d83 commit 788541b

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

test/parallel/test-timers-unrefd-interval-still-fires.js

+17-9
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,27 @@
22
/*
33
* This test is a regression test for joyent/node#8900.
44
*/
5-
require('../common');
6-
var assert = require('assert');
5+
const common = require('../common');
6+
const assert = require('assert');
77

8-
var N = 5;
8+
const TEST_DURATION = common.platformTimeout(100);
9+
const N = 5;
910
var nbIntervalFired = 0;
10-
var timer = setInterval(function() {
11+
12+
const keepOpen = setTimeout(() => {
13+
console.error('[FAIL] Interval fired %d/%d times.', nbIntervalFired, N);
14+
throw new Error('Test timed out. keepOpen was not canceled.');
15+
}, TEST_DURATION);
16+
17+
const timer = setInterval(() => {
1118
++nbIntervalFired;
12-
if (nbIntervalFired === N)
19+
if (nbIntervalFired === N) {
1320
clearInterval(timer);
21+
timer._onTimeout = () => {
22+
throw new Error('Unrefd interval fired after being cleared.');
23+
};
24+
setImmediate(() => clearTimeout(keepOpen));
25+
}
1426
}, 1);
1527

1628
timer.unref();
17-
18-
setTimeout(function onTimeout() {
19-
assert.strictEqual(nbIntervalFired, N);
20-
}, 100);

0 commit comments

Comments
 (0)