https://kotlinlang.org logo
Title
d

Davide Giuseppe Farella

08/22/2020, 2:12 PM
Can somebody explain me why this fails with
Test finished with active jobs
scope.launch(Io) {
    withTimeout(500) {
        while (stack.isEmpty()) {
            delay(1) // await
        }
        result.data = stack.removeFirst()
    }
}
While this completes successfully
val j = scope.launch(Io) {
    while (stack.isEmpty()) {
        delay(1) // await
    }
    result.data = stack.removeFirst()
}
scope.launch {
    delay(500)
    j.cancel()
}
t

tseisel

08/22/2020, 2:31 PM
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

Davide Giuseppe Farella

08/22/2020, 2:32 PM
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

tseisel

08/22/2020, 2:48 PM
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

Davide Giuseppe Farella

08/22/2020, 2:49 PM
Yup, that’s what I’m doin, it is
Io
, not
IO
🙂
interface DispatchersProvider {

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