Skip to content

Commit 9ddf337

Browse files
ekkinoxMonkeyDDude
andauthored
doc(fxclock): Updated documentation (#334)
Co-authored-by: Julien Stanek <julien.stanek@ankorstore.com>
1 parent 4704444 commit 9ddf337

File tree

3 files changed

+144
-5
lines changed

3 files changed

+144
-5
lines changed

docs/modules/fxclock.md

+137
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
---
2+
title: Modules - Clock
3+
icon: material/cube-outline
4+
---
5+
6+
# :material-cube-outline: Clock Module
7+
8+
[![ci](https://github.com/ankorstore/yokai/actions/workflows/fxclock-ci.yml/badge.svg)](https://github.com/ankorstore/yokai/actions/workflows/fxclock-ci.yml)
9+
[![go report](https://goreportcard.com/badge/github.com/ankorstore/yokai/fxclock)](https://goreportcard.com/report/github.com/ankorstore/yokai/fxclock)
10+
[![codecov](https://codecov.io/gh/ankorstore/yokai/graph/badge.svg?token=ghUBlFsjhR&flag=fxclock)](https://app.codecov.io/gh/ankorstore/yokai/tree/main/fxclock)
11+
[![Deps](https://img.shields.io/badge/osi-deps-blue)](https://deps.dev/go/github.com%2Fankorstore%2Fyokai%2Ffxclock)
12+
[![PkgGoDev](https://pkg.go.dev/badge/github.com/ankorstore/yokai/fxclock)](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.

fxclock/README.md

+6-5
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,6 @@ For example:
7777
package service
7878

7979
import (
80-
"time"
81-
8280
"github.com/jonboulle/clockwork"
8381
)
8482

@@ -131,27 +129,30 @@ import (
131129
"time"
132130
133131
"github.com/ankorstore/yokai/fxsql"
132+
"github.com/foo/bar/internal"
134133
"github.com/foo/bar/internal/service"
135134
"github.com/jonboulle/clockwork"
135+
"github.com/stretchr/testify/assert"
136136
"go.uber.org/fx"
137137
)
138138
139139
func TestExampleService(t *testing.T) {
140140
testTime := "2025-03-30T12:00:00Z"
141-
expectedTime, _ := time.Parse(time.RFC3339, testTime)
141+
expectedTime, err := time.Parse(time.RFC3339, testTime)
142+
assert.NoError(t, err)
142143
143144
t.Setenv("MODULES_CLOCK_TEST_TIME", testTime)
144145
145146
var svc service.ExampleService
146147
var clock *clockwork.FakeClock
147148
148149
internal.RunTest(t, fx.Populate(&svc, &clock))
149-
150+
150151
// current time as configured above
151152
assert.Equal(t, expectedTime, svc.Now()) // 2025-03-30T12:00:00Z
152153
153154
clock.Advance(5 * time.Hour)
154-
155+
155156
// current time is now advanced by 5 hours
156157
assert.Equal(t, expectedTime.Add(5*time.Hour), svc.Now()) // 2025-03-30T17:00:00Z
157158
}

mkdocs.yml

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ nav:
8383
- "Worker application": demos/worker-application.md
8484
- "Modules":
8585
- "Core": modules/fxcore.md
86+
- "Clock": modules/fxclock.md
8687
- "Config": modules/fxconfig.md
8788
- "Cron": modules/fxcron.md
8889
- "Generate": modules/fxgenerate.md

0 commit comments

Comments
 (0)