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
AnExecutor
implementation 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 TestExecutor
advanceTimeBy(long time, java.util.concurrent.TimeUnit unit)
Advance the internal clock time bytime
in the specifiedunit
s, executing scheduled tasks whose time has come.TestExecutor
advanceTimeByNoExecuteTasks(long time, java.util.concurrent.TimeUnit unit)
Advance the internal clock time bytime
in the specifiedunit
s, without executing scheduled tasks.Completable
closeAsync()
Used to close/shutdown a resource.long
currentMillis()
Returns the internal clock time in milliseconds.long
currentNanos()
Returns the internal clock time in nanoseconds.long
currentTime(java.util.concurrent.TimeUnit unit)
Returns the internal clock time in the specifiedunit
.Cancellable
execute(java.lang.Runnable task)
Executes the passedtask
as soon as possible.TestExecutor
executeNextScheduledTask()
Execute the next scheduled (schedule
/timer
methods) task whose time has come.TestExecutor
executeNextTask()
Execute the next queued (execute
/submit
methods) task.TestExecutor
executeScheduledTasks()
Execute all scheduled (schedule
/timer
methods) tasks whose time has come.TestExecutor
executeTasks()
Execute all queued (execute
/submit
methods) tasks.Completable
onClose()
Returns aCompletable
that is notified once theListenableAsyncCloseable
was closed.int
queuedTasksExecuted()
Returns the number of queued (execute
/submit
methods) tasks that have been executed.int
queuedTasksPending()
Returns the number of queued (execute
/submit
methods) tasks currently pending.Cancellable
schedule(java.lang.Runnable task, long delay, java.util.concurrent.TimeUnit unit)
Executes the passedtask
afterdelay
amount ofunit
s time has passed.int
scheduledTasksExecuted()
Returns the number of scheduled (schedule
/timer
methods) tasks that have been executed.int
scheduledTasksPending()
Returns the number of scheduled (schedule
/timer
methods) 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:Executor
Executes the passedtask
as soon as possible.- Specified by:
execute
in interfaceExecutor
- 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 passedtask
afterdelay
amount ofunit
s 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 interfaceExecutor
- Parameters:
task
- to execute.delay
- The time duration that is allowed to elapse beforetask
is executed.unit
- The units fordelay
.- Returns:
Cancellable
to 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:ListenableAsyncCloseable
Returns aCompletable
that is notified once theListenableAsyncCloseable
was closed.- Specified by:
onClose
in interfaceListenableAsyncCloseable
- Returns:
- the
Completable
that is notified on close.
-
closeAsync
public Completable closeAsync()
Description copied from interface:AsyncCloseable
Used to close/shutdown a resource.- Specified by:
closeAsync
in interfaceAsyncCloseable
- Returns:
- A
Completable
that 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 bytime
in the specifiedunit
s, executing scheduled tasks whose time has come.Queued tasks (
execute
/submit
methods as opposed toschedule
/timer
methods) 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 bytime
in the specifiedunit
s, without executing scheduled tasks.- Parameters:
time
- the duration to advance byunit
- The units fortime
.- 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.
-
-