public class TestExecutor extends java.lang.Object implements Executor
Executor
implementation that provides methods for controlling execution of queued and schedules tasks,
for testing.Constructor and Description |
---|
TestExecutor()
Create a new instance.
|
Modifier and Type | Method and Description |
---|---|
TestExecutor |
advanceTimeBy(long time,
java.util.concurrent.TimeUnit unit)
Advance the internal clock time by
time in the specified unit s, executing scheduled tasks
whose time has come. |
TestExecutor |
advanceTimeByNoExecuteTasks(long time,
java.util.concurrent.TimeUnit unit)
Advance the internal clock time by
time in the specified unit 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 specified
unit . |
Cancellable |
execute(java.lang.Runnable task)
Executes the passed
task 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 a
Completable that is notified once the ListenableAsyncCloseable 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 passed
task after delay amount of unit 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. |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
submit, submit, submitCallable, submitRunnable, timer, timer
closeAsyncGracefully
public Cancellable execute(java.lang.Runnable task) throws java.util.concurrent.RejectedExecutionException
Executor
task
as soon as possible.execute
in interface Executor
task
- to execute.Cancellable
to cancel the task if not yet executed.java.util.concurrent.RejectedExecutionException
- If the task is rejected.public Cancellable schedule(java.lang.Runnable task, long delay, java.util.concurrent.TimeUnit unit) throws java.util.concurrent.RejectedExecutionException
Executor
task
after delay
amount of unit
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.
schedule
in interface Executor
task
- to execute.delay
- The time duration that is allowed to elapse before task
is executed.unit
- The units for delay
.Cancellable
to cancel the task if not yet executed.java.util.concurrent.RejectedExecutionException
- If the task is rejected.public Completable onClose()
ListenableAsyncCloseable
Completable
that is notified once the ListenableAsyncCloseable
was closed.onClose
in interface ListenableAsyncCloseable
Completable
that is notified on close.public Completable closeAsync()
AsyncCloseable
closeAsync
in interface AsyncCloseable
Completable
that is notified once the close is complete.public long currentNanos()
public long currentMillis()
public long currentTime(java.util.concurrent.TimeUnit unit)
unit
.unit
- the time unit to calculateunit
.public TestExecutor advanceTimeBy(long time, java.util.concurrent.TimeUnit unit)
time
in the specified unit
s, executing scheduled tasks
whose time has come.
Queued tasks (execute
/submit
methods as opposed to schedule
/timer
methods) are
not executed.
time
- the duration to advance byunit
- The units for time
.public TestExecutor advanceTimeByNoExecuteTasks(long time, java.util.concurrent.TimeUnit unit)
time
in the specified unit
s, without executing
scheduled tasks.time
- the duration to advance byunit
- The units for time
.public TestExecutor executeTasks()
execute
/submit
methods) tasks. Any exceptions thrown by tasks will
propagate, preventing execution of any further tasks.public TestExecutor executeNextTask()
execute
/submit
methods) task. Any exceptions thrown by the task will
propagate.public TestExecutor executeScheduledTasks()
schedule
/timer
methods) tasks whose time has come. Any exceptions thrown
by tasks will propagate, preventing execution of any further tasks.public TestExecutor executeNextScheduledTask()
schedule
/timer
methods) task whose time has come. Any exceptions
thrown by the task will propagate.public int queuedTasksPending()
execute
/submit
methods) tasks currently pending.execute
/submit
methods) tasks currently pending.public int scheduledTasksPending()
schedule
/timer
methods) tasks currently pending.schedule
/timer
methods) tasks currently pending.public int queuedTasksExecuted()
execute
/submit
methods) tasks that have been executed.execute
/submit
methods) tasks that have been executed.public int scheduledTasksExecuted()
schedule
/timer
methods) tasks that have been executed.schedule
/timer
methods) tasks that have been executed.