Adecker
02/03/2023, 8:03 PMSystem.currentTimeMillis()
or similar…) that is compatible with the virtual clock time in kotlinx-coroutines-test
? I found a reference to an older method that I don’t think will work since DelayController
is deprecated, but I’m not able to find any methods to replace it.hfhbd
02/03/2023, 8:08 PMAdecker
02/03/2023, 8:13 PMTestCoroutineScheduler
in production code. I’m currently passing in a parameter to provide the time, but it’s not the most convenient to pass it through multiple functions.hfhbd
02/03/2023, 8:15 PMAdecker
02/03/2023, 8:21 PMhfhbd
02/03/2023, 8:26 PMdelay
in your flow
BTW I mean this:
@ExperimentalTime
class TimeSourceContext(timeSource: TimeSource): CoroutineContext.Element, TimeSource by timeSource {
override val key: CoroutineContext.Key<TimeSourceContext> = TimeSourceContext
companion object: CoroutineContext.Key<TimeSourceContext>
}
@Test
fun a() = runTest {
withContext(TimeSourceContext(testTimeSource)) {
val timeSource: TimeSource = coroutineContext[TimeSourceContext]!!
}
}
Adecker
02/03/2023, 8:30 PMhfhbd
02/03/2023, 8:48 PMmeasureTime
or getting a TimeMark
without di.CLOVIS
02/03/2023, 9:40 PMfun foo(clock: Clock) {
println(clock.now())
}
Production usage (or use your favorite DI lib):
foo(Clock.System)
Test usage:
@Test
fun testFoo() = runTest {
foo(testClock())
}
hfhbd
02/03/2023, 10:07 PM