|
| 1 | +--- |
| 2 | +title: Modules - Clock |
| 3 | +icon: material/cube-outline |
| 4 | +--- |
| 5 | + |
| 6 | +# :material-cube-outline: Clock Module |
| 7 | + |
| 8 | +[](https://github.com/ankorstore/yokai/actions/workflows/fxclock-ci.yml) |
| 9 | +[](https://goreportcard.com/report/github.com/ankorstore/yokai/fxclock) |
| 10 | +[](https://app.codecov.io/gh/ankorstore/yokai/tree/main/fxclock) |
| 11 | +[](https://deps.dev/go/github.com%2Fankorstore%2Fyokai%2Ffxclock) |
| 12 | +[](https://pkg.go.dev/github.com/ankorstore/yokai/fxclock) |
| 13 | + |
| 14 | +## Overview |
| 15 | + |
| 16 | +Yokai provides a [fxclock](https://github.com/ankorstore/yokai/tree/main/fxclock) module, that you can use to control time. |
| 17 | + |
| 18 | +It wraps the [clockwork](https://github.com/jonboulle/clockwork) module. |
| 19 | + |
| 20 | +## Installation |
| 21 | + |
| 22 | +First install the module: |
| 23 | + |
| 24 | +```shell |
| 25 | +go get github.com/ankorstore/yokai/fxclock |
| 26 | +``` |
| 27 | + |
| 28 | +Then activate it in your application bootstrapper: |
| 29 | + |
| 30 | +```go title="internal/bootstrap.go" |
| 31 | +package internal |
| 32 | + |
| 33 | +import ( |
| 34 | + "github.com/ankorstore/yokai/fxcore" |
| 35 | + "github.com/ankorstore/yokai/fxclock" |
| 36 | +) |
| 37 | + |
| 38 | +var Bootstrapper = fxcore.NewBootstrapper().WithOptions( |
| 39 | + // modules registration |
| 40 | + fxclock.FxClockModule, |
| 41 | + // ... |
| 42 | +) |
| 43 | +``` |
| 44 | + |
| 45 | +## Usage |
| 46 | + |
| 47 | +This module provides a [clockwork.Clock](https://github.com/jonboulle/clockwork) instance, ready to inject in your code. |
| 48 | + |
| 49 | +This is particularly useful if you need to control time (set time, fast-forward, ...). |
| 50 | + |
| 51 | +For example: |
| 52 | + |
| 53 | +```go title="internal/service/example.go" |
| 54 | +package service |
| 55 | + |
| 56 | +import ( |
| 57 | + "github.com/jonboulle/clockwork" |
| 58 | +) |
| 59 | + |
| 60 | +type ExampleService struct { |
| 61 | + clock clockwork.Clock |
| 62 | +} |
| 63 | + |
| 64 | +func NewExampleService(clock clockwork.Clock) *ExampleService { |
| 65 | + return &ExampleService{ |
| 66 | + clock: clock, |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func (s *ExampleService) Now() string { |
| 71 | + return s.clock.Now().String() |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +See the underlying vendor [documentation](https://github.com/jonboulle/clockwork) for more details. |
| 76 | + |
| 77 | +## Testing |
| 78 | + |
| 79 | +This module provides a [*clockwork.FakeClock](https://github.com/jonboulle/clockwork) instance, that will be automatically injected as `clockwork.Clock` in your constructors in `test` mode. |
| 80 | + |
| 81 | +### Global time |
| 82 | + |
| 83 | +By default, the fake clock is set to `time.Now()` (your test execution time). |
| 84 | + |
| 85 | +You can configure the global time in your test in your testing configuration file (for all your tests), in [RFC3339](https://datatracker.ietf.org/doc/html/rfc3339) format: |
| 86 | + |
| 87 | +```yaml title="configs/config_test.yaml" |
| 88 | +modules: |
| 89 | + clock: |
| 90 | + test: |
| 91 | + time: "2006-01-02T15:04:05Z07:00" # time in RFC3339 format |
| 92 | +``` |
| 93 | +
|
| 94 | +You can also [override this value](https://ankorstore.github.io/yokai/modules/fxconfig/#env-var-substitution), per test, by setting the `MODULES_CLOCK_TEST_TIME` env var. |
| 95 | + |
| 96 | +### Time control |
| 97 | + |
| 98 | +You can `populate` the [*clockwork.FakeClock](https://github.com/jonboulle/clockwork) from your test to control time: |
| 99 | + |
| 100 | +```go title="internal/service/example_test.go" |
| 101 | +package service_test |
| 102 | +
|
| 103 | +import ( |
| 104 | + "testing" |
| 105 | + "time" |
| 106 | +
|
| 107 | + "github.com/ankorstore/yokai/fxsql" |
| 108 | + "github.com/foo/bar/internal" |
| 109 | + "github.com/foo/bar/internal/service" |
| 110 | + "github.com/jonboulle/clockwork" |
| 111 | + "github.com/stretchr/testify/assert" |
| 112 | + "go.uber.org/fx" |
| 113 | +) |
| 114 | +
|
| 115 | +func TestExampleService(t *testing.T) { |
| 116 | + testTime := "2025-03-30T12:00:00Z" |
| 117 | + expectedTime, err := time.Parse(time.RFC3339, testTime) |
| 118 | + assert.NoError(t, err) |
| 119 | +
|
| 120 | + t.Setenv("MODULES_CLOCK_TEST_TIME", testTime) |
| 121 | +
|
| 122 | + var svc service.ExampleService |
| 123 | + var clock *clockwork.FakeClock |
| 124 | +
|
| 125 | + internal.RunTest(t, fx.Populate(&svc, &clock)) |
| 126 | +
|
| 127 | + // current time as configured above |
| 128 | + assert.Equal(t, expectedTime, svc.Now()) // 2025-03-30T12:00:00Z |
| 129 | +
|
| 130 | + clock.Advance(5 * time.Hour) |
| 131 | +
|
| 132 | + // current time is now advanced by 5 hours |
| 133 | + assert.Equal(t, expectedTime.Add(5*time.Hour), svc.Now()) // 2025-03-30T17:00:00Z |
| 134 | +} |
| 135 | +``` |
| 136 | + |
| 137 | +See [tests example](https://github.com/ankorstore/yokai/blob/main/fxclock/module_test.go) for more details. |
0 commit comments