oaskamay
oaskamay

Reputation: 274

Espresso IdlingResource polling frequency

I noticed that Espresso polls a particular IdlingResource's isIdleNow() method every five second.

Is it possible to adjust this granularity?

Thanks in advance!

Upvotes: 3

Views: 690

Answers (1)

Be_Negative
Be_Negative

Reputation: 4972

Short answer:

No, it's baked into the IdlingPolicies.dynamicIdlingResourceWarningPolicy and not controlled by external configuration as of version 3.0.1. This policy controls the frequency of warning events that are posted to the handler. These events, in turn, cause the call to isIdleNow()

Slightly longer answer:

It is like this for a very good reason. isIdleNow() is not a place to set your application state. At the time isIdleNow() is called the state should already be known. The state should be updated with the call to ResourceCallback::onTransitionToIdle and generally(99% of a time) not inside of isIdleNow()

The documentation hints at it when it says that you should return immediately, but does not make it clear.

Updating the state in isIdleNow() makes it no better than just sleeping the thread conditionally. Actually, makes it even worse, as your tests are now 5 seconds slower on a per test basis. Which defeats the purpose of espresso as it kills the determinism of the test. And in my opinion determinism of espresso tests is what makes it so great thanks to the built-in main thread synchronization and idling resource interface.

With that being said there should not be a need to modify this timeout. Calling ResourceCallback::onTransitionToIdle will post this event to the handler and espresso will proceed immediately.

Unfortunately, there are lots of examples and even blogs that promote inefficient implementations.

These examples should be great sources of inspiration if you want a more conventional idling resource:

okhttp idling resource

DrawerActions$IdlingDrawerListener bundled with espresso-contrib. here is the source.

CountingTaskExecutorRule from Room library, here is the usage of it.

Upvotes: 3

Related Questions