Skip to content

Add boot-clock: the application's clock, bound once - #126

Merged
rocketraman merged 2 commits into
masterfrom
rg/clock
Nov 25, 2026
Merged

rocketraman merged 2 commits into
masterfrom
rg/clock

Conversation

@rocketraman

@rocketraman rocketraman commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

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

  • A kotlin.time.Clock, Clock.System by default, which is the type Bootable's modules already take.
  • A java.time.Clock, which many Java libraries take. It wraps the kotlin.time.Clock binding, in UTC, so replacing that binding replaces both.
  • asJavaClock(zone = UTC) and asKotlinClock() in the main module wrap either clock as the other. They wrap the clock rather than copy its time, so moving a TestClock moves its java.time.Clock too. Clock.System maps to java.time.Clock.systemUTC(), and a kotlin.time.Clock converted to a java.time.Clock and back is the original clock (a java.time.Clock converted the other way comes back in UTC, unless given its own zone).

Per flavor

Flavor Binding How a test replaces it
Kodein (deprecated) clockModule(clock = Clock.System) clockModule(testClock), or bindSingleton<Clock>(overrides = true)
Koin clockModule(clock = Clock.System) clockModule(testClock), or a later single<Clock>
Metro ClockBindings, contributed to BootScope createDynamicGraph<AppGraph>(containerProvidingClock)
Inara ClockModule (clock, javaClock), built by clockModule(clock = Clock.System) clockModule(testClock)

The existing modules

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 when one exists, and fall back to Clock.System. So with boot-clock they use the shared clock, and nothing in those modules changes. The tests check this for boot-periodic's PeriodicServices in 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

  • New module, not boot-common. boot-common holds internal helpers with no DI flavors. A separate boot-clock keeps the clock opt-in.
  • Same-named builders. In the same package, the Kodein and Inara flavors both declare clockModule(clock: Clock). This follows the precedent of jdbcTracingModule, and it only matters when both flavors are on one classpath, as in this module's tests.
  • Metro duplicate binding. Because ClockBindings is 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

  • A new Clock function page, with wiring and a test override for each wiring style. Its snippets are compiled: a new snippets/clock/Clock.kt per style, in a new docs/snippets/metro/clock project for Metro, plus the shared classes in docs/snippets/common.
  • boot-clock as the Clock function in modules.yml, a pointer from the testing page's clock section, and in the TestClock kdoc.
  • A CHANGELOG entry.

@rocketraman
rocketraman marked this pull request as ready for review November 25, 2026 19:19
@claude

claude Bot commented Nov 25, 2026

Copy link
Copy Markdown

Code review

No issues found. Checked for bugs and CLAUDE.md compliance.

@github-actions

github-actions Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
PR Preview Action v1.8.2
Preview removed because the pull request was closed.
2026-11-25 21:27 UTC

@rocketraman
rocketraman force-pushed the rg/clock branch 5 times, most recently from 269a09d to 3194c07 Compare November 25, 2026 21:16
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
rocketraman merged commit 89ec641 into master Nov 25, 2026
3 checks passed
@rocketraman
rocketraman deleted the rg/clock branch November 25, 2026 21:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant