Class ManualClock

java.lang.Object
io.krabka.streams.coordination.ManualClock
All Implemented Interfaces:
Clock

public final class ManualClock extends Object implements Clock
A clock that a caller moves by hand.

The lease rules take an instant as a parameter, so a test drives a whole failover with no sleep. The clock is safe for concurrent use, so a test shares one clock between a holder and its challengers and moves them all together.

advance(Duration) saturates at Long.MAX_VALUE, so a large step does not wrap the instant it produces.

Example

ManualClock clock = new ManualClock(1_700_000_000_000L);
assertThat(Succession.evaluate(state, standby, clock.nowMillis(), config).action())
    .isEqualTo(Decision.Action.WAIT);
clock.advance(Duration.ofSeconds(30));
assertThat(Succession.evaluate(state, standby, clock.nowMillis(), config).action())
    .isEqualTo(Decision.Action.CHALLENGE);
  • Constructor Details

    • ManualClock

      public ManualClock(long nowMillis)
      Creates a clock that reads one instant.
      Parameters:
      nowMillis - the instant the clock starts at, in milliseconds since the Unix epoch
  • Method Details

    • nowMillis

      public long nowMillis()
      Returns the instant the clock reads now.
      Specified by:
      nowMillis in interface Clock
      Returns:
      the milliseconds since the Unix epoch
    • set

      public void set(long instantMillis)
      Moves the clock to one instant.

      The clock moves backwards too, so a test reproduces a clock step.

      Parameters:
      instantMillis - the instant to read from now on, in milliseconds since the Unix epoch
    • advance

      public void advance(Duration step)
      Moves the clock forward by one extent.
      Parameters:
      step - the extent to add
      Throws:
      NullPointerException - if the step is null