https://kotlinlang.org logo
#coroutines
Title
# coroutines
m

marcinmoskala

11/13/2021, 4:48 PM
It seems that
TestCoroutineDispatcher
is waiting for cancelled tasks.
Copy code
@Test
fun test() = runBlockingTest {
    val job = launch {
        launch { delay(1000) }
        launch { delay(1000) }
    }
    delay(100)
    job.cancel()

    // then
    advanceUntilIdle()
    assertEquals(100, currentTime) // Should be true, because after 100 everything is cancelled,
    // but it says expected:<100> but was:<1000>
}
I've created an issue here.
j

Joffrey

11/13/2021, 4:50 PM
Why use
delay(100)
instead of
advanceTimeBy(100)
in your test method though?
☝️ 2
m

marcinmoskala

11/16/2021, 10:46 AM
I respect both ways, but this one seems more intuitive for me. It is like one coroutine is running, and another is observing it.
j

Joffrey

11/16/2021, 11:48 AM
Yeah in any case both should work I believe. Either I'm missing something or this is a bug
m

marcinmoskala

11/16/2021, 1:40 PM
You mean the code snippet? I think of it as a limitation. I imagine that TestCoroutineDispatcher has a list of registered delays it needs to wait for. The problem is that when a coroutine is cancelled, a registered element is not eliminated from this list. It could be, and it would simplify testing.
3 Views