-
Notifications
You must be signed in to change notification settings - Fork 30.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
timers: minor _unrefActive fixes and improvements
This commit addresses most of the review comments in #2540, which are kept in this separate commit so as to better preserve the prior two patches as they landed in 0.12. This commit: - Fixes a bug with unrefActive timers and disposed domains. - Fixes a bug with unrolling an unrefActive timer from another. - Adds a test for both above bugs. - Improves check logic, making it stricter, simpler, or both. - Optimizes nicer with a smaller, separate function for the try/catch. Fixes: nodejs/node-convergence-archive#23 Ref: #268 PR-URL: #2540 Reviewed-By: bnoordhuis - Ben Noordhuis <info@bnoordhuis.nl>
- Loading branch information
1 parent
9724047
commit 049b3a3
Showing
2 changed files
with
68 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
test/parallel/test-timers-unref-active-unenrolled-disposed.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
'use strict'; | ||
|
||
// https://github.com/nodejs/node/pull/2540/files#r38231197 | ||
|
||
const common = require('../common'); | ||
const timers = require('timers'); | ||
const assert = require('assert'); | ||
const domain = require('domain'); | ||
|
||
// Crazy stuff to keep the process open, | ||
// then close it when we are actually done. | ||
const TEST_DURATION = common.platformTimeout(100); | ||
const keepOpen = setTimeout(function() { | ||
throw new Error('Test timed out. keepOpen was not canceled.'); | ||
}, TEST_DURATION); | ||
|
||
const endTest = makeTimer(2); | ||
|
||
const someTimer = makeTimer(1); | ||
someTimer.domain = domain.create(); | ||
someTimer.domain.dispose(); | ||
someTimer._onTimeout = function() { | ||
throw new Error('someTimer was not supposed to fire!'); | ||
}; | ||
|
||
endTest._onTimeout = common.mustCall(function() { | ||
assert.strictEqual(someTimer._idlePrev, null); | ||
assert.strictEqual(someTimer._idleNext, null); | ||
clearTimeout(keepOpen); | ||
}); | ||
|
||
const cancelsTimer = makeTimer(1); | ||
cancelsTimer._onTimeout = common.mustCall(function() { | ||
someTimer._idleTimeout = 0; | ||
}); | ||
|
||
timers._unrefActive(cancelsTimer); | ||
timers._unrefActive(someTimer); | ||
timers._unrefActive(endTest); | ||
|
||
function makeTimer(msecs) { | ||
const timer = {}; | ||
timers.unenroll(timer); | ||
timers.enroll(timer, msecs); | ||
return timer; | ||
} |