Class VirtualTimeScheduler

  • All Implemented Interfaces:
    java.util.concurrent.Executor, java.util.concurrent.ExecutorService, java.util.concurrent.ScheduledExecutorService

    public class VirtualTimeScheduler
    extends java.lang.Object
    implements java.util.concurrent.ScheduledExecutorService
    VirtualTimeScheduler is a ScheduledExecutorService that uses a virtual notion of time. Actions are queued and can be inspected. Time can be advanced on-demand and metadata on how many actions are queued and have been executed can be retrieved.

    This scheduler is ideally suited to use in tests as it avoids having to have tests sleep which would make the tests inherently flaky.

    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      long advanceBy​(long intervalNanos)
      advanceBy will run all actions scheduled within the interval specified from the current time.
      long advanceBy​(long interval, java.util.concurrent.TimeUnit unit)
      advanceBy will run all actions scheduled within the interval (converted to nanos) specified from the current time.
      long advanceTo​(long endTimeNanos)
      advanceTo will run all actions scheduled between CurrentTimeNanos and endTimeNanos.
      boolean awaitTermination​(long timeout, java.util.concurrent.TimeUnit unit)  
      void execute​(java.lang.Runnable command)  
      long getActionsExecuted()  
      long getActionsQueued()  
      long getCurrentTimeNanos()  
      long getFurthestScheduledTimeNanos()  
      java.util.PriorityQueue<VirtualTimeFuture<?>> getQueue()  
      <T> java.util.List<java.util.concurrent.Future<T>> invokeAll​(java.util.Collection<? extends java.util.concurrent.Callable<T>> tasks)  
      <T> java.util.List<java.util.concurrent.Future<T>> invokeAll​(java.util.Collection<? extends java.util.concurrent.Callable<T>> tasks, long timeout, java.util.concurrent.TimeUnit unit)  
      <T> T invokeAny​(java.util.Collection<? extends java.util.concurrent.Callable<T>> tasks)  
      <T> T invokeAny​(java.util.Collection<? extends java.util.concurrent.Callable<T>> tasks, long timeout, java.util.concurrent.TimeUnit unit)  
      boolean isShutdown()  
      boolean isTerminated()  
      java.util.concurrent.ScheduledFuture<?> schedule​(java.lang.Runnable command, long delay, java.util.concurrent.TimeUnit unit)  
      <V> java.util.concurrent.ScheduledFuture<V> schedule​(java.util.concurrent.Callable<V> callable, long delay, java.util.concurrent.TimeUnit unit)  
      java.util.concurrent.ScheduledFuture<?> scheduleAtFixedRate​(java.lang.Runnable command, long initialDelay, long period, java.util.concurrent.TimeUnit unit)  
      java.util.concurrent.ScheduledFuture<?> scheduleWithFixedDelay​(java.lang.Runnable command, long initialDelay, long delay, java.util.concurrent.TimeUnit unit)  
      void shutdown()  
      java.util.List<java.lang.Runnable> shutdownNow()  
      java.util.concurrent.Future<?> submit​(java.lang.Runnable task)  
      <T> java.util.concurrent.Future<T> submit​(java.lang.Runnable task, T result)  
      <T> java.util.concurrent.Future<T> submit​(java.util.concurrent.Callable<T> task)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • VirtualTimeScheduler

        public VirtualTimeScheduler()
    • Method Detail

      • schedule

        public java.util.concurrent.ScheduledFuture<?> schedule​(java.lang.Runnable command,
                                                                long delay,
                                                                java.util.concurrent.TimeUnit unit)
        Specified by:
        schedule in interface java.util.concurrent.ScheduledExecutorService
      • schedule

        public <V> java.util.concurrent.ScheduledFuture<V> schedule​(java.util.concurrent.Callable<V> callable,
                                                                    long delay,
                                                                    java.util.concurrent.TimeUnit unit)
        Specified by:
        schedule in interface java.util.concurrent.ScheduledExecutorService
      • scheduleAtFixedRate

        public java.util.concurrent.ScheduledFuture<?> scheduleAtFixedRate​(java.lang.Runnable command,
                                                                           long initialDelay,
                                                                           long period,
                                                                           java.util.concurrent.TimeUnit unit)
        Specified by:
        scheduleAtFixedRate in interface java.util.concurrent.ScheduledExecutorService
      • scheduleWithFixedDelay

        public java.util.concurrent.ScheduledFuture<?> scheduleWithFixedDelay​(java.lang.Runnable command,
                                                                              long initialDelay,
                                                                              long delay,
                                                                              java.util.concurrent.TimeUnit unit)
        Specified by:
        scheduleWithFixedDelay in interface java.util.concurrent.ScheduledExecutorService
      • shutdown

        public void shutdown()
        Specified by:
        shutdown in interface java.util.concurrent.ExecutorService
      • shutdownNow

        public java.util.List<java.lang.Runnable> shutdownNow()
        Specified by:
        shutdownNow in interface java.util.concurrent.ExecutorService
      • isShutdown

        public boolean isShutdown()
        Specified by:
        isShutdown in interface java.util.concurrent.ExecutorService
      • isTerminated

        public boolean isTerminated()
        Specified by:
        isTerminated in interface java.util.concurrent.ExecutorService
      • awaitTermination

        public boolean awaitTermination​(long timeout,
                                        java.util.concurrent.TimeUnit unit)
                                 throws java.lang.InterruptedException
        Specified by:
        awaitTermination in interface java.util.concurrent.ExecutorService
        Throws:
        java.lang.InterruptedException
      • submit

        public <T> java.util.concurrent.Future<T> submit​(java.util.concurrent.Callable<T> task)
        Specified by:
        submit in interface java.util.concurrent.ExecutorService
      • submit

        public <T> java.util.concurrent.Future<T> submit​(java.lang.Runnable task,
                                                         T result)
        Specified by:
        submit in interface java.util.concurrent.ExecutorService
      • submit

        public java.util.concurrent.Future<?> submit​(java.lang.Runnable task)
        Specified by:
        submit in interface java.util.concurrent.ExecutorService
      • invokeAll

        public <T> java.util.List<java.util.concurrent.Future<T>> invokeAll​(java.util.Collection<? extends java.util.concurrent.Callable<T>> tasks)
                                                                     throws java.lang.InterruptedException
        Specified by:
        invokeAll in interface java.util.concurrent.ExecutorService
        Throws:
        java.lang.InterruptedException
      • invokeAll

        public <T> java.util.List<java.util.concurrent.Future<T>> invokeAll​(java.util.Collection<? extends java.util.concurrent.Callable<T>> tasks,
                                                                            long timeout,
                                                                            java.util.concurrent.TimeUnit unit)
                                                                     throws java.lang.InterruptedException
        Specified by:
        invokeAll in interface java.util.concurrent.ExecutorService
        Throws:
        java.lang.InterruptedException
      • invokeAny

        public <T> T invokeAny​(java.util.Collection<? extends java.util.concurrent.Callable<T>> tasks)
                        throws java.lang.InterruptedException,
                               java.util.concurrent.ExecutionException
        Specified by:
        invokeAny in interface java.util.concurrent.ExecutorService
        Throws:
        java.lang.InterruptedException
        java.util.concurrent.ExecutionException
      • invokeAny

        public <T> T invokeAny​(java.util.Collection<? extends java.util.concurrent.Callable<T>> tasks,
                               long timeout,
                               java.util.concurrent.TimeUnit unit)
                        throws java.lang.InterruptedException,
                               java.util.concurrent.ExecutionException,
                               java.util.concurrent.TimeoutException
        Specified by:
        invokeAny in interface java.util.concurrent.ExecutorService
        Throws:
        java.lang.InterruptedException
        java.util.concurrent.ExecutionException
        java.util.concurrent.TimeoutException
      • execute

        public void execute​(java.lang.Runnable command)
        Specified by:
        execute in interface java.util.concurrent.Executor
      • advanceBy

        public long advanceBy​(long interval,
                              java.util.concurrent.TimeUnit unit)
        advanceBy will run all actions scheduled within the interval (converted to nanos) specified from the current time.
        Parameters:
        interval - time in unit to advance.
        unit - for interval to advance.
        Returns:
        amount of actions executed.
      • advanceBy

        public long advanceBy​(long intervalNanos)
        advanceBy will run all actions scheduled within the interval specified from the current time.
        Parameters:
        intervalNanos - time in nano seconds to advance.
        Returns:
        amount of actions executed.
      • advanceTo

        public long advanceTo​(long endTimeNanos)
        advanceTo will run all actions scheduled between CurrentTimeNanos and endTimeNanos.
        Parameters:
        endTimeNanos - time in nano seconds to advance to.
        Returns:
        amount of actions executed.
      • getActionsExecuted

        public long getActionsExecuted()
        Returns:
        the total number of actions executed by this scheduler.
      • getActionsQueued

        public long getActionsQueued()
        Returns:
        the number of actions currently queued on this scheduler.
      • getFurthestScheduledTimeNanos

        public long getFurthestScheduledTimeNanos()
        Returns:
        the furthest time an action is/was scheduled at within this scheduler.
      • getCurrentTimeNanos

        public long getCurrentTimeNanos()
        Returns:
        the current virtual time of this scheduler.
      • getQueue

        public java.util.PriorityQueue<VirtualTimeFuture<?>> getQueue()
        Returns:
        a copy of the PriorityQueue of queued futures.