Skip to content

Commit

Permalink
Merge pull request #124 from elad-nach/master
Browse files Browse the repository at this point in the history
issue #67 - overflowing the timer correctly according to int32_t
  • Loading branch information
fatso83 committed Jul 25, 2017
2 parents fd595c6 + 8da6a91 commit 946ed51
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/lolex-src.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var userAgent = global.navigator && global.navigator.userAgent;
var isRunningInIE = userAgent && userAgent.indexOf("MSIE ") > -1;
var maxTimeout = Math.pow(2, 31) - 1; //see https://heycam.github.io/webidl/#abstract-opdef-converttoint

// Make properties writable in IE, as per
// http://www.adequatelygood.com/Replacing-setTimeout-Globally.html
Expand Down Expand Up @@ -190,6 +191,14 @@ function addTimer(clock, timer) {
throw new Error("Callback must be provided to timer calls");
}

if (timer.hasOwnProperty("delay")) {
timer.delay = timer.delay > maxTimeout ? 1 : timer.delay;
}

if (timer.hasOwnProperty("interval")) {
timer.interval = timer.interval > maxTimeout ? 1 : timer.interval;
}

if (!clock.timers) {
clock.timers = {};
}
Expand Down
44 changes: 44 additions & 0 deletions test/lolex-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,50 @@ describe("issue #73", function () {
});
});

describe("issue #67", function () {
// see https://nodejs.org/api/timers.html
it("should overflow to 1 on very big timeouts", function () {
var clock = lolex.install();
var stub1 = sinon.stub();
var stub2 = sinon.stub();

clock.setTimeout(stub1, 100);
clock.setTimeout(stub2, 214748334700); //should be called after 1 tick

clock.tick(1);
assert(stub2.called);
assert.isFalse(stub1.called);

clock.tick(99);
assert(stub1.called);
assert(stub2.called);

clock.uninstall();
});

it("should overflow to interval 1 on very big timeouts", function () {
var clock = lolex.install();
var stub = sinon.stub();

clock.setInterval(stub, 214748334700);
clock.tick(3);
assert(stub.calledThrice);

clock.uninstall();
});

it("should execute setTimeout smaller than 1", function () {
var clock = lolex.install();
var stub1 = sinon.stub();

clock.setTimeout(stub1, 0.5);
clock.tick(1);
assert(stub1.calledOnce);

clock.uninstall();
});
});

describe("lolex", function () {

describe("setTimeout", function () {
Expand Down

0 comments on commit 946ed51

Please sign in to comment.