I'm trying to understand the subtleties of new tes...
# coroutines
m
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? 🧵
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
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.
🙏 1
m
yeah, if I join() on it, it times out as well...