How can I write unit tests for a class wrapping an...
# coroutines
t
How can I write unit tests for a class wrapping an actor coroutine ? Say that I have such class :
Copy code
class Actor<E>(scope: CoroutineScope) {
    private val processor = scope.actor<E> {
        consumeEach(::println)
        println("Finished processing")
    }

    suspend fun process(element: E) {
        processor.send(element)
    }
}
In the following test, the test function never returns due to the actor coroutine still being active :
Copy code
fun test() = runBlocking<Unit> {
    val actor = Actor<String>(this)
    actor.process("Foo")
    actor.process("Bar")
}
I'd like some utility function that cancels the scope the actor runs in, but only after all messages in the actor has been processed. Is it possible to do that with
coroutineScope
or
launch
or do I have to write my own utility function ?