paulblessing
08/27/2019, 3:52 PM@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?Vlad
08/27/2019, 3:56 PMrunBlockingTest
to automatically advance the timepaulblessing
08/27/2019, 4:05 PMrunBlockingTest
block, I can observe the behavior I was originally expecting to see.