Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 10 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,16 @@ See Logging in the https://rocketraman.github.io/bootable/[documentation].
* `JSON` no longer writes the logging context's `trace_id`, `span_id`, and `trace_flags` as fields of those names.
* `JSON` and `GCLOUD` also write an existing map message's entries, such as a `StringMapMessage`'s, as top-level fields.

=== Clock

See Clock in the https://rocketraman.github.io/bootable/[documentation].

* `boot-clock` binds the application's clock once, as a `kotlin.time.Clock` and a `java.time.Clock`.
* The `java.time.Clock` wraps the `kotlin.time.Clock`, in UTC, so a test that replaces one replaces both.
* The modules that take a `kotlin.time.Clock` binding, if there is one, take this one.
* `asJavaClock()` and `asKotlinClock()` wrap either clock as the other.
* Koin, Metro, and Inara flavors, and a deprecated Kodein flavor.

=== HTTP clients (Ktor)

See the HTTP client in the https://rocketraman.github.io/bootable/[documentation].
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
@file:JvmName("ClockModuleInara")

package com.github.rocketraman.bootable.clock

import java.time.Clock as JClock
import org.kodein.di.inara.Binding
import org.kodein.di.inara.DIModule
import kotlin.time.Clock

/**
* The Inara module that provides the application's clock, as a `kotlin.time.Clock` and as a `java.time.Clock`, for
* the application's own modules to require. Compose [clockModule], with `Clock.System`, or, in a test, with a clock
* of its own.
*/
@DIModule
interface ClockModule {
val clock: Clock
val javaClock: JClock
}

/** A [ClockModule] on [clock], whose `java.time.Clock` is [clock] in UTC, with [asJavaClock]. */
fun clockModule(clock: Clock = Clock.System): ClockModule = ClockModule(
clock = Binding.value(clock),
javaClock = Binding.single { this.clock.asJavaClock() },
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.github.rocketraman.bootable.clock

import java.time.Clock as JClock
import org.kodein.di.DI
import org.kodein.di.bindSingleton
import org.kodein.di.instance
import kotlin.time.Clock

/**
* A Kodein module that binds the application's clock, [clock], `Clock.System` by default, as a `kotlin.time.Clock`,
* and the same clock as a `java.time.Clock` in UTC, with [asJavaClock].
*
* The `java.time.Clock` binding wraps the `kotlin.time.Clock` binding, so a test that binds its own `kotlin.time.Clock`,
* with `bindSingleton<Clock>(overrides = true) { testClock }`, or that imports `clockModule(testClock)`, sets both.
* The Bootable modules that take a `kotlin.time.Clock` binding, if there is one, take this one.
*/
@Deprecated("Kodein is deprecated: use the -inara flavor, or the -koin flavor for a DSL like Kodein's")
fun clockModule(clock: Clock = Clock.System): DI.Module = DI.Module("clockModule") {
bindSingleton<Clock> { clock }
bindSingleton<JClock> { instance<Clock>().asJavaClock() }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.github.rocketraman.bootable.clock.koin

import com.github.rocketraman.bootable.clock.asJavaClock
import java.time.Clock as JClock
import org.koin.core.module.Module
import org.koin.dsl.module
import kotlin.time.Clock

/**
* A Koin module that defines the application's clock, [clock], `Clock.System` by default, as a `kotlin.time.Clock`,
* and the same clock as a `java.time.Clock` in UTC, with [asJavaClock].
*
* The `java.time.Clock` definition gets the `kotlin.time.Clock` definition, so a test that defines its own
* `kotlin.time.Clock` after this module, with `single<Clock> { testClock }`, or that includes `clockModule(testClock)`,
* sets both. The Bootable modules that take a `kotlin.time.Clock` definition, if there is one, take this one.
*/
fun clockModule(clock: Clock = Clock.System): Module = module {
single<Clock> { clock }
single<JClock> { get<Clock>().asJavaClock() }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.github.rocketraman.bootable.clock

import java.time.Clock as JClock
import java.time.Instant as JInstant
import java.time.ZoneId
import java.time.ZoneOffset
import kotlin.time.Clock
import kotlin.time.Instant
import kotlin.time.toJavaInstant
import kotlin.time.toKotlinInstant

/**
* Wraps the Kotlin `Clock` receiver as a `java.time.Clock` in [zone], UTC by default.
*
* The `java.time.Clock` wrapper simply delegates to the underlying Kotlin `Clock`.
* A good use case for this is to adapt a Kotlin `Clock`, such as a test clock, to a library that takes a
* `java.time.Clock`, without creating a new clock.
*
* [Clock.System] becomes `java.time.Clock.system(zone)`, such as `java.time.Clock.systemUTC()`. A receiver from
* [asKotlinClock] is unwrapped: the result is simply the original wrapped `java.time.Clock`, in [zone].
*/
fun Clock.asJavaClock(zone: ZoneId = ZoneOffset.UTC): JClock = when (this) {
Clock.System -> JClock.system(zone)
is KotlinClockOfJava -> javaClock.withZone(zone)
else -> JavaClockOfKotlin(this, zone)
}

/**
* Wraps the `java.time.Clock` receiver as a Kotlin `Clock`.
*
* The Kotlin `Clock` wrapper simply delegates to the underlying `java.time.Clock`.
* A good use case for this is to adapt a `java.time.Clock` to a Kotlin `Clock`, without creating a new clock, such as
* in tests.
*
* A receiver from [asJavaClock] is unwrapped: the result is simply the original wrapped Kotlin `Clock`.
* `java.time.Clock.systemUTC()`, or any other system clock, becomes [Clock.System].
*/
fun JClock.asKotlinClock(): Clock = when {
this is JavaClockOfKotlin -> clock
this == JClock.system(zone) -> Clock.System
else -> KotlinClockOfJava(this)
}

private class JavaClockOfKotlin(val clock: Clock, private val zone: ZoneId) : JClock() {
override fun getZone(): ZoneId = zone
override fun withZone(zone: ZoneId): JClock = if (zone == this.zone) this else JavaClockOfKotlin(clock, zone)
override fun instant(): JInstant = clock.now().toJavaInstant()
override fun equals(other: Any?) = other is JavaClockOfKotlin && other.clock == clock && other.zone == zone
override fun hashCode() = clock.hashCode() xor zone.hashCode()
override fun toString() = "JavaClock($clock, $zone)"
}

private class KotlinClockOfJava(val javaClock: JClock) : Clock {
override fun now(): Instant = javaClock.instant().toKotlinInstant()
override fun equals(other: Any?) = other is KotlinClockOfJava && other.javaClock == javaClock
override fun hashCode() = javaClock.hashCode()
override fun toString() = "KotlinClock($javaClock)"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.github.rocketraman.bootable.clock

import com.github.rocketraman.bootable.boot.BootScope
import dev.zacsweers.metro.ContributesTo
import dev.zacsweers.metro.Provides
import java.time.Clock as JClock
import kotlin.time.Clock

/**
* Provides the application's clock, `Clock.System`, as a `kotlin.time.Clock`, and the same clock as a
* `java.time.Clock` in UTC, with [asJavaClock].
*
* The `java.time.Clock` is the graph's `kotlin.time.Clock`, so a test that gives its graph another `kotlin.time.Clock`,
* such as with a binding container of its own passed to `createDynamicGraph`, sets both. The Bootable bindings that
* take the graph's `kotlin.time.Clock`, if it has one, take this one.
*/
@ContributesTo(BootScope::class)
interface ClockBindings {
companion object {
@Provides fun clock(): Clock = Clock.System

@Provides fun javaClock(clock: Clock): JClock = clock.asJavaClock()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
@file:Suppress("DEPRECATION") // the deprecated Kodein flavor is still tested

package com.github.rocketraman.bootable.clock

import com.github.rocketraman.bootable.boot.BootScope
import com.github.rocketraman.bootable.boot.koin.bootableModule as koinBootableModule
import com.github.rocketraman.bootable.boot.bootableModule
import com.github.rocketraman.bootable.clock.koin.clockModule as koinClockModule
import com.github.rocketraman.bootable.leader.AlwaysLeaderModule
import com.github.rocketraman.bootable.leader.LeaderElectionModule
import com.github.rocketraman.bootable.periodic.PeriodicModule
import com.github.rocketraman.bootable.periodic.PeriodicServices
import com.github.rocketraman.bootable.periodic.koin.periodicModule as koinPeriodicModule
import com.github.rocketraman.bootable.periodic.periodicModule
import com.github.rocketraman.bootable.telemetry.otel.NoopOpenTelemetryModule
import com.github.rocketraman.bootable.telemetry.otel.OpenTelemetryApiModule
import com.github.rocketraman.bootable.test.TestClock
import de.infix.testBalloon.framework.core.testSuite
import dev.zacsweers.metro.BindingContainer
import dev.zacsweers.metro.DependencyGraph
import dev.zacsweers.metro.Provides
import dev.zacsweers.metro.createDynamicGraph
import dev.zacsweers.metro.createGraph
import java.time.Clock as JClock
import kotlin.time.Duration.Companion.hours
import org.kodein.di.DI
import org.kodein.di.bindSingleton
import org.kodein.di.direct
import org.kodein.di.inara.DIModule
import org.kodein.di.instance
import org.koin.dsl.koinApplication
import org.koin.dsl.module
import kotlin.time.Clock
import kotlin.time.toJavaInstant

sealed interface TestScope

@DependencyGraph(TestScope::class, additionalScopes = [BootScope::class])
interface ClockGraph {
val clock: Clock
val javaClock: JClock
val periodicServices: PeriodicServices
}

/** A test's clock, in place of the graph's. */
@BindingContainer
class TestClockBindings(private val clock: TestClock) {
@Provides fun clock(): Clock = clock
}

/** An application's module, composed as an Inara application composes it, with its clock. */
@DIModule(composed = true)
interface ClockAppModule : ClockModule, LeaderElectionModule, OpenTelemetryApiModule, PeriodicModule

// a call resolves to the Kodein flavor's clockModule, of the same signature; a reference of this type to Inara's
private val inaraClockModule: (Clock) -> ClockModule = ::clockModule

private fun clockAppModule(clock: Clock = Clock.System): ClockAppModule {
val clockModule = inaraClockModule(clock)
return ClockAppModule(
clockModule = clockModule,
leaderElectionModule = AlwaysLeaderModule,
openTelemetryApiModule = NoopOpenTelemetryModule,
periodicModule = periodicModule(clockModule.clock),
)
}

/** Checks [javaClock] wraps [clock], by moving [clock]. */
private fun checkWraps(javaClock: JClock, clock: TestClock) {
assert(javaClock.instant() == clock.now().toJavaInstant())
clock.advanceBy(1.hours)
assert(javaClock.instant() == clock.now().toJavaInstant())
}

val ClockWiringTest by testSuite {
testSuite("kodein") {
test("binds the system clock, as both clocks, for the modules that take a clock") {
val di = DI {
import(bootableModule)
import(clockModule())
import(periodicModule)
}.direct
assert(di.instance<Clock>() == Clock.System)
assert(di.instance<JClock>() == JClock.systemUTC())
assert(di.instance<PeriodicServices>().clock == Clock.System)
}

test("a test's clock, in place of the module's, sets both clocks") {
val testClock = TestClock()
val di = DI {
import(bootableModule)
import(clockModule())
import(periodicModule)
bindSingleton<Clock>(overrides = true) { testClock }
}.direct
assert(di.instance<Clock>() === testClock)
assert(di.instance<PeriodicServices>().clock === testClock)
checkWraps(di.instance(), testClock)
}

test("binds the clock it is given") {
val testClock = TestClock()
val di = DI { import(clockModule(testClock)) }.direct
assert(di.instance<Clock>() === testClock)
checkWraps(di.instance(), testClock)
}
}

testSuite("koin") {
test("defines the system clock, as both clocks, for the modules that take a clock") {
val koin = koinApplication { modules(koinBootableModule(), koinClockModule(), koinPeriodicModule()) }.koin
try {
assert(koin.get<Clock>() == Clock.System)
assert(koin.get<JClock>() == JClock.systemUTC())
assert(koin.get<PeriodicServices>().clock == Clock.System)
} finally {
koin.close()
}
}

test("a test's clock, defined after the module's, sets both clocks") {
val testClock = TestClock()
val koin = koinApplication {
modules(koinBootableModule(), koinClockModule(), koinPeriodicModule(), module { single<Clock> { testClock } })
}.koin
try {
assert(koin.get<Clock>() === testClock)
assert(koin.get<PeriodicServices>().clock === testClock)
checkWraps(koin.get(), testClock)
} finally {
koin.close()
}
}

test("defines the clock it is given") {
val testClock = TestClock()
val koin = koinApplication { modules(koinClockModule(testClock)) }.koin
try {
assert(koin.get<Clock>() === testClock)
checkWraps(koin.get(), testClock)
} finally {
koin.close()
}
}
}

testSuite("metro") {
test("provides the system clock, as both clocks, for the bindings that take a clock") {
val graph = createGraph<ClockGraph>()
assert(graph.clock == Clock.System)
assert(graph.javaClock == JClock.systemUTC())
assert(graph.periodicServices.clock == Clock.System)
}

test("a test's clock, in a dynamic graph, sets both clocks") {
val testClock = TestClock()
val graph = createDynamicGraph<ClockGraph>(TestClockBindings(testClock))
assert(graph.clock === testClock)
assert(graph.periodicServices.clock === testClock)
checkWraps(graph.javaClock, testClock)
}
}

testSuite("inara") {
test("provides the system clock, as both clocks") {
val module = clockAppModule()
assert(module.clock == Clock.System)
assert(module.javaClock == JClock.systemUTC())
assert(module.periodicServices.clock == Clock.System)
}

test("provides the clock it is given, as both clocks") {
val testClock = TestClock()
val module = clockAppModule(testClock)
assert(module.clock === testClock)
assert(module.periodicServices.clock === testClock)
checkWraps(module.javaClock, testClock)
}
}
}
Loading
Loading