-
Notifications
You must be signed in to change notification settings - Fork 107
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
Upgrade lolex with async versions of all timer-executing calls (2019) #237
Conversation
I've just tried this out, dropping this branch in instead of some local code that implemented async timers, and I hit some snags along the way - but, once I overcame them, I can report it worked fine. I do have one problem to report. Specifically, sequential calls to If I don't flush the timer queue at the end of every test, my tests stall and time out. OK, so I need to write my tests like this: const itAsync = (name, fn) => {
it(name, async () => {
fn();
await clock.runAllAsync();
// D
});
};
itAsync("flarbs the blah", async () => {
// A
await clock.runAllAsync();
// B
await clock.runAllAsync();
// C
}); Unfortunately, concurrent If, however, const itAsync = (name, fn) => {
it(name, async () => {
let running = true;
Promise.resolve(fn()).finally(() => {
running = false;
});
while (running) {
await clock.runAllAsync();
}
});
}; Then it works, and the code at C gets run before the end of the test. But this is clearly demonstrating that |
Actually, upon reflection, concurrent |
I don't think this will work with async/await. I think the way to make this work is to use the devtools protocol. I am not opposed to landing a stop-gap though. |
I will also ping my employer and ask for a full day of work on lolex to close all the stuff I need. Sorry! I was hoping to do this in May but then all the conferences happened. |
0650e29
to
06f544b
Compare
Rebased this on top of Will try to add some docs to this. |
@quasicomputational in your example, if you add an |
I didn't perform as thorough a review as I would at my day job, but @kylefleming's #105 looks very well thought out, so I hope we can merge this :) I know there's comments about different approaches on how this could have been implemented differently, but I don't think the required functionality is present in node nor in the browsers, and this hasn't changed in the last 2 years, so quite possibly this is as good as it will ever get... |
@dominykas Can you add some docs that explain the limits of the I'm inline with you on this with regards to having a stopgap solution that works in 80% of the cases, while waiting for The Perfect Fix ™️. As an alternative to trying to fix bigger examples into the API docs, I could add a Lolex how-to for doing this on the SinonJS how-to page, but I'd still need a somewhat useful template. To not stop this from expanding, I could split that into a separate issue. |
FWIW I think that this PR is as good as can be done, and that there's not even a theoretical 'better fix' that merging should block on. The bug is in user expectations, not library code. |
Wanted to chime in with a 👍 on this. I'm using a quick wrapper (built by my colleague @digli) for this functionality in some current projects and it has shown a very real value. The wrapper: const tick = async (clock, ms) => { clock.tick(ms); };
// ...
await tick(clock, 300); |
@fatso83 sure, I'll see what I can do. |
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.
I think this is good to go. Sorry for keeping you and @kylefleming out dry by leaving this hanging for so long. As you stated, it would be nice to have some additional docs explaining the limits of runAllAsync
, but it can be added later on. Preferably in lolex
, with either a duplicate comment in the Sinon API docs or with a link to lolex for more insight (like you did with lolex.install
).
I'll merge this today, unless there's more input.
@fatso83 go ahead and merge this |
I ran |
1435b14
to
5c52684
Compare
🎉 ✅ |
I think this needs a rebase |
Is that to keep the commit history clean? I can do that tomorrow, I suppose. |
It needs a little bit more work, @dominykas, as I just ran the
Are you able to modify and run the test in Firefox yourself? We can provide you with sauce labs credentials (privately in a dm; my email is public). BTW, I rebased and pushed to a branch of my own: https://github.com/fatso83/lolex/tree/async-upgrade-2019. You can just checkout that branch and reset your own branch pointer to that, to save you some work (added a small fix). |
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.
failed in Firefox
Cheers, I'll take a look at the Firefox issue. |
5c52684
to
fca902f
Compare
Added tickAsync(), nextAsync(), runAllAsync(), runToLastAsync()
fca902f
to
6bea37c
Compare
To make reviewing easier: Force push 1: reset my head to @fatso83's head - diff provided by GH Force push 2: rebased that on top of master (5.0.1) - I have a branch with the same code as before, but master merged in - if you were to diff that against i.e. no change in the final result since last reviews. Now, onto Firefox... |
Promises are slow, they result in many browser ticks, making the tests timeout on Firefox.
I was not able to start that up locally... Seems mochify needs some love? Or maybe webdriver/selenium... Sigh. I used Saucelabs credentials from one of my other OSS projects. Please don't tell anyone ;) Anyways, it seems the issue was due to the fact that promises (new ticks?) seem to be some 3x slower than Chrome - decreased the number of loops and got this to pass on Saucelabs - @fatso83 can you give it a spin? |
Had another look. TBH, I'm not entirely sure I understand the problem - but I also posted a suggestion of where I think @quasicomputational's code itself might be problematic (or maybe I misunderstood the intent there). The other linked comment is this:
I'm not entirely sure what is the problem here either. I'm thinking that maybe it's best to get some feedback on this from the users? Feel free to tag me on the issues that are raised on sinon/lolex repos that you think may be related and we'll see whether the code needs an update or the docs? |
🎉 thanks! |
This is a rebased and updated version of #105.
I still have to do a full review and test in our code base, probably might need to update some documentation, but just wanted to open this up. I probably also need to re-read all the threads on this... But hopefully this is now closer to getting merged than it was before :)