Rename Clock to TimeSource, ClockMark to TimeMark

Step 2: rename classes and interfaces.
Provide deprecated typealiases to smooth migration.
This commit is contained in:
Ilya Gorbunov
2020-01-13 21:08:00 +03:00
parent a11a25087a
commit 1336da8453
9 changed files with 194 additions and 155 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2020 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.
*/
@@ -49,18 +49,18 @@ class MeasureTimeTest {
@Test
fun measureTimeTestClock() {
val clock = TestClock()
val timeSource = TestTimeSource()
val expectedNs = Random.nextLong(1_000_000_000L)
val elapsed = clock.measureTime {
clock += expectedNs.nanoseconds
val elapsed = timeSource.measureTime {
timeSource += expectedNs.nanoseconds
}
assertEquals(expectedNs.nanoseconds, elapsed)
val expectedResult: Long
val (result, elapsed2) = clock.measureTimedValue {
clock += expectedNs.nanoseconds
val (result, elapsed2) = timeSource.measureTimedValue {
timeSource += expectedNs.nanoseconds
expectedResult = expectedNs
expectedNs
}
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2020 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.
*/
@@ -9,54 +9,54 @@ package test.time
import kotlin.test.*
import kotlin.time.*
class TestClockTest {
class TestTimeSourceTest {
@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 }
assertFailsWith<IllegalStateException>(enormousDuration.toString()) { TestTimeSource() += enormousDuration }
assertFailsWith<IllegalStateException>((-enormousDuration).toString()) { TestTimeSource() += -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 }
val timeSource = TestTimeSource()
timeSource += moderatePositiveDuration
assertFailsWith<IllegalStateException>("Should overflow positive") { timeSource += moderatePositiveDuration }
}
run {
val clock = TestClock()
clock += borderlinePositiveDuration
assertFailsWith<IllegalStateException>("Should overflow positive") { clock += 1.nanoseconds }
val timeSource = TestTimeSource()
timeSource += borderlinePositiveDuration
assertFailsWith<IllegalStateException>("Should overflow positive") { timeSource += 1.nanoseconds }
}
run {
val clock = TestClock()
clock += borderlineNegativeDuration
assertFailsWith<IllegalStateException>("Should overflow negative") { clock += -1.nanoseconds }
val timeSource = TestTimeSource()
timeSource += borderlineNegativeDuration
assertFailsWith<IllegalStateException>("Should overflow negative") { timeSource += -1.nanoseconds }
}
run {
val clock = TestClock()
clock += moderatePositiveDuration
val timeSource = TestTimeSource()
timeSource += moderatePositiveDuration
// does not overflow event if duration doesn't fit in long
clock += -moderatePositiveDuration + borderlineNegativeDuration
timeSource += -moderatePositiveDuration + borderlineNegativeDuration
}
}
@Test
fun nanosecondRounding() {
val clock = TestClock()
val mark = clock.markNow()
val timeSource = TestTimeSource()
val mark = timeSource.markNow()
repeat(10_000) {
clock += 0.9.nanoseconds
timeSource += 0.9.nanoseconds
assertEquals(Duration.ZERO, mark.elapsedNow())
}
clock += 1.9.nanoseconds
timeSource += 1.9.nanoseconds
assertEquals(1.nanoseconds, mark.elapsedNow())
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2020 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)
@@ -8,27 +8,27 @@ package test.time
import kotlin.test.*
import kotlin.time.*
class ClockMarkTest {
class TimeMarkTest {
@Test
fun adjustment() {
val clock = TestClock()
val timeSource = TestTimeSource()
fun ClockMark.assertHasPassed(hasPassed: Boolean) {
fun TimeMark.assertHasPassed(hasPassed: Boolean) {
assertEquals(!hasPassed, this.hasNotPassedNow(), "Expected mark in the future")
assertEquals(hasPassed, this.hasPassedNow(), "Expected mark in the past")
assertEquals(!hasPassed, this.elapsedNow() < Duration.ZERO, "Mark elapsed: ${this.elapsedNow()}, expected hasPassed: $hasPassed")
}
val mark = clock.markNow()
val mark = timeSource.markNow()
val markFuture1 = (mark + 1.milliseconds).apply { assertHasPassed(false) }
val markFuture2 = (mark - (-1).milliseconds).apply { assertHasPassed(false) }
val markPast1 = (mark - 1.milliseconds).apply { assertHasPassed(true) }
val markPast2 = (markFuture1 + (-2).milliseconds).apply { assertHasPassed(true) }
clock += 500_000.nanoseconds
timeSource += 500_000.nanoseconds
val elapsed = mark.elapsedNow()
val elapsedFromFuture = elapsed - 1.milliseconds
@@ -44,7 +44,7 @@ class ClockMarkTest {
markFuture1.assertHasPassed(false)
markPast1.assertHasPassed(true)
clock += 1.milliseconds
timeSource += 1.milliseconds
markFuture1.assertHasPassed(true)
markPast1.assertHasPassed(true)