'Testing parallel suspend functions in Kotlin
I have a function that takes two suspend functions and runs them in parallel, I'm using Arrow to do this with parZip and either
fun <E, R1, R2, R3> combineCalls(
call1: suspend () -> Either<E, R1>,
call2: suspend () -> Either<E, R2>,
combine: (R1, R2) -> R3
): suspend () -> Either<E, R3> = suspend {
either {
parZip({ call1().bind() }, { call2().bind() }) { r1, r2 ->
r1 to r2
}
}
}
So which ever of call1 or call2 fails first (returns an Either.Left) will cause the other one to be cancelled and that error will be immediately returned.
I want to test this, but I'm not sure how to ensure which call function will return first.
@Test
fun basicCombinationErrorBoth() = runTest {
val result = combineCalls({ 400.left() }, {
delay(100)
500.left()
})()
result assertEquals 400.left()
}
I'm using runTest so that it doesn't take up actual time (if we have a lot of tests with delays it adds up), but this means the delay is skipped and the test only passes sometimes as it's indeterminate which passed in suspend function returns first.
How can I use runTest and ensure that one suspend function takes more virtual time than the other?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
