Package io.servicetalk.concurrent.api
Class TestExecutor
- java.lang.Object
-
- io.servicetalk.concurrent.api.TestExecutor
-
- All Implemented Interfaces:
AsyncCloseable,Executor,ListenableAsyncCloseable,Executor
public class TestExecutor extends java.lang.Object implements Executor
AnExecutorimplementation that provides methods for controlling execution of queued and schedules tasks, for testing.
-
-
Constructor Summary
Constructors Constructor Description TestExecutor()Create a new instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description TestExecutoradvanceTimeBy(long time, java.util.concurrent.TimeUnit unit)Advance the internal clock time bytimein the specifiedunits, executing scheduled tasks whose time has come.TestExecutoradvanceTimeByNoExecuteTasks(long time, java.util.concurrent.TimeUnit unit)Advance the internal clock time bytimein the specifiedunits, without executing scheduled tasks.CompletablecloseAsync()Used to close/shutdown a resource.longcurrentMillis()Returns the internal clock time in milliseconds.longcurrentNanos()Returns the internal clock time in nanoseconds.longcurrentTime(java.util.concurrent.TimeUnit unit)Returns the internal clock time in the specifiedunit.Cancellableexecute(java.lang.Runnable task)Executes the passedtaskas soon as possible.TestExecutorexecuteNextScheduledTask()Execute the next scheduled (schedule/timermethods) task whose time has come.TestExecutorexecuteNextTask()Execute the next queued (execute/submitmethods) task.TestExecutorexecuteScheduledTasks()Execute all scheduled (schedule/timermethods) tasks whose time has come.TestExecutorexecuteTasks()Execute all queued (execute/submitmethods) tasks.CompletableonClose()Returns aCompletablethat is notified once theListenableAsyncCloseablewas closed.intqueuedTasksExecuted()Returns the number of queued (execute/submitmethods) tasks that have been executed.intqueuedTasksPending()Returns the number of queued (execute/submitmethods) tasks currently pending.Cancellableschedule(java.lang.Runnable task, long delay, java.util.concurrent.TimeUnit unit)Executes the passedtaskafterdelayamount ofunits time has passed.intscheduledTasksExecuted()Returns the number of scheduled (schedule/timermethods) tasks that have been executed.intscheduledTasksPending()Returns the number of scheduled (schedule/timermethods) tasks currently pending.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface io.servicetalk.concurrent.api.AsyncCloseable
closeAsyncGracefully
-
Methods inherited from interface io.servicetalk.concurrent.api.Executor
submit, submit, submitCallable, submitRunnable, timer, timer
-
-
-
-
Method Detail
-
execute
public Cancellable execute(java.lang.Runnable task) throws java.util.concurrent.RejectedExecutionException
Description copied from interface:ExecutorExecutes the passedtaskas soon as possible.- Specified by:
executein interfaceExecutor- Parameters:
task- to execute.- Returns:
Cancellableto 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:ExecutorExecutes the passedtaskafterdelayamount ofunits 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:
schedulein interfaceExecutor- Parameters:
task- to execute.delay- The time duration that is allowed to elapse beforetaskis executed.unit- The units fordelay.- Returns:
Cancellableto cancel the task if not yet executed.- Throws:
java.util.concurrent.RejectedExecutionException- If the task is rejected.
-
onClose
public Completable onClose()
Description copied from interface:ListenableAsyncCloseableReturns aCompletablethat is notified once theListenableAsyncCloseablewas closed.- Specified by:
onClosein interfaceListenableAsyncCloseable- Returns:
- the
Completablethat is notified on close.
-
closeAsync
public Completable closeAsync()
Description copied from interface:AsyncCloseableUsed to close/shutdown a resource.- Specified by:
closeAsyncin interfaceAsyncCloseable- Returns:
- A
Completablethat is notified once the close is complete.
-
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 specifiedunit.- 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 bytimein the specifiedunits, executing scheduled tasks whose time has come.Queued tasks (
execute/submitmethods as opposed toschedule/timermethods) are not executed.- Parameters:
time- the duration to advance byunit- The units fortime.- Returns:
- this.
-
advanceTimeByNoExecuteTasks
public TestExecutor advanceTimeByNoExecuteTasks(long time, java.util.concurrent.TimeUnit unit)
Advance the internal clock time bytimein the specifiedunits, without executing scheduled tasks.- Parameters:
time- the duration to advance byunit- The units fortime.- Returns:
- this.
-
executeTasks
public TestExecutor executeTasks()
Execute all queued (execute/submitmethods) 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/submitmethods) task. Any exceptions thrown by the task will propagate.- Returns:
- this.
-
executeScheduledTasks
public TestExecutor executeScheduledTasks()
Execute all scheduled (schedule/timermethods) 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/timermethods) 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/submitmethods) tasks currently pending.- Returns:
- the number of queued (
execute/submitmethods) tasks currently pending.
-
scheduledTasksPending
public int scheduledTasksPending()
Returns the number of scheduled (schedule/timermethods) tasks currently pending.- Returns:
- the number of scheduled (
schedule/timermethods) tasks currently pending.
-
queuedTasksExecuted
public int queuedTasksExecuted()
Returns the number of queued (execute/submitmethods) tasks that have been executed.- Returns:
- the number of queued (
execute/submitmethods) tasks that have been executed.
-
scheduledTasksExecuted
public int scheduledTasksExecuted()
Returns the number of scheduled (schedule/timermethods) tasks that have been executed.- Returns:
- the number of scheduled (
schedule/timermethods) tasks that have been executed.
-
-