node/test/parallel/test-timers-unref-active-unenrolled-disposed.js
Jeremiah Senkpiel 049b3a3ae2 timers: minor _unrefActive fixes and improvements
This commit addresses most of the review comments in
https://github.com/nodejs/node/pull/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: https://github.com/nodejs/node-convergence-archive/issues/23
Ref: https://github.com/nodejs/node/issues/268
PR-URL: https://github.com/nodejs/node/pull/2540
Reviewed-By: bnoordhuis - Ben Noordhuis <info@bnoordhuis.nl>
2015-09-02 08:14:26 -04:00

47 lines
1.2 KiB
JavaScript

'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;
}