Browse Source

Fixed: clearTimeouts calling multiple times

When clearTimeouts was called on a timer multiple times, it would break the
doubly-linked list along with future timeouts. This patch fixes that.
v0.7.4-release
Michael W 14 years ago
committed by Ryan Dahl
parent
commit
645c3b3713
  1. 4
      lib/timers.js
  2. 18
      test/pummel/test-timers.js

4
lib/timers.js

@ -43,6 +43,7 @@ function shift (list) {
function remove (item) { function remove (item) {
item._idleNext._idlePrev = item._idlePrev; item._idleNext._idlePrev = item._idlePrev;
item._idlePrev._idleNext = item._idleNext; item._idlePrev._idleNext = item._idleNext;
item._idleNext = null;
} }
@ -111,8 +112,7 @@ function insert (item, msecs) {
var unenroll = exports.unenroll = function (item) { var unenroll = exports.unenroll = function (item) {
if (item._idleNext) { if (item._idleNext) {
item._idleNext._idlePrev = item._idlePrev; remove(item);
item._idlePrev._idleNext = item._idleNext;
var list = lists[item._idleTimeout]; var list = lists[item._idleTimeout];
// if empty then stop the watcher // if empty then stop the watcher

18
test/pummel/test-timers.js

@ -84,8 +84,26 @@ interval4 = setInterval(function () {
if (++count4 > 10) clearInterval(interval4); if (++count4 > 10) clearInterval(interval4);
}, 0); }, 0);
// we should be able to clearTimeout multiple times without breakage.
var expectedTimeouts = 3;
function t () {
expectedTimeouts--;
}
w = setTimeout(t, 200),
x = setTimeout(t, 200),
y = setTimeout(t, 200);
clearTimeout(y),
z = setTimeout(t, 200);
clearTimeout(y);
process.addListener("exit", function () { process.addListener("exit", function () {
assert.equal(true, setTimeout_called); assert.equal(true, setTimeout_called);
assert.equal(3, interval_count); assert.equal(3, interval_count);
assert.equal(11, count4); assert.equal(11, count4);
assert.equal(0, expectedTimeouts, "clearTimeout cleared too many timeouts");
}); });

Loading…
Cancel
Save