Hi folks, is there a way to test/assert when a fun...
# coroutines
g
Hi folks, is there a way to test/assert when a function in suspended?
g
By any chance, do you have any ready example? This is the code i came up with:
Copy code
@Test
fun shouldSuspendWhenQueueIsEmpty(): Unit = runBlocking(Dispatchers.Default) {
    val dispatcher = newSingleThreadContext("testCoroutine")
    var isSuspended = false
    dispatcher.interceptContinuation(
        object : Continuation<Unit> {
            override val context: CoroutineContext get() = dispatcher
            override fun resumeWith(result: Result<Unit>) {
                isSuspended = true
                result.getOrThrow()
            }
        }
    )
    withContext(dispatcher) {
        val emptyQueue = emptyArrayQueue()
        emptyQueue.dequeue() // suspends forever
    }
    assert(isSuspended) { "when queue is empty, dequeue operations should be suspended" }
}
im trying to test/assert if my queue if is suspended when a consumer tries to dequeue when it's empty.
p
dispatcher.interceptContinuation not modifying dispatcher. It create new coroutine context, chained with dispatcher.
Try to do: val newDispatcher = dispatcher.interceptContinuation(...); with(newDispatcher){...}