https://kotlinlang.org logo
#coroutines
Title
# coroutines
z

zak.taccardi

04/17/2020, 6:25 PM
I want to control whether
delay(..)
reports IDLE or not in an Espresso test dynamically at runtime. What would be the right way to handle this?. For example, if a
delay(..)
launched on
Dispatchers.Default
is currently suspended, I would like to be able to control at any point through the test whether the dispatcher should report idle or not
z

Zach Klippenstein (he/him) [MOD]

04/18/2020, 5:52 PM
You could wrap the dispatchers you're injecting for your tests in ones that tracks delays as idling resources or something. I haven't tried this but it seems like it should work? For that matter, to be more general, you could also probably just wrap your dispatchers with a ContinuationInterceptor that reports all unresumed continuations as idling resources. But I'm not sure if that would handle cancellation.
z

zak.taccardi

04/18/2020, 6:55 PM
the app needs to report idle only when all coroutines are suspended
but this doesn’t account for
delay(..)
or ongoing coroutines (actor coroutine)
I was able to get this working I think by creating a new
CoroutineDispatcher
that wrapped
Dispatchers.Default
and implemented an internal API for
Delay
The coroutines team needs to do a better job of providing APIs and clear guidance for Espresso idling in my opinion
obviously a non-trivial task too
r

rkeazor

04/19/2020, 3:50 PM
@zak.taccardi I think the most common practice, if not the best practice is to use Idiling Resources. Than you can just focus on working with the counter, rather than the coroutine
6 Views