gumil
01/22/2019, 9:01 PMlaunch
? If I understand correctly launch
always run asynchronously and in tests we would like to run it sequentially.Allan Wang
01/22/2019, 9:01 PMrunBlocking
then use .join()
gumil
01/22/2019, 9:05 PMlaunch
is abstracted? I don't have access to the job
ghedeon
01/22/2019, 9:05 PMrunBlocking
is enough. Plus, you need to override Main dispatcher on AndroidAllan Wang
01/22/2019, 9:08 PMgumil
01/22/2019, 9:13 PMrunBlocking { joinAll() }
work too?Allan Wang
01/22/2019, 9:15 PMDico
01/23/2019, 3:03 AMparent.join()
should join all.
launch
is not always asynchronous, it depends on the coroutine context.gumil
01/23/2019, 9:25 AMlaunch
is abstracted so I am not able to get the job
. I had to wrap it in an async
block and await()
until everything is finished. Do you guys know a cleaner way to do this?Dico
01/23/2019, 9:32 AMcoroutineContext[Job]
gumil
01/23/2019, 9:45 AMfun main(args: Array<String>) = runBlocking {
abstractedCallToLaunch() // which I cannot get the job of the call to launch
println("this should execute after call to launch")
}
fun CoroutineScope.abstractedCallToLaunch() {
launch {
delay(1000)
println("do something")
}
}
this should execute after call to launch
do something
but it should be the other way aroundDico
01/23/2019, 9:48 AMrunBlocking
which is an event loop on the blocked thread.coroutineContext[Job]!!.joinChildren()
yield()
should work too, but it might not wait for full completionghedeon
01/23/2019, 10:06 AMRxAndroidPlugins/InstantTaskExecutorRule
but it's not coming: https://github.com/Kotlin/kotlinx.coroutines/issues/102. Basically, your launch
has a dispatcher attached, so it's considered your responsibility to inject the right dispatcher in your test, so the execution becomes synchronous.gumil
01/23/2019, 10:30 AMlaunch
always run asynchronously. I’m not sure how to make it run synchronouslyghedeon
01/23/2019, 10:44 AMfun test(){
blah blah
runBlocking{
foo.bar() // <-- launch inside
}
assert(...)
}
Dico
01/23/2019, 11:25 AMcoroutineScope { foo.bar() }