Package pl.amazingcode.timeflow
Class TestTime
- java.lang.Object
-
- pl.amazingcode.timeflow.Time
-
- pl.amazingcode.timeflow.TestTime
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclearObservers()Remove all registered observers.Clockclock()Returns the clock.voidfastBackward(Duration duration)Jump the clock backward by the given duration.voidfastForward(Duration duration)Jump the clock forward by the given duration.Instantnow()Returns the current instant using the clock.voidregisterObserver(Consumer<Clock> clockConsumer)Register an observer that will be notified every time the clock is changed byfastForward(Duration),fastBackward(Duration)ortimeFlow(Duration, Instant, int).voidresetClock()voidsetClock(Clock clock)static TestTimetestInstance()Returns the singleton instance of TestTime.voidtimeFlow(Duration step, Instant endTime, int flowSpeedMillis)Simulate time flow by jumping the clock forward by the given step every flowSpeedMillis.
-
-
-
Method Detail
-
testInstance
public static TestTime testInstance()
Returns the singleton instance of TestTime.- Returns:
- the singleton instance of TestTime
-
now
public Instant now()
Returns the current instant using the clock.
-
clock
public Clock clock()
Returns the clock. Inner clock is instantiated withClock.systemUTC().
-
setClock
public void setClock(Clock clock)
Replace the clock used byTimeandTestTimewith a custom one.// Prepare a fixed clockZoneId zoneId = TimeZone.getTimeZone("Europe/Warsaw").toZoneId();ZonedDateTime dateTime = LocalDateTime.of(2001, 11, 23, 12, 15).atZone(zoneId);Clock fixedClock = Clock.fixed(dateTime.toInstant(), zoneId);// Set the fixed clock as the clock used by Time in the production codeTestTime.testInstance().setClock(fixedClock);
-
resetClock
public void resetClock()
-
fastForward
public void fastForward(Duration duration)
Jump the clock forward by the given duration.- Parameters:
duration- the duration to jump forward
-
fastBackward
public void fastBackward(Duration duration)
Jump the clock backward by the given duration.- Parameters:
duration- the duration to jump backward
-
timeFlow
public void timeFlow(Duration step, Instant endTime, int flowSpeedMillis)
Simulate time flow by jumping the clock forward by the given step every flowSpeedMillis. This will replace the clock used byTimein the production code.// Prepare a fixed clockZoneId zoneId = TimeZone.getTimeZone("Europe/Warsaw").toZoneId();ZonedDateTime dateTime = LocalDateTime.of(2001, 11, 23, 12, 15).atZone(zoneId);Clock fixedClock = Clock.fixed(dateTime.toInstant(), zoneId);// Set the fixed clock as the clock used by Time in the production codeTestTime.testInstance().setClock(fixedClock);// Prepare time flow parametersDuration step = Duration.of(1, ChronoUnit.MINUTES);Instant endTime = fixedClock.instant().plus(10, ChronoUnit.MINUTES);int flowSpeedMillis = 100;// Simulate time flowTestTime.testInstance().timeFlow(step, endTime, flowSpeedMillis);- Parameters:
step- the duration to jump forwardendTime- the time when the simulated time flow should stopflowSpeedMillis- the speed of the simulated time flow in milliseconds- Throws:
IllegalArgumentException- if flowSpeedMillis is not positive or endTime is before the current timeRuntimeException- if interrupted while sleeping between jumps
-
registerObserver
public void registerObserver(Consumer<Clock> clockConsumer)
Register an observer that will be notified every time the clock is changed byfastForward(Duration),fastBackward(Duration)ortimeFlow(Duration, Instant, int).TestTime.testInstance().registerObserver(clock -> System.out.println(clock.instant()));- Parameters:
clockConsumer- the observer
-
clearObservers
public void clearObservers()
Remove all registered observers.
-
-