TestClock

class TestClock(initial: Instant = Instant.fromEpochMilliseconds(0)) : Clock(source)

Deterministic Clock for time-sensitive tests.

Tests that need to control the passage of time without relying on runTest's virtual scheduler — for instance, when verifying InMemoryStorage timestamps or asserting on time-stamped state — can construct a TestClock, pass it to the system under test, and call advance or set to move virtual wall-clock time forward.

val clock = TestClock(initial = Instant.fromEpochMilliseconds(1_700_000_000_000))
val storage = InMemoryStorage(clock = clock)
storage.saveNode(node)
clock.advance(5.seconds)
// storage entries written before advance() have timestamps 5s in the past

Not thread-safe; intended for single-threaded test usage.

Since

0.1.0

Constructors

Link copied to clipboard
constructor(initial: Instant = Instant.fromEpochMilliseconds(0))

Functions

Link copied to clipboard
fun advance(duration: Duration)

Advance virtual time by duration.

Link copied to clipboard
open override fun now(): Instant
Link copied to clipboard
fun set(time: Instant)

Set virtual time to an exact time.