Files
kotlin-fork/libraries/stdlib/jvm/test/time/TimeMarkJVMTest.kt
T
Ilya Gorbunov 1014434475 KT-58046 Use saturated math in LongTimeMark implementation
- keep offset in range (-1..+1) of time source units
- when adding big or infinite offset, saturate startedAt instead
- displace initial reading to zero, similar to MonotonicTimeSource
- fix the zero reading in TestTimeSource by calling markNow in constructor
- use more precise reading adjustment in TestTimeSource for big durations
- add a note about comparable time marks for AbstractLongTimeSource and TestTimeSource
2023-04-18 15:25:31 +00:00

31 lines
942 B
Kotlin

/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package test.time
import kotlin.test.*
import kotlin.time.*
import kotlin.time.Duration.Companion.nanoseconds
@OptIn(ExperimentalTime::class)
class TimeMarkJVMTest {
@Test
fun longDurationElapsed() {
TimeMarkTest().testLongAdjustmentElapsedPrecision(TimeSource.Monotonic, { waitDuration -> Thread.sleep((waitDuration * 1.1).inWholeMilliseconds) })
}
@Test
fun defaultTimeMarkAdjustmentInfinite() {
val baseMark = TimeSource.Monotonic.markNow()
val longDuration = Long.MAX_VALUE.nanoseconds
val pastMark = baseMark - longDuration
val infiniteFutureMark1 = pastMark + longDuration * 3
assertEquals(-Duration.INFINITE, infiniteFutureMark1.elapsedNow())
}
}