Elffuss BraidHub

kikocis/EDC public

main · 985 BRenderedRawPermalink
1# Shared clock
2
3## Decision
4
5A mockable shared `Clock` is used across EDC modules to get the time.
6
7Other mechanisms for accessing the time, such as calls to `Instant.now()`, `new Date()`, or `System.currentTimeMillis()` shall not be used in production code. They may be used in test code.
8
9## Rationale
10
11This allows writing consistent test code with assertions about time behavior.
12
13## Approach
14
15The shared clock can be injected in service extensions:
16
17```java
18@Inject
19private Clock clock;
20```
21
22or with a convenience method:
23
24```java
25@Override
26public void initialize(ServiceExtensionContext context) {
27 var clock = context.getClock();
28 ...
29}
30```
31
32By default, this provides the system clock. In integration tests, another `Clock` implementation can be registered through mocking:
33
34```java
35@ExtendWith(EdcExtension.class)
36class AnIntegrationTest {
37 @BeforeEach
38 void setUp(EdcExtension extension) {
39 extension.registerServiceMock(Clock.class, Clock.fixed(now, UTC));
40 }
41}
42```
43