how would i write a unit test for that? because as...
# coroutines
b
how would i write a unit test for that? because as soon as i run this in runBlocking{} in the test doSomething() will be called on the same thread.
e
It will not be called in the same thread in test. By default,
async { ... }
uses
CommonPool
dispatcher and
doSomething
will be executed in that another thread.
c
but we actually want it running in the same thread. can i tell async that it should use the same thread?
it will still be async because the method that we call is a non blocking suspend method
e
You can specify the context for async explicitly if you need it.
async(ctx) { ... }
c
ok so just use unconfined to run it in the same thread?
e
Unconfined
runs in the same thread only until the first suspension point.
If you actually want the same thread, then from inside
runBlocking
you should be passing
coroutineContext
.
c
runBlocking is just what we use in the test suite
e
So, if you want it to run in `UI' thread in production, then you use
async(UI) { … }
and for tests you make UI injectable and use
coroutineContext
that’s available inside
runBlocking