Repository navigation
Add boot-clock: the application's clock, bound once - #126
Merged
Merged
Conversation
rocketraman
marked this pull request as ready for review
November 25, 2026 19:19
Code reviewNo issues found. Checked for bugs and CLAUDE.md compliance. |
|
rocketraman
force-pushed
the
rg/clock
branch
5 times, most recently
from
November 25, 2026 21:16
269a09d to
3194c07
Compare
Applications bind a clock by hand, often as both a kotlin.time.Clock and a java.time.Clock, and their tests replace it to set the time. boot-clock binds it once, in each DI flavor: - Kodein clockModule(clock) and Koin clockModule(clock) bind Clock.System by default; Metro's ClockBindings contributes it to BootScope; Inara's clockModule(clock) is a ClockModule for the application's modules to require. - The java.time.Clock is the kotlin.time.Clock in UTC, read from the kotlin.time.Clock binding, so a test that replaces that binding replaces both. - asJavaClock() and asKotlinClock() view either clock as the other. The Kodein, Koin, and Metro bindings of boot-periodic, boot-leader-k8s, boot-scheduler-db, and boot-telemetry-otel already take a kotlin.time.Clock binding if there is one, so they take this one; the tests check boot-periodic's in each flavor.
A Clock function page, with the wiring and a test's override in each wiring style, compiled as snippets, in a Metro snippets project of its own; boot-clock in the modules list; and pointers from the testing page and TestClock. Adds a CHANGELOG entry.
rocketraman
force-pushed
the
rg/clock
branch
from
November 25, 2026 21:17
3194c07 to
0c31cae
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds
boot-clock, which binds the application's clock once, so an application doesn't bind its own and a test replaces it in one place.What it binds
kotlin.time.Clock,Clock.Systemby default, which is the type Bootable's modules already take.java.time.Clock, which many Java libraries take. It wraps thekotlin.time.Clockbinding, in UTC, so replacing that binding replaces both.asJavaClock(zone = UTC)andasKotlinClock()in the main module wrap either clock as the other. They wrap the clock rather than copy its time, so moving aTestClockmoves itsjava.time.Clocktoo.Clock.Systemmaps tojava.time.Clock.systemUTC(), and akotlin.time.Clockconverted to ajava.time.Clockand back is the original clock (ajava.time.Clockconverted the other way comes back in UTC, unless given its own zone).Per flavor
clockModule(clock = Clock.System)clockModule(testClock), orbindSingleton<Clock>(overrides = true)clockModule(clock = Clock.System)clockModule(testClock), or a latersingle<Clock>ClockBindings, contributed toBootScopecreateDynamicGraph<AppGraph>(containerProvidingClock)ClockModule(clock,javaClock), built byclockModule(clock = Clock.System)clockModule(testClock)The existing modules
The Kodein, Koin, and Metro bindings of
boot-periodic,boot-leader-k8s,boot-scheduler-db, andboot-telemetry-otelalready take akotlin.time.Clockbinding when one exists, and fall back toClock.System. So withboot-clockthey use the shared clock, and nothing in those modules changes. The tests check this forboot-periodic'sPeriodicServicesin every flavor.Inara has no optional bindings, so those modules' Inara builders still take the clock as a parameter, such as
periodicModule(clock). The application passes them the same clock.Review notes
boot-common.boot-commonholds internal helpers with no DI flavors. A separateboot-clockkeeps the clock opt-in.clockModule(clock: Clock). This follows the precedent ofjdbcTracingModule, and it only matters when both flavors are on one classpath, as in this module's tests.ClockBindingsis contributed, a graph that also has@Provides fun clock()of its own gets a duplicate-binding error. Such a graph should drop its own provider.Docs
snippets/clock/Clock.ktper style, in a newdocs/snippets/metro/clockproject for Metro, plus the shared classes indocs/snippets/common.boot-clockas the Clock function inmodules.yml, a pointer from the testing page's clock section, and in theTestClockkdoc.