Interface Ticker

All Known Subinterfaces:
MockTicker

public interface Ticker
A nanosecond-based time source, e.g. System.nanoTime().
  • Method Summary

    Modifier and Type
    Method
    Description
    long
    The initial value used for delay and computations based upon a monotonic time source.
    long
    The time elapsed since initialization of this class in nanoseconds.
    static MockTicker
    Returns a newly created mock Ticker that allows the caller control the flow of time.
    void
    sleep(long delay, TimeUnit unit)
    Waits until the given amount of time goes by.
    default void
    sleepMillis(long delayMillis)
    Waits until the given amount of time goes by.
    static Ticker
    Returns the singleton Ticker that returns the values from the real system clock source.
  • Method Details

    • systemTicker

      static Ticker systemTicker()
      Returns the singleton Ticker that returns the values from the real system clock source. However, note that this is not the same as System.nanoTime() because we apply a fixed offset to the nanoTime.
    • newMockTicker

      static MockTicker newMockTicker()
      Returns a newly created mock Ticker that allows the caller control the flow of time. This can be useful when you test time-sensitive logic without waiting for too long or introducing flakiness due to non-deterministic nature of system clock.
    • initialNanoTime

      long initialNanoTime()
      The initial value used for delay and computations based upon a monotonic time source.
      Returns:
      initial value used for delay and computations based upon a monotonic time source.
    • nanoTime

      long nanoTime()
      The time elapsed since initialization of this class in nanoseconds. This may return a negative number just like System.nanoTime().
    • sleep

      void sleep(long delay, TimeUnit unit) throws InterruptedException
      Waits until the given amount of time goes by.
      Parameters:
      delay - the amount of delay.
      unit - the TimeUnit of delay.
      Throws:
      InterruptedException
      See Also:
    • sleepMillis

      default void sleepMillis(long delayMillis) throws InterruptedException
      Waits until the given amount of time goes by.
      Parameters:
      delayMillis - the number of milliseconds.
      Throws:
      InterruptedException
      See Also: