Skip to content

Add TimeSource asClock converter #164

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions core/common/src/Clock.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,17 @@ private class InstantTimeMark(private val instant: Instant, private val clock: C

override fun minus(duration: Duration): TimeMark = InstantTimeMark(instant - duration, clock)
}

/**
* Returns the [Clock] by storing an initial [TimeMark] using [TimeSource.markNow] and [returns][Clock.now] the elapsed
* time using [TimeMark.elapsedNow] plus the provided [offset].
*
* This clock stores the initial [TimeMark], so repeatedly creating [Clock]s from the same [TimeSource] results
* into different [Instant]s iff the time of the [TimeSource] was increased. To sync different [Clock]s, use the [offset]
* parameter.
*/
@ExperimentalTime
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like, after all this time, this is no longer needed!

public fun TimeSource.toClock(offset: Instant = Instant.fromEpochSeconds(0)): Clock = object : Clock {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you considered some other default value for offset, e.g. Clock.System.now(), or no default value at all?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did. The main use-case is converting the testTimeSource of coroutines to a Clock for high-level datetime usage, like Instant, with coroutines sync support, like delay. I prefer deterministic values for easier testing, like assertEquals or using test fixtures. With this use-case, I wouldn't use Clock.System.now() using the current changing time.
I am open to having no default value, but this would require passing an offset each time using this converter and if you want to customize the offset, you can already do it by passing your offset explicitly. So I don't see a reason to remove a convenience default value.
I used the start of the epoch, because, well, it is the start and it's commonly used for test fixtures.

private val startMark: TimeMark = markNow()
override fun now() = offset + startMark.elapsedNow()
}
51 changes: 51 additions & 0 deletions core/common/test/ClockTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2019-2021 JetBrains s.r.o.
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file.
*/

package kotlinx.datetime.test

import kotlinx.datetime.*
import kotlin.test.*
import kotlin.time.*
import kotlin.time.Duration.Companion.seconds

@ExperimentalTime
class ClockTest {
@Test
fun timeSourceToClock() {
val timeSource = TestTimeSource()
val clock = timeSource.toClock()

assertEquals(Instant.fromEpochSeconds(0), clock.now())
assertEquals(Instant.fromEpochSeconds(0), clock.now())

timeSource += 1.seconds
assertEquals(Instant.fromEpochSeconds(1), clock.now())
assertEquals(Instant.fromEpochSeconds(1), clock.now())
}

@Test
fun syncMultipleClocksFromTimeSource() {
val timeSource = TestTimeSource()
val clock1 = timeSource.toClock()

assertEquals(0, clock1.now().epochSeconds)

timeSource += 1.seconds
assertEquals(1, clock1.now().epochSeconds)

val clock2 = timeSource.toClock(offset = Instant.fromEpochSeconds(1))
assertEquals(clock1.now(), clock2.now())

timeSource += 1.seconds
assertEquals(2, clock1.now().epochSeconds)
assertEquals(clock1.now(), clock2.now())

val clock3 = timeSource.toClock(offset = clock2.now())
timeSource += 1.seconds
assertEquals(3, clock3.now().epochSeconds)
assertEquals(clock1.now(), clock2.now())
assertEquals(clock1.now(), clock3.now())
}
}