A test that sleeps for a second to verify a one-second delay is slow by design. A test that sleeps a little longer to avoid failing on CI is also difficult to trust.
For shared coroutine logic, virtual time gives you a better question: what should have happened before this deadline, and what should happen at it?
Make the delayed behavior small
Here is a deliberately tiny piece of shared code:
import kotlinx.coroutines.delay
class DelayedRefresh(
private val refresh: suspend () -> Unit,
) {
suspend fun run() {
delay(1_000)
refresh()
}
}
The delay is only an example. In a real feature, a product rule should justify it. The important design choice is that the class suspends in its caller's coroutine instead of creating an unrelated scope.
Assert the boundary explicitly
Put this test in commonTest with kotlin.test and kotlinx-coroutines-test dependencies:
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.
Discussion
Take the lead—comment now
Lead the way—your insights can inspire others.