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
    • FIXED_NANOS_FOR_TESTS

      public static final long FIXED_NANOS_FOR_TESTS
      Constant monotonic value returned by nanoTime() when a test has pinned time. Any constant works; both the start and end reads in a duration measurement return it, so the delta is deterministically 0.
  • 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.
    • nanoTime

      public static long nanoTime()
      Returns a monotonic nanosecond timestamp for measuring elapsed time, mirroring System.nanoTime(). In production (no test pin) this returns the real System.nanoTime() - monotonic semantics and overhead are unchanged (the fast path is a single volatile read of fixedActiveCount == 0). When a test has pinned time (per-thread via org.mockserver.time.FixedTime or globally via GlobalFixedTime), it returns the constant FIXED_NANOS_FOR_TESTS, so both the start and end reads of a duration measurement are equal and the elapsed delta is deterministically 0. Unlike now(), this is NOT affected by the controllable freeze(Instant) clock, since elapsed-time measurements must remain monotonic.
    • 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()