Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: runToLast should respect current time #208

Merged
merged 3 commits into from
Sep 18, 2018

Conversation

SimenB
Copy link
Member

@SimenB SimenB commented Sep 17, 2018

Purpose (TL;DR) - mandatory

Without the code change, the added test makes node hangs forever until it runs out of memory.

Background (Problem in detail) - optional

clock.tick expect the argument to be how many millis it should tick. Currently, we pass it the timestamp of when it should be done ticking.

https://github.com/sinonjs/lolex/blob/1278eaade7da311d203fc6c33f67f85c828a09f2/src/lolex-src.js#L723

I don't understand how no-one hit this bug for 2.5 years... It's been that way since the original implementation: #50.

Solution

Subtract the current time on the clock before calling tick. The first thing tick does is add it back, so it knows when to stop.

https://github.com/sinonjs/lolex/blob/1278eaade7da311d203fc6c33f67f85c828a09f2/src/lolex-src.js#L603

We might want a tickTo method? Implementation would be this.tick(to - clock.now), so maybe not valuable.

@SimenB
Copy link
Member Author

SimenB commented Sep 17, 2018

Also, I would have expected the timers to bail after the max number of iterations were reached, not keep scheduling new ones until we run out of memory

@@ -1222,6 +1222,22 @@ describe("lolex", function () {
assert.isTrue(spy.called);
});

it("should support clocks with start time", function () {
var startingPoint = new Date("2018-01-01").getTime();
this.clock = lolex.createClock(startingPoint);
Copy link
Member Author

@SimenB SimenB Sep 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a stupidly high number (1514764800000), but if you do e.g. 200 instead the test will fail with invocations being equal to 5 instead of hanging forever. Might be a better assertion? Feel free to change it if you want 🙂

Copy link
Contributor

@fatso83 fatso83 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, so the reason this hasn't been caught must be because it's not used all that much, I guess. I never used it at least.

@SimenB
Copy link
Member Author

SimenB commented Sep 18, 2018

Simplified the test per my comment 🙂

Would love a release of this, as I'm trying to pick up Lolex in Jest again, and this is blocking

@fatso83 fatso83 merged commit cbaa8ba into sinonjs:master Sep 18, 2018
@SimenB SimenB deleted the run-to-last-loop branch September 18, 2018 11:25
@fatso83
Copy link
Contributor

fatso83 commented Sep 19, 2018

Out as 2.7.5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants