-
Notifications
You must be signed in to change notification settings - Fork 104
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
Conversation
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 |
test/lolex-test.js
Outdated
@@ -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); |
There was a problem hiding this comment.
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 🙂
There was a problem hiding this 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.
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 |
Out as 2.7.5 |
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 bethis.tick(to - clock.now)
, so maybe not valuable.