I'm trying to understand the subtleties of new test dispatchers and I'm not sure about the never ending coroutine handling. I have 3 test cases, why the third one passes? ๐งต
Michal Klimczak
06/13/2022, 6:15 AM
Copy code
//times out
@Test
fun defaultScope() = runTest(dispatchTimeoutMs = 2000L) {
this.launch {
flow<Nothing> { awaitCancellation() }.collect()
}
}
//times out
@Test
fun explicitDispatcher() = runTest(StandardTestDispatcher(), dispatchTimeoutMs = 2000L) {
this.launch {
flow<Nothing> { awaitCancellation() }.collect()
}
}
//passes
@Test
fun explicitDispatcherInner() = runTest(dispatchTimeoutMs = 2000L) {
val scope = CoroutineScope(StandardTestDispatcher())
scope.launch {
flow<Nothing> { awaitCancellation() }.collect()
}
}
t
tseisel
06/13/2022, 6:42 AM
The 3rd test breaks structured concurrency: the coroutine scope you created is not attached as a child of the test scope, therefore it is not waiting for it to finish. This also means that this coroutine is leaking between your tests, which is not desirable.