Colton Idle
08/21/2023, 3:14 AMSystem.currentTimeMillis()
advanceTimeBy(1.minutes)
System.currentTimeMillis()
but the difference isn't 1 minute. How do you all recommend advancing time so that the System time that is grabbed actually advances?Pablichjenkov
08/21/2023, 3:18 AMadvanceTimeBy(1.minutes)
has any effect on the System time. It affects the test scheduler time which is a different time counter. You have to read the time from the test scheduler.Colton Idle
08/21/2023, 3:24 AMClassUnderTest.markTimestamp()
advanceTimeBy(1.minutes)
ClassUnderTest.markTimestamp()
assertThat(ClassUnderTest...
Maybe my issue is that my markTimestamp() method shouldn't call System.currentTimeMillis. Maybe I can inject something else in there instead? I'll take any recs. 😅Pablichjenkov
08/21/2023, 3:36 AMCLOVIS
08/21/2023, 7:49 AMSystem.currentTime
functions! Production code should access time from a provided clock (via your DI technique of choice), so your tests can control how time passes. Otherwise, it's almost impossible to detect bugs that only happen in specific cases (e.g. bugs that happen the last minute before a day change).
If you use KotlinX.Datetime, here's how you can create a clock from the test time: https://gitlab.com/opensavvy/pedestal/-/blob/main/cache/src/commonTest/kotlin/TestClock.kt?ref_type=heads
If you use some other datetime library, there most likely is a similar concept you can implement in a similar mannerSam
08/21/2023, 9:27 AMTimeSource.Monotonic
. In the test code, use the test coroutine scope's testTimeSource.Sam
08/21/2023, 9:28 AMColton Idle
08/21/2023, 12:26 PMColton Idle
08/21/2023, 12:52 PMCLOVIS
08/21/2023, 12:55 PM