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

Michael Friend

04/08/2020, 4:11 PM
I think i found a bug in the coroutine test library where exceptions, including test failures, dont cancel chil jobs of the TestCoroutineScope of runBlocking test. The following recreates the issue
Copy code
@Test
fun eternalTest() = testCoroutineRule.testDispatcher.runBlockingTest {
    launch {
        while (isActive) {
            delay(1000)
        }
    }
    Assert.fail()/throw Exception/verify(...)
}
My expectation is that the exception from an assertion failure or otherwise would cancel the testCoroutineScope which then cancels all children, including the code inside launch. What actually happens is that the job in the launch is never cancelled and continues forever, causing runBlockingTest to hang forever. Unless the coroutine test library is injecting a
SupervisorJob
into testCoroutineScope somewhere the cancellation should be propagated. Am i missing something and if not, does anyone know of a workaround?
The same test works as expected when using regular run blocking. The job inside launch is cancelled when
Assert.fail
is called, only printing 5 dots
Copy code
@Test
fun test() {
    runBlocking {
        launch {
            while (isActive) {
                delay(100)
                println(".")
            }
        }
        delay(500)
        Assert.fail()
    }
}
3 Views