Introduce TestClock.plusAssign(Duration) and hide implementation details

TestClock.plusAssign(Duration) is for advancing TestClock time.
Hide reading value, do not allow to provide initial reading,
fix clock unit to nanoseconds at construction time.
This commit is contained in:
Ilya Gorbunov
2019-08-08 22:40:36 +03:00
parent 4de9361c37
commit f889d25287
5 changed files with 111 additions and 20 deletions
+2 -2
View File
@@ -12,7 +12,7 @@ class ClockMarkTest {
@Test
fun adjustment() {
val clock = TestClock(unit = DurationUnit.NANOSECONDS)
val clock = TestClock()
val mark = clock.mark()
val markFuture1 = mark + 1.milliseconds
@@ -21,7 +21,7 @@ class ClockMarkTest {
val markPast1 = mark - 1.milliseconds
val markPast2 = markFuture1 + (-2).milliseconds
clock.reading = 500_000L
clock += 500_000.nanoseconds
val elapsed = mark.elapsed()
val elapsedFromFuture = elapsed - 1.milliseconds
@@ -47,11 +47,12 @@ class MeasureTimeTest {
}
@Test fun measureTimeTestClock() {
val clock = TestClock(unit = DurationUnit.NANOSECONDS)
@Test
fun measureTimeTestClock() {
val clock = TestClock()
val expectedNs = Random.nextLong(1_000_000_000L)
val elapsed = clock.measureTime {
clock.reading += expectedNs
clock += expectedNs.nanoseconds
}
assertEquals(expectedNs.nanoseconds, elapsed)
@@ -59,7 +60,7 @@ class MeasureTimeTest {
val expectedResult: Long
val (result, elapsed2) = clock.measureTimedValue {
clock.reading += expectedNs
clock += expectedNs.nanoseconds
expectedResult = expectedNs
expectedNs
}
@@ -0,0 +1,62 @@
/*
* Copyright 2010-2019 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.
*/
@file:UseExperimental(ExperimentalTime::class)
package test.time
import kotlin.test.*
import kotlin.time.*
class TestClockTest {
@Test
fun overflows() {
for (enormousDuration in listOf(Duration.INFINITE, Double.MAX_VALUE.nanoseconds, Long.MAX_VALUE.nanoseconds * 2)) {
assertFailsWith<IllegalStateException>(enormousDuration.toString()) { TestClock() += enormousDuration }
assertFailsWith<IllegalStateException>((-enormousDuration).toString()) { TestClock() += -enormousDuration }
}
val moderatePositiveDuration = Long.MAX_VALUE.takeHighestOneBit().nanoseconds
val borderlinePositiveDuration = Long.MAX_VALUE.nanoseconds // rounded to 2.0^63, which is slightly more than Long.MAX_VALUE
val borderlineNegativeDuration = Long.MIN_VALUE.nanoseconds
run {
val clock = TestClock()
clock += moderatePositiveDuration
assertFailsWith<IllegalStateException>("Should overflow positive") { clock += moderatePositiveDuration }
}
run {
val clock = TestClock()
clock += borderlinePositiveDuration
assertFailsWith<IllegalStateException>("Should overflow positive") { clock += 1.nanoseconds }
}
run {
val clock = TestClock()
clock += borderlineNegativeDuration
assertFailsWith<IllegalStateException>("Should overflow negative") { clock += -1.nanoseconds }
}
run {
val clock = TestClock()
clock += moderatePositiveDuration
// does not overflow event if duration doesn't fit in long
clock += -moderatePositiveDuration + borderlineNegativeDuration
}
}
@Test
fun nanosecondRounding() {
val clock = TestClock()
val mark = clock.mark()
repeat(10_000) {
clock += 0.9.nanoseconds
assertEquals(Duration.ZERO, mark.elapsed())
}
clock += 1.9.nanoseconds
assertEquals(1.nanoseconds, mark.elapsed())
}
}