Skip to content

Commit 501165f

Browse files
Trottevanlucas
authored andcommitted
test: fix timers-same-timeout-wrong-list-deleted
test-timers-same-timeout-wrong-list-deleted was flaky under load because there is no guarantee that a timer will fire within a given period of time. It had an exit handler that checked that the process was finishing in less than twice as much as a timer was set for. Under load, the timer could take over 200ms to fire even if it was set for 100ms, so this was causing the test to be flaky on CI from time to time. However, that timing check is unnecessary to identify the regression that the test was written for. When run with a version of Node.js that does not contain the fix that accompanied the test in its initial commit, an assertion indicating that there were still timers in the active timer list fired. So, this commit removes the exit handler timing check and relies on the existing robust active timers list length check. This allows us to move the test back to parallel because it does not seem to fail under load anymore. The test was refactored slightly, removing duplicated code to a function, using `assert.strictEqual()` instead of `assert.equal()`, changing a 10ms timer to 1ms, and improving the messages provided by assertions. Fixes: #8459 PR-URL: #10362 Reviewed-By: Jeremiah Senkpiel <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent ba63363 commit 501165f

File tree

1 file changed

+16
-24
lines changed

1 file changed

+16
-24
lines changed

test/sequential/test-timers-same-timeout-wrong-list-deleted.js renamed to test/parallel/test-timers-same-timeout-wrong-list-deleted.js

+16-24
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,6 @@ const assert = require('assert');
1616
const Timer = process.binding('timer_wrap').Timer;
1717

1818
const TIMEOUT = common.platformTimeout(100);
19-
const start = Timer.now();
20-
21-
// This bug also prevents the erroneously dereferenced timer's callback
22-
// from being called, so we can't use it's execution or lack thereof
23-
// to assert that the bug is fixed.
24-
process.on('exit', function() {
25-
const end = Timer.now();
26-
assert.equal(end - start < TIMEOUT * 2, true,
27-
'Elapsed time does not include second timer\'s timeout.');
28-
});
2919

3020
const handle1 = setTimeout(common.mustCall(function() {
3121
// Cause the old TIMEOUT list to be deleted
@@ -42,39 +32,41 @@ const handle1 = setTimeout(common.mustCall(function() {
4232
// erroneously deleted. If we are able to cancel the timer successfully,
4333
// the bug is fixed.
4434
clearTimeout(handle2);
35+
4536
setImmediate(common.mustCall(function() {
4637
setImmediate(common.mustCall(function() {
47-
const activeHandles = process._getActiveHandles();
48-
const activeTimers = activeHandles.filter(function(handle) {
49-
return handle instanceof Timer;
50-
});
38+
const activeTimers = getActiveTimers();
5139

5240
// Make sure our clearTimeout succeeded. One timer finished and
5341
// the other was canceled, so none should be active.
54-
assert.equal(activeTimers.length, 0, 'No Timers remain.');
42+
assert.strictEqual(activeTimers.length, 0, 'Timers remain.');
5543
}));
5644
}));
57-
}), 10);
45+
}), 1);
5846

5947
// Make sure our timers got added to the list.
60-
const activeHandles = process._getActiveHandles();
61-
const activeTimers = activeHandles.filter(function(handle) {
62-
return handle instanceof Timer;
63-
});
48+
const activeTimers = getActiveTimers();
6449
const shortTimer = activeTimers.find(function(handle) {
65-
return handle._list.msecs === 10;
50+
return handle._list.msecs === 1;
6651
});
6752
const longTimers = activeTimers.filter(function(handle) {
6853
return handle._list.msecs === TIMEOUT;
6954
});
7055

7156
// Make sure our clearTimeout succeeded. One timer finished and
7257
// the other was canceled, so none should be active.
73-
assert.equal(activeTimers.length, 3, 'There are 3 timers in the list.');
74-
assert(shortTimer instanceof Timer, 'The shorter timer is in the list.');
75-
assert.equal(longTimers.length, 2, 'Both longer timers are in the list.');
58+
assert.strictEqual(activeTimers.length, 3,
59+
'There should be 3 timers in the list.');
60+
assert(shortTimer instanceof Timer, 'The shorter timer is not in the list.');
61+
assert.strictEqual(longTimers.length, 2,
62+
'Both longer timers should be in the list.');
7663

7764
// When this callback completes, `listOnTimeout` should now look at the
7865
// correct list and refrain from removing the new TIMEOUT list which
7966
// contains the reference to the newer timer.
8067
}), TIMEOUT);
68+
69+
function getActiveTimers() {
70+
const activeHandles = process._getActiveHandles();
71+
return activeHandles.filter((handle) => handle instanceof Timer);
72+
}

0 commit comments

Comments
 (0)