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

ember-testing with scheduled timers fails #3008

Closed
jschilli opened this issue Jul 17, 2013 · 26 comments
Closed

ember-testing with scheduled timers fails #3008

jschilli opened this issue Jul 17, 2013 · 26 comments
Labels

Comments

@jschilli
Copy link

the wait method in ember-testing is checking for scheduledTimers (https://github.com/emberjs/ember.js/blob/master/packages/ember-testing/lib/helpers.js#L102) and returns if any are active.

We have several scheduled timers running in the app via run.later

When a test action (e.g. click) gets beyond the login in our application, the timers start and all subsequent thennables are blocked.

I'm happy to construct a fix (or failing test case) but need to understand what the motivation for checking hasTimers is.

@teddyzeenny
Copy link
Contributor

wait() makes sure all async operations have completed before it resolves. In fact that's what wait is for. run.later is an async operation so it is supposed to block wait.

We need to figure out how to tell wait to skip waiting for specific timers. In the meantime, replacing run.later with setTimeout(function() { Em.run(function() { //.. code here } }); is a possible workaround.

@jschilli
Copy link
Author

thx. The workaround you propose does indeed resolve it.

agreed on telling wait to skip timers - not sure where to dig in on that - will review.

@hajee
Copy link

hajee commented Oct 10, 2013

Any progress on this one? I have this problem when testing a function that calls Ember data's store.push(). I haven't looked into it yet, but store.push() creates some scheduled timers. The test fails because it wait's indefinitely . Because it is Ember Data code, I cannot use the work around.

@stefanpenner
Copy link
Member

@hajee can you open a ED specific issue (with example, so we can replicate) and link it back to this one.

@wagenet
Copy link
Member

wagenet commented Nov 9, 2013

@hajee What's the status of this?

@hajee
Copy link

hajee commented Nov 9, 2013

I've not been able to recreate the symptoms with any test.

@wagenet
Copy link
Member

wagenet commented Nov 9, 2013

@hajee but you're still seeing the problem?

@hajee
Copy link

hajee commented Nov 10, 2013

No. I've changed my test setup.

@wagenet wagenet closed this as completed Nov 10, 2013
@balinterdi
Copy link
Contributor

The problem @jschilli mentioned is still valid. In our application, I poll for new notifications every 5 seconds.

The acceptance test will wait indefinitely because Ember.run.hasScheduledTimers() always returns true and thus the wait helper in ember-testing waits 10 more msecs each time. The solution proposed by @teddyzeenny seems a good one.

@stefanpenner stefanpenner reopened this Jan 30, 2014
@mixonic
Copy link
Member

mixonic commented Mar 31, 2014

We usually have a flag for test environments where we disable nextTick style loops. I don't believe wait needs to change here, but maybe wait should be better documented regarding this behavior?

Unless we can come up with an alternative API.

@balinterdi
Copy link
Contributor

@mixonic Could you give an insight into how you guys do that? I find that the tests that exercise "run-loop scheduling" (e.g Ember.run.later) are super flaky and what you describe might improve on that. Thank you.

@rwjblue
Copy link
Member

rwjblue commented Jul 13, 2014

Would love to see a PR documenting this behavior on wait() helper.

Closing this as the suggestion by both @teddyzeenny (using setTimeout instead) and @mixonic (disabling run.later's in testing) work well.

@rwjblue rwjblue closed this as completed Jul 13, 2014
@bjornstar
Copy link

This issue wasted several days of development time as a user new to Ember starting to test an existing Ember app. I had absolutely no idea why the visit promise wasn't resolving, all tests just timed out with no hint as to what might be causing the problem.

It was a horrible experience.

If you put the sample code from Continuous Redrawing of Views into your app you will be unable to run integration tests in your app.

Here's a jsbin that illustrates the problem: http://jsbin.com/renapukufu/1/edit?html,js,output

@sheldonbaker
Copy link
Contributor

I was also bitten by this a couple weeks ago. Some additional documentation in the testing guide would be very helpful.

@ebryn
Copy link
Member

ebryn commented Feb 5, 2015

I think we might want to add some information about the run loop to the Ember Inspector to help you see what's going on. What do you think @teddyzeenny?

@workmanw
Copy link

workmanw commented Feb 6, 2015

It does seem like this trips a lot of people up. FWIW, this was our solution: http://discuss.emberjs.com/t/proper-way-to-handler-timers-w-ember-testing/4693/9

EDIT: Though instead of using injection, a service might be perfect for a "config". Just a thought.

@teddyzeenny
Copy link
Contributor

@ebryn I think a run loop tab and an ember-testing tab (now that the inspector works in tests) would be really helpful.

rafalp added a commit to rafalp/Misago that referenced this issue Mar 1, 2015
@fishermand46
Copy link

I've been bit on this again. Trying to test some code that uses Ember.run.later. I really don't like the idea of running a different code path in testing vs. production. That's a sign that your code isn't testable to begin with.

@oliverwilkie
Copy link

This has caught me too as I had a live-updating clock on the page that was being tested.

@durkie
Copy link

durkie commented Jan 17, 2016

argh, me too. a chrome app with an auto-refreshing list of serial ports. any documentation about this would be great...i had to really dig down in to andThen() to find out that it had scheduled timers, and that they were a problem.

@PhilLehmann
Copy link

PhilLehmann commented Jul 5, 2017

I also walked into this trap while calling moment.js in a run.later recursion in order to wait 20 seconds before updating the moment strings.

I am now disabling that recursion if Ember.testing is set to true, as acceptance tests will hopefully not run that long anyway.

Ideal would be to add a parameter to run.later, which would mark it as "unblocking" for tests. Otherwise, we would have to keep a central list of timer ids up to date, that seems error prone... What do you think about this?

@aprescott
Copy link

I don't know if a different issue replaced this one, but I think this should be considered a bug. I've recently hit this problem, with tests which time out because I have an run.later call which recursively sets up another run.later. I'd like to use Ember.run to stay within the framework instead of switching to setTimeout. Like another comment above says, codepaths which branch based on testing vs. not testing is generally a bad sign.

@mixonic
Copy link
Member

mixonic commented Jul 24, 2017

@aprescott unfortunately the current testing API design is simply limited in this way. We definitely cannot change it without a semver violation, and indeed this is just a tricky problem to solve regardless of how free our hand is.

I can suggest using ember-lifeline and pollTask which provides a more testable polling mechanism. Instead of a loop running forever and blocking tests, you must manually tick the polling task during test. I think ember-concurrency has a similar API now or is considering one for the future.

@micky2be
Copy link

micky2be commented Aug 1, 2017

The only way to make it work for me was to override Ember.run.later and Ember.run.next with a setTimeout counterpart when running my test.
For some reason functions in later or next do not run and cause my tests to time out.

@johnnyshields
Copy link
Contributor

@mixonic Micky above is the second senior dev from my company who wasted significant time dealing with issues due to the unintuitive API design here. Please consider improving this in Ember 3.

@tsing80
Copy link

tsing80 commented Aug 4, 2017

angular has similiar problem as well : angular/protractor#169

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

No branches or pull requests