Can somebody explain me why this fails with `Test ...
# coroutines
d
Can somebody explain me why this fails with
Test finished with active jobs
Copy code
scope.launch(Io) {
    withTimeout(500) {
        while (stack.isEmpty()) {
            delay(1) // await
        }
        result.data = stack.removeFirst()
    }
}
While this completes successfully
Copy code
val j = scope.launch(Io) {
    while (stack.isEmpty()) {
        delay(1) // await
    }
    result.data = stack.removeFirst()
}
scope.launch {
    delay(500)
    j.cancel()
}
t
Check that one of those coroutines is not stuck in a while loop. If you are looking for a way to suspend consumers until an element has been pushed onto the stack, I suggest that you have a look at `Channel`s instead, they do exactly that !
d
The
delay
should be my way out. I tried with
Channel
but I had trouble https://github.com/Kotlin/kotlinx.coroutines/issues/2202 and my code was buggy, so I tried to start from basic things for now
t
Indeed,
runBlockingTest
has some flaws currently, it is unable to work with alternative dispatchers like
<http://Dispatchers.IO|Dispatchers.IO>
. The trick is to inject those `CoroutineDispatcher`s so that you could inject a
TestCoroutineDispatcher
in tests.
d
Yup, that’s what I’m doin, it is
Io
, not
IO
🙂
Copy code
interface DispatchersProvider {

    val Main: CoroutineDispatcher
    val Comp: CoroutineDispatcher
    val Io: CoroutineDispatcher
}