okhttp-idling-resource
okhttp-idling-resource copied to clipboard
Espresso isIdleNow() error
I have a situation and I'm not sure if it's a bug or if I'm doing something wrong. The project I'm working on has a fairly long series of tests, each of which creates and registers an OkHttp3IdlingResource in @Before and unregisters it in @After. Each class passes individually, but if I run the complete set, at some point I start getting the Espresso error: "isIdleNow() is returning true, but a message indicating that the resource has transitioned from busy to idle was never sent"
I'm wondering if there's a race condition with the dispatcher idle callback, since isIdleNow() queries the dispatcher directly, and the dispatcher callback is a Runnable. Following the pattern of a few other IdleResource examples:
http://stackoverflow.com/questions/28674899/isidlenow-is-returning-true-but-a-message-indicating-that-the-resource-has-tr http://blog.sqisland.com/2015/04/espresso-custom-idling-resource.html
I revised isIdleNow() to:
@Override public boolean isIdleNow() {
boolean idle = (dispatcher.runningCallsCount() == 0);
if (idle && callback != null) callback.onTransitionToIdle();
return idle;
}
Obviously, that makes the problem go away. If you think that's appropriate, I'd be happy to contribute the code, but if I'm just using it wrong, I'd be happy for a better solution.
Thanks @JimEH42g for the patch! It worked wonderful on my projects :·)
Also the espresso samples mention the onTransitionToIdle() call:
https://github.com/chiuki/espresso-samples/blob/master/idling-resource-intent-service/app/src/androidTest/java/com/sqisland/espresso/idling_resource/intent_service/IntentServiceIdlingResource.java
I guess it is a new requirement to execute callback.onTransitionToIdle()?
Thanks as well @JimEH42g, it fixed my problem as well. Are you thinking of submitting a pull request?
@JakeWharton please accept the pull request https://github.com/JakeWharton/okhttp-idling-resource/pull/16
It's worth noting that while this fix prevents Espresso complaining, it works around what it's trying to tell you: the IdlingResource contract hasn't been properly fulfilled, as the callback isn't getting called as soon as this resource is idle. This can lead to tests waiting longer than they need to before continuing, which may also lead to them missing things on the UI that disappear quickly.
If you're getting the error this describes, I'd make sure that you're only setting the idleCallback on the OkHttp dispatcher once, and not accidentally overwriting / otherwise clearing it. That's what this error indicated in our case. That happened because we created one of these per OkHttp client, and didn't consider that those clients actually shared the same Dispatcher.