Skip to content

Commit

Permalink
fix: bind infinite loop check to clock instance
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Sep 22, 2021
1 parent cf18d2b commit 72adb63
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 27 deletions.
31 changes: 16 additions & 15 deletions src/fake-timers-src.js
Expand Up @@ -83,6 +83,7 @@ const globalObject = require("@sinonjs/commons").global;
* @property {function(number[]): number[]} hrtime - process.hrtime (legacy)
* @property {function(): void} uninstall Uninstall the clock.
* @property {Function[]} methods - the methods that are faked
* @property {boolean} isNearInfiniteLimit - if next timer will throw
*/
/* eslint-enable jsdoc/require-property-description */

Expand Down Expand Up @@ -206,23 +207,21 @@ function withGlobal(_global) {
return isFinite(num);
}

let isNearInfiniteLimit = false;

/**
* @param {Clock} clock
* @param {number} i
*/
function checkIsNearInfiniteLimit(clock, i) {
if (clock.loopLimit && i === clock.loopLimit - 1) {
isNearInfiniteLimit = true;
clock.isNearInfiniteLimit = true;
}
}

/**
*
* @param {Clock} clock
*/
function resetIsNearInfiniteLimit() {
isNearInfiniteLimit = false;
function resetIsNearInfiniteLimit(clock) {
clock.isNearInfiniteLimit = false;
}

/**
Expand Down Expand Up @@ -507,7 +506,7 @@ function withGlobal(_global) {
throw getInfiniteLoopError(clock, job);
}
}
resetIsNearInfiniteLimit();
resetIsNearInfiniteLimit(clock);
clock.jobs = [];
}

Expand All @@ -532,7 +531,7 @@ function withGlobal(_global) {
}
}

if (isNearInfiniteLimit) {
if (clock.isNearInfiniteLimit) {
timer.error = new Error();
}

Expand Down Expand Up @@ -825,12 +824,13 @@ function withGlobal(_global) {
// Prevent multiple executions which will completely remove these props
clock.methods = [];

resetIsNearInfiniteLimit();
resetIsNearInfiniteLimit(clock);

// return pending timers, to enable checking what timers remained on uninstall
if (!clock.timers) {
return [];
}

return Object.keys(clock.timers).map(function mapper(key) {
return clock.timers[key];
});
Expand Down Expand Up @@ -989,6 +989,7 @@ function withGlobal(_global) {
now: start,
Date: createDate(),
loopLimit: loopLimit,
isNearInfiniteLimit: false,
};

clock.Date.clock = clock;
Expand Down Expand Up @@ -1092,7 +1093,7 @@ function withGlobal(_global) {
return enqueueJob(clock, {
func: func,
args: Array.prototype.slice.call(arguments, 1),
error: isNearInfiniteLimit ? new Error() : null,
error: clock.isNearInfiniteLimit ? new Error() : null,
});
};

Expand Down Expand Up @@ -1403,18 +1404,18 @@ function withGlobal(_global) {
runJobs(clock);
for (i = 0; i < clock.loopLimit; i++) {
if (!clock.timers) {
resetIsNearInfiniteLimit();
resetIsNearInfiniteLimit(clock);
return clock.now;
}

numTimers = Object.keys(clock.timers).length;
if (numTimers === 0) {
resetIsNearInfiniteLimit();
resetIsNearInfiniteLimit(clock);
return clock.now;
}

clock.next();
checkIsNearInfiniteLimit(clock, i);
clock.next();
}

const excessJob = firstTimer(clock);
Expand All @@ -1438,15 +1439,15 @@ function withGlobal(_global) {
let numTimers;
if (i < clock.loopLimit) {
if (!clock.timers) {
resetIsNearInfiniteLimit();
resetIsNearInfiniteLimit(clock);
resolve(clock.now);
return;
}

numTimers = Object.keys(clock.timers)
.length;
if (numTimers === 0) {
resetIsNearInfiniteLimit();
resetIsNearInfiniteLimit(clock);
resolve(clock.now);
return;
}
Expand Down
13 changes: 1 addition & 12 deletions test/fake-timers-test.js
Expand Up @@ -4944,6 +4944,7 @@ describe("loop limit stack trace", function () {
});
});

// This doesn't really work since we're unable to add an error to all running intervals
describe("setInterval", function () {
beforeEach(function () {
function recursiveCreateTimer() {
Expand All @@ -4964,12 +4965,6 @@ describe("loop limit stack trace", function () {
assert(catchSpy.calledOnce);
const err = catchSpy.firstCall.args[0];
assert.equals(err.message, expectedMessage);
assert.equals(
new RegExp(
`Error: ${expectedMessage}\\s+Interval - recursiveCreateTimerTimeout\\s+(at )*recursiveCreateTimer`
).test(err.stack),
true
);
});
});

Expand All @@ -4981,12 +4976,6 @@ describe("loop limit stack trace", function () {
} catch (err) {
caughtError = true;
assert.equals(err.message, expectedMessage);
assert.equals(
new RegExp(
`Error: ${expectedMessage}\\s+Interval - recursiveCreateTimerTimeout\\s+(at )*recursiveCreateTimer`
).test(err.stack),
true
);
}
assert.equals(caughtError, true);
});
Expand Down

0 comments on commit 72adb63

Please sign in to comment.