You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Say I have a class that encapsulates the actorCounter coroutine (defined here in the docs):
classIntCounter(scope:CoroutineScope) {
privateval actor = scope.counterActor()
suspendfunget(): Int {
val deferred =CompletableDeferred<Int>()
counter.send(CounterMessage.Get(deferred))
return deferred.await()
}
suspendfuninc() {
counter.send(CounterMessage.Increment)
}
}
If I write the following test for this class
fun`Initial counter value should be zero`() = runBlockingTest {
val counter =IntCounter(this)
val initialValue = counter.get()
assertEquals(0, initialValue)
}
Then the test fails with UncompletedCoroutinesError: Test finished with active jobs [ActorCoroutine[Active]@7cbc5d3, which is expected since the actor coroutine is still running after the test block ends.
But then, what is the recommended way of writing such a test ?
I tried multiple constructs, such as
nested runBlockingTest
wrap in coroutineScope
cancelling children with coroutineContext.chancelChildren()
But all are failing the test with various exceptions (respectively UncompletedCoroutinesError, IllegalStateException: This job has not completed yet, and JobCancellationException).
Thanks in advance !
The text was updated successfully, but these errors were encountered:
@ExtendWith(CoroutinesExecutorExtension::class)
classTestClass(privatevaltestCoroutineDispatcher:TestCoroutineDispatcher) {
fun`Initial counter value should be zero`() = runBlockingTest {
val counter =IntCounter(TestCoroutineScope(testCoroutineDispatcher))
val initialValue = counter.get()
assertEquals(0, initialValue)
}
}
Not sure if there is any downside to this, but it worked for me.
Say I have a class that encapsulates the
actorCounter
coroutine (defined here in the docs):If I write the following test for this class
Then the test fails with
UncompletedCoroutinesError: Test finished with active jobs [ActorCoroutine[Active]@7cbc5d3
, which is expected since the actor coroutine is still running after the test block ends.But then, what is the recommended way of writing such a test ?
I tried multiple constructs, such as
runBlockingTest
coroutineScope
coroutineContext.chancelChildren()
But all are failing the test with various exceptions (respectively
UncompletedCoroutinesError
,IllegalStateException: This job has not completed yet
, andJobCancellationException
).Thanks in advance !
The text was updated successfully, but these errors were encountered: