Make it possible to instantiate TestTimer at a given Instant
@@ -4,6 +4,8 @@ package com.yahoo.jdisc.test;
import com.yahoo.jdisc.Timer;
import java.time.Duration;
+import java.time.Instant;
+import java.util.Objects;
/**
* A {@link Timer} to be used in tests when the advancement of time needs to be controlled.
@@ -11,21 +13,34 @@ import java.time.Duration;
* @author hakonhall
*/
public class TestTimer implements Timer {
- private Duration durationSinceEpoch = Duration.ZERO;
+ private Instant instant;
+
+ public TestTimer() {
+ this(Instant.EPOCH);
+ }
+
+ public TestTimer(Instant instant) {
+ this.instant = Objects.requireNonNull(instant);
+ }
public void setMillis(long millisSinceEpoch) {
- durationSinceEpoch = Duration.ofMillis(millisSinceEpoch);
+ instant = Instant.ofEpochMilli(millisSinceEpoch);
}
public void advanceMillis(long millis) { advance(Duration.ofMillis(millis)); }
public void advanceSeconds(long seconds) { advance(Duration.ofSeconds(seconds)); }
public void advanceMinutes(long minutes) { advance(Duration.ofMinutes(minutes)); }
public void advance(Duration duration) {
- durationSinceEpoch = durationSinceEpoch.plus(duration);
+ instant = instant.plus(duration);
+ }
+
+ @Override
+ public Instant currentTime() {
+ return instant;
}
@Override
public long currentTimeMillis() {
- return durationSinceEpoch.toMillis();
+ return instant.toEpochMilli();
}
}
