-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Provide a way to determine if TestCoroutineDispatcher is "idle" #1202
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
Comments
But why mutliple test dispatchers are needed? Why one is not enough? What practical problem is being solved by using multiple test dispatchers? |
cc @yigit @manuelvicnt who have provided the feedback that they wanted use multiple dispatchers. |
When testing complex scenarios that involve multiple dispatchers (for example in Android, a Obviously, you wouldn't inject multiple |
I'm curious: Is this for testing possible race-conditions that could happen between UI coroutines running on the main dispatcher vs storage/networking coroutines running on the IO dispatcher in the actual app? |
@elizarov on Android Espresso UI tests, we are testing the application from within. The Android apps are running on different dispatchers to make sure the background work is not blocking the user from clicking. We want to test these apps from Espresso JUnit tests running on devices which have the concept of IdlingResources, which block every single assertion/action within Espresso: all registered idling resources have to be idle before anything is actioned/verified. This is to wait for background work to mutate the UI, similar to how a manual tester / user would wait for the app to settle (e.g. progress bars to disappear, results to appear) before continuing on. To implement this we could use a TestCoroutineDispatcher to listen for idle and an IdlingResource to block Espresso when not idle. We would wrap each of the IO/Main/Computation dispatchers individually. When running tests we want to get an environment as close to real as possible, so using a single dispatcher would mean we would be change real behavior and hiding potential threading issues, while all we want is to know "when things are done". |
Has there been any progress on this? I noticed the API already exists on |
When writing an advanced test that uses multiple related
TestCoroutineDispatcher
instances, there is a problem that can occur when one dispatches a task to another:In this contrived example, there's an infinite recursion so it will never resolve, but this problem comes up in normal test code.
This also comes up in a more typical case of testing
withContext
. To testwithContext
is called, the easiest way is to inject a dispatcher to intercept the coroutine started bywithContext
. However, in the current API for TestCoroutineDispatcher there is no way to determine if a task has been sent to a dispatcher.In addition, this sort of inspection is important for implementing an Espresso IdlingResource as discussed in #242.
A bit of a complication here (since this should fit well with the Espresso integration which likely needs to work w/ regular dispatchers). A dispatcher has the ability to differentiate between two states without introducing extra tracking:
Idle
(all coroutines are suspended, or delayed into the future)Busy
(some coroutine is currently running)However, a TestCoroutineDispatcher can differentiate between three states
Idle
(all coroutines are suspended)Busy
(some coroutine is currently running)DelayQueued(long)
(some coroutine will resume after long delay)No concrete API proposals at this second - wanted to make a issue to track this.
The text was updated successfully, but these errors were encountered: