Slackbot
11/27/2022, 12:10 AMjw
11/27/2022, 12:23 AMjanvladimirmostert
11/27/2022, 12:35 AMcontext(Clock)
suspend inline fun <T> slowdown(duration: Duration, block: () -> T): T {
var result: T
val timeTaken = this@Clock.measureTime {
result = block()
}
delay(duration - timeTaken)
return result
}
then in runTest
scope, I should use currentTime
and in normal runBlocking
scope, `System.currentTimeInMilliSeconds`before and after block
...
I guess that'll work
Thanks Jake!ephemient
11/27/2022, 3:51 AMval (result, timeTaken) = measureTimedValue { ... }
ephemient
11/27/2022, 3:53 AMTimeSource
instead of Clock
, defaulting to Monotonic
, but either way you do have to provide the test value yourself since there isn't a public way of extracting it from the coroutine contexthfhbd
11/27/2022, 2:29 PMhfhbd
11/27/2022, 2:29 PMrunTest { testTimeSource.measureTimedValue {} }
ephemient
11/27/2022, 2:30 PMslowdown
function can't pull that out of its context - the test still has to pass it alonghfhbd
11/27/2022, 2:31 PMtestTimeSource
supports delay-skipping.ephemient
11/27/2022, 2:31 PMephemient
11/27/2022, 2:34 PMsuspend fun <T> slowdown(duration: Duration, block: () -> T): T = coroutineScope {
launch { delay(duration) }
block()
}
would achieve the same result without having to deal with time, but in general you do need to use the test timesource or create a fake clock based on test time in order to work with test delay skipping