Class TestExecutor

    • Constructor Detail

      • TestExecutor

        public TestExecutor()
        Create a new instance.
    • Method Detail

      • execute

        public Cancellable execute​(java.lang.Runnable task)
                            throws java.util.concurrent.RejectedExecutionException
        Description copied from interface: Executor
        Executes the passed task as soon as possible.
        Specified by:
        execute in interface Executor
        Parameters:
        task - to execute.
        Returns:
        Cancellable to cancel the task if not yet executed.
        Throws:
        java.util.concurrent.RejectedExecutionException - If the task is rejected.
      • schedule

        public Cancellable schedule​(java.lang.Runnable task,
                                    long delay,
                                    java.util.concurrent.TimeUnit unit)
                             throws java.util.concurrent.RejectedExecutionException
        Description copied from interface: Executor
        Executes the passed task after delay amount of units time has passed.

        Note this method is not guaranteed to provide real time execution. For example implementations are free to consolidate tasks into time buckets to reduce the overhead of timer management at the cost of reduced timer fidelity.

        Specified by:
        schedule in interface Executor
        Parameters:
        task - to execute.
        delay - The time duration that is allowed to elapse before task is executed.
        unit - The units for delay.
        Returns:
        Cancellable to cancel the task if not yet executed.
        Throws:
        java.util.concurrent.RejectedExecutionException - If the task is rejected.
      • currentNanos

        public long currentNanos()
        Returns the internal clock time in nanoseconds.
        Returns:
        the internal clock time in nanoseconds.
      • currentMillis

        public long currentMillis()
        Returns the internal clock time in milliseconds.
        Returns:
        the internal clock time in milliseconds.
      • currentTime

        public long currentTime​(java.util.concurrent.TimeUnit unit)
        Returns the internal clock time in the specified unit.
        Parameters:
        unit - the time unit to calculate
        Returns:
        the internal clock time in the specified unit.
      • advanceTimeBy

        public TestExecutor advanceTimeBy​(long time,
                                          java.util.concurrent.TimeUnit unit)
        Advance the internal clock time by time in the specified units, executing scheduled tasks whose time has come.

        Queued tasks (execute/submit methods as opposed to schedule/timer methods) are not executed.

        Parameters:
        time - the duration to advance by
        unit - The units for time.
        Returns:
        this.
      • advanceTimeByNoExecuteTasks

        public TestExecutor advanceTimeByNoExecuteTasks​(long time,
                                                        java.util.concurrent.TimeUnit unit)
        Advance the internal clock time by time in the specified units, without executing scheduled tasks.
        Parameters:
        time - the duration to advance by
        unit - The units for time.
        Returns:
        this.
      • executeTasks

        public TestExecutor executeTasks()
        Execute all queued (execute/submit methods) tasks. Any exceptions thrown by tasks will propagate, preventing execution of any further tasks.
        Returns:
        this.
      • executeNextTask

        public TestExecutor executeNextTask()
        Execute the next queued (execute/submit methods) task. Any exceptions thrown by the task will propagate.
        Returns:
        this.
      • executeScheduledTasks

        public TestExecutor executeScheduledTasks()
        Execute all scheduled (schedule/timer methods) tasks whose time has come. Any exceptions thrown by tasks will propagate, preventing execution of any further tasks.
        Returns:
        this.
      • executeNextScheduledTask

        public TestExecutor executeNextScheduledTask()
        Execute the next scheduled (schedule/timer methods) task whose time has come. Any exceptions thrown by the task will propagate.
        Returns:
        this.
      • queuedTasksPending

        public int queuedTasksPending()
        Returns the number of queued (execute/submit methods) tasks currently pending.
        Returns:
        the number of queued (execute/submit methods) tasks currently pending.
      • scheduledTasksPending

        public int scheduledTasksPending()
        Returns the number of scheduled (schedule/timer methods) tasks currently pending.
        Returns:
        the number of scheduled (schedule/timer methods) tasks currently pending.
      • queuedTasksExecuted

        public int queuedTasksExecuted()
        Returns the number of queued (execute/submit methods) tasks that have been executed.
        Returns:
        the number of queued (execute/submit methods) tasks that have been executed.
      • scheduledTasksExecuted

        public int scheduledTasksExecuted()
        Returns the number of scheduled (schedule/timer methods) tasks that have been executed.
        Returns:
        the number of scheduled (schedule/timer methods) tasks that have been executed.