``` @Test fun testFooWithLaunchAndDelay() = runBlo...
# coroutines
p
Copy code
@Test
fun testFooWithLaunchAndDelay() = runBlockingTest {
  foo()
  // the coroutine launched by foo has not completed here, it is suspended waiting for delay(1_000)
  advanceTimeBy(1_000) // progress time, this will cause the delay to resume
  // the coroutine launched by foo has completed here
  // ...
}

suspend fun CoroutineScope.foo() {
  launch {
    println(1)   // executes eagerly when foo() is called due to runBlockingTest
    delay(1_000) // suspends until time is advanced by at least 1_000
    println(2)   // executes after advanceTimeBy(1_000)
  }
}
Trying to learn how to use
kotlinx-coroutines-test
using the above code sample from the docs. Even if the
advanceTimeBy
is removed or if the
delay
time is increased, the test seems to print both 1 and 2 immediately. What am I missing?
v
https://github.com/Kotlin/kotlinx.coroutines/tree/master/kotlinx-coroutines-test#testing-regular-suspend-functions It's a built-in feature of the
runBlockingTest
to automatically advance the time
p
Ah I think I get it. If I block the thread right before exiting the
runBlockingTest
block, I can observe the behavior I was originally expecting to see.