Stylianos Gakis
12/14/2022, 12:27 PMClock
interface? Basically one where I can control the time passed myself.
Basically I’m trying to run a test where two classes receive a Clock instance, current just passing in Clock.System in, but then now()
resolves two different times but I want for the test for them to resolve on the same now.
I can probably make my own Clock instance where I just always return the same Instance, but just checking if there’s some existing artifact which gives a nice test Clock instance with some more control than what I’d do myself.Dmitry Khalanskiy [JB]
12/14/2022, 12:33 PMpublic class TestClock: Clock {
private var now: Instant = Clock.System.now()
public fun advanceTimeBy(duration: Duration) {
require(duration > Duration.ZERO)
now += duration
}
override fun now(): Instant = now
}
Stylianos Gakis
12/14/2022, 12:39 PMhfhbd
12/14/2022, 2:58 PMrunTest
and its `testTimeSource`:
@ExperimentalTime
public fun TimeSource.toClock(offset: Instant = Instant.fromEpochSeconds(0)): Clock = object : Clock {
private val startMark: TimeMark = markNow()
override fun now() = offset + startMark.elapsedNow()
}
Stylianos Gakis
12/14/2022, 3:01 PM