Class TimeService

java.lang.Object
org.mockserver.time.TimeService

public class TimeService extends Object
  • Field Details

    • FIXED_INSTANT_FOR_TESTS

      public static final Instant FIXED_INSTANT_FOR_TESTS
  • Constructor Details

    • TimeService

      public TimeService()
  • Method Details

    • now

      public static Instant now()
      Returns the current instant: the controllable-clock frozen instant if set, then FIXED_INSTANT_FOR_TESTS if a test has pinned time (per-thread or globally), otherwise the real wall-clock time.
    • currentTimeMillis

      public static long currentTimeMillis()
      Returns current time in epoch milliseconds, consistent with now().
    • offsetNow

      public static OffsetDateTime offsetNow()
    • freeze

      public static void freeze(Instant instant)
      Freeze the clock at the given instant. If instant is null, freezes at the current real time. Part of the controllable-clock API used by chaos time-outage features; JVM-global.
    • advance

      public static void advance(Duration duration)
      Advance the frozen clock by the given duration. If the clock is not currently frozen, it is first frozen at the current real time (or the test-fixed instant if a test has pinned time), then advanced.
    • reset

      public static void reset()
      Reset the controllable clock to real time (unfrozen). Does not affect test-only time pinning.
    • isFrozen

      public static boolean isFrozen()
      Returns true if the clock is currently frozen, either via the controllable clock (freeze(Instant)) or because a test has pinned time.
    • fixedTime

      public static void fixedTime(boolean fixed)
      Test-only: pin (or unpin) now() to FIXED_INSTANT_FOR_TESTS for the CURRENT thread only, so parallel test classes do not observe each other's fixed clock. Prefer the org.mockserver.time.FixedTime JUnit rule, which guarantees the reset.
    • fixedTimeGlobally

      public static void fixedTimeGlobally(boolean fixed)
      Test-only: pin (or unpin) now() for ALL threads. JVM-global and therefore NOT parallel-safe - use only from tests in the sequential Surefire phase. Prefer the org.mockserver.time.GlobalFixedTime JUnit rule.
    • fixedTime

      public static boolean fixedTime()