If I had a utility function similar to this ```su...
# random
c
If I had a utility function similar to this
Copy code
suspend fun <T> retryWithDelay(
  retryCount: Int
  delay: Duration,
  thing: ()->Unit
How would you all write a unit test for that? I think I'd like to make sure that my delay duration is actually respected, but putting in a test that slows down the rest of the test suite seems bad?
e
kotlinx.coroutines.test.runTest uses virtual time, see its docs
c
So I could roughly assert that my delay was 5 seconds, while only needing to wait a few ms (if IM understanding correctly?)
e
there's no waiting in real time, it automatically advances time to the next runnable coroutine (unless you disable that). but yes, your can assert that the test scheduler time has changed, or switch to
StandardTestDispatcher
and assert that nothing runs at the current time and then does run when you manually advance the time
c
cool. yeah. i think my question is more of a theorhetical one too. where "is it okay to test that x amount of time has passed". because that seems like testing implementation details. which i know the community has gotten really big on lately to not test behavior (i think). the whole mocks vs fakes debate (which i dont 100% follow but yeah)
c
Inject a
@Clock
object, never use static time measurements.
e
or
TimeSource
, but neither is necessary if using `withTimeout`/`withTimeoutOrNull`
c
Oh. thanks for the feedback. will try that out.