Skip to content

Commit b95cafe

Browse files
committed
refine tests
1 parent da2d877 commit b95cafe

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

src/test/java/com/uid2/operator/OperatorShutdownHandlerTest.java

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,13 @@ public class OperatorShutdownHandlerTest {
3434
@Mock private Clock clock;
3535
@Mock private ShutdownService shutdownService;
3636
private OperatorShutdownHandler operatorShutdownHandler;
37-
38-
// Fixed base time for predictable testing
39-
private Instant baseTime;
4037

4138

4239

4340
@BeforeEach
4441
void beforeEach() {
4542
mocks = MockitoAnnotations.openMocks(this);
46-
baseTime = Instant.now(); // Capture a fixed base time
47-
when(clock.instant()).thenReturn(baseTime); // Use thenReturn for fixed value
43+
when(clock.instant()).thenAnswer(i -> Instant.now());
4844
doThrow(new RuntimeException()).when(shutdownService).Shutdown(1);
4945
this.operatorShutdownHandler = new OperatorShutdownHandler(Duration.ofHours(12), Duration.ofHours(12), Duration.ofHours(12), clock, shutdownService);
5046
}
@@ -173,13 +169,13 @@ void saltsLogErrorAtInterval(VertxTestContext testContext) {
173169
void storeRefreshRecordsSuccessTimestamp(VertxTestContext testContext) {
174170
this.operatorShutdownHandler.handleStoreRefresh("test_store", true);
175171

176-
when(clock.instant()).thenReturn(baseTime.plus(11, ChronoUnit.HOURS));
172+
when(clock.instant()).thenAnswer(i -> Instant.now().plus(11, ChronoUnit.HOURS));
177173
assertDoesNotThrow(() -> {
178174
this.operatorShutdownHandler.checkStoreRefreshStaleness();
179175
});
180176
verify(shutdownService, never()).Shutdown(anyInt());
181177

182-
when(clock.instant()).thenReturn(baseTime.plus(13, ChronoUnit.HOURS));
178+
when(clock.instant()).thenAnswer(i -> Instant.now().plus(13, ChronoUnit.HOURS));
183179
try {
184180
this.operatorShutdownHandler.checkStoreRefreshStaleness();
185181
} catch (RuntimeException e) {
@@ -192,14 +188,14 @@ void storeRefreshRecordsSuccessTimestamp(VertxTestContext testContext) {
192188
void storeRefreshFailureDoesNotResetTimestamp(VertxTestContext testContext) {
193189
this.operatorShutdownHandler.handleStoreRefresh("test_store", true);
194190

195-
when(clock.instant()).thenReturn(baseTime.plus(2, ChronoUnit.HOURS));
191+
when(clock.instant()).thenAnswer(i -> Instant.now().plus(2, ChronoUnit.HOURS));
196192

197193
this.operatorShutdownHandler.handleStoreRefresh("test_store", false);
198194
this.operatorShutdownHandler.handleStoreRefresh("test_store", false);
199195
this.operatorShutdownHandler.handleStoreRefresh("test_store", false);
200196

201197

202-
when(clock.instant()).thenReturn(baseTime.plus(13, ChronoUnit.HOURS));
198+
when(clock.instant()).thenAnswer(i -> Instant.now().plus(13, ChronoUnit.HOURS));
203199

204200
try {
205201
this.operatorShutdownHandler.checkStoreRefreshStaleness();
@@ -217,7 +213,7 @@ void storeRefreshStaleShutdown(VertxTestContext testContext) {
217213

218214
this.operatorShutdownHandler.handleStoreRefresh("test_store", true);
219215

220-
when(clock.instant()).thenReturn(baseTime.plus(12, ChronoUnit.HOURS).plusSeconds(1));
216+
when(clock.instant()).thenAnswer(i -> Instant.now().plus(12, ChronoUnit.HOURS).plusSeconds(1));
221217

222218
try {
223219
this.operatorShutdownHandler.checkStoreRefreshStaleness();
@@ -234,11 +230,11 @@ void storeRefreshStaleShutdown(VertxTestContext testContext) {
234230
void storeRefreshRecoverBeforeStale(VertxTestContext testContext) {
235231
this.operatorShutdownHandler.handleStoreRefresh("test_store", true);
236232

237-
when(clock.instant()).thenReturn(baseTime.plus(11, ChronoUnit.HOURS));
233+
when(clock.instant()).thenAnswer(i -> Instant.now().plus(11, ChronoUnit.HOURS));
238234

239235
this.operatorShutdownHandler.handleStoreRefresh("test_store", true);
240236

241-
when(clock.instant()).thenReturn(baseTime.plus(12, ChronoUnit.HOURS));
237+
when(clock.instant()).thenAnswer(i -> Instant.now().plus(12, ChronoUnit.HOURS));
242238

243239
assertDoesNotThrow(() -> {
244240
this.operatorShutdownHandler.checkStoreRefreshStaleness();
@@ -258,12 +254,12 @@ void multipleStoresOneStaleTriggers(VertxTestContext testContext) {
258254
this.operatorShutdownHandler.handleStoreRefresh("store2", true);
259255
this.operatorShutdownHandler.handleStoreRefresh("store3", true);
260256

261-
when(clock.instant()).thenReturn(baseTime.plus(6, ChronoUnit.HOURS));
257+
when(clock.instant()).thenAnswer(i -> Instant.now().plus(6, ChronoUnit.HOURS));
262258

263259
this.operatorShutdownHandler.handleStoreRefresh("store1", true);
264260
this.operatorShutdownHandler.handleStoreRefresh("store2", true);
265261

266-
when(clock.instant()).thenReturn(baseTime.plus(12, ChronoUnit.HOURS).plusSeconds(1));
262+
when(clock.instant()).thenAnswer(i -> Instant.now().plus(12, ChronoUnit.HOURS).plusSeconds(1));
267263

268264
try {
269265
this.operatorShutdownHandler.checkStoreRefreshStaleness();

0 commit comments

Comments
 (0)