Skip to content

Commit 6734041

Browse files
committed
GH-1490 - Allow resetting the TimeMachine shift.
1 parent 02bd158 commit 6734041

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

spring-modulith-moments/src/main/java/org/springframework/modulith/moments/support/Moments.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,13 @@ Moments shiftBy(Duration duration) {
123123
return this;
124124
}
125125

126+
Moments reset() {
127+
128+
this.shift = Duration.ZERO;
129+
130+
return this;
131+
}
132+
126133
/*
127134
* (non-Javadoc)
128135
* @see org.springframework.modulith.moments.support.Now#now()

spring-modulith-moments/src/main/java/org/springframework/modulith/moments/support/TimeMachine.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,13 @@ public LocalDateTime now() {
5959
public Moments shiftBy(Duration duration) {
6060
return super.shiftBy(duration);
6161
}
62+
63+
/**
64+
* @see org.springframework.modulith.moments.support.Moments#reset()
65+
* @since 2.0.1
66+
*/
67+
@Override
68+
public Moments reset() {
69+
return super.reset();
70+
}
6271
}

spring-modulith-moments/src/test/java/org/springframework/modulith/moments/support/MomentsUnitTests.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.time.temporal.WeekFields;
3030
import java.util.Locale;
3131

32+
import org.assertj.core.data.TemporalUnitOffset;
3233
import org.junit.jupiter.api.Test;
3334
import org.springframework.context.ApplicationEventPublisher;
3435
import org.springframework.modulith.moments.DayHasPassed;
@@ -47,6 +48,8 @@
4748
*/
4849
class MomentsUnitTests {
4950

51+
static final TemporalUnitOffset ACCEPTABLE_OFFSET = within(50, ChronoUnit.MILLIS);
52+
5053
ApplicationEventPublisher events = mock(ApplicationEventPublisher.class);
5154
Clock clock = Clock.systemUTC();
5255

@@ -180,7 +183,20 @@ void returnsInstant() {
180183
var instant = hourly.instant();
181184
var now = hourly.now();
182185

183-
assertThat(LocalDateTime.ofInstant(instant, ZoneOffset.UTC)).isCloseTo(now, within(50, ChronoUnit.MILLIS));
186+
assertThat(LocalDateTime.ofInstant(instant, ZoneOffset.UTC)).isCloseTo(now, ACCEPTABLE_OFFSET);
187+
}
188+
189+
@Test // GH-1490
190+
void resettingWipesShift() {
191+
192+
var now = hourly.instant();
193+
var afterShift = hourly.shiftBy(Duration.ofMinutes(1)).instant();
194+
195+
assertThat(now.plus(Duration.ofMinutes(1))).isCloseTo(afterShift, ACCEPTABLE_OFFSET);
196+
197+
var afterReset = hourly.reset().instant();
198+
199+
assertThat(afterReset).isCloseTo(now, ACCEPTABLE_OFFSET);
184200
}
185201

186202
private Duration getNumberOfDaysForThreeMonth(LocalDate date) {

0 commit comments

Comments
 (0)