Can’t `runCurrent` be used to advance the event lo...
# coroutines
p
Can’t
runCurrent
be used to advance the event loop in case of nested coroutines launched with
UnconfinedTestDispatcher
? Below code outputs
1 2 4 3 5
Copy code
fun test() = runTest(UnconfinedTestDispatcher()) {
  println(1)
  launch {
    println(2)
    launch { println(3) }
    runCurrent()
    println(4)
  }
  println(5)
}
k
I thought we have no control over execution order with 'Unconfined'. Everything is executed eagerly?
p
Except when there are nested coroutines, in order to prevent
StackOverflowException
Same for
Dispatchers.Main.immediate
In which case it forms an event loop
Seems we can’t control this event loop at all in tests.
k
So you would see your expected behavior if you ran this same code in application code inside 'Dispatchers.UNCONFINED'? although those clock operations are only available in coroutines test package
p
Same code would output the same outside tests, yes
k
So it seems there is no case where we control execution order when using an unconfined dispatcher in kotlin. If the behavior is no different between tests and app code
p
It’s been some time and I don’t remember exactly in which context I wanted to manually advance the event loop in tests, but I think In the end I chose to run runTest in standard dispatcher and using CoroutineStart.UNDISPATCHED in real code Real code does want to avoid dispatch
Ok based on this I guess it’s considered a bug