public class ManuallyTriggeredScheduledExecutorService extends Object implements ScheduledExecutorService
ScheduledExecutorService
implementation for testing purposes. It spawns no
threads, but lets you trigger the execution of tasks manually.
This class is helpful when implementing tests tasks synchronous and control when they run, which would otherwise asynchronous and require complex triggers and latches to test.
Modifier and Type | Class and Description |
---|---|
static class |
ManuallyTriggeredScheduledExecutorService.RunnableCaller<T>
|
Constructor and Description |
---|
ManuallyTriggeredScheduledExecutorService() |
Modifier and Type | Method and Description |
---|---|
boolean |
awaitTermination(long timeout,
TimeUnit unit) |
void |
execute(Runnable command) |
Collection<ScheduledFuture<?>> |
getActiveNonPeriodicScheduledTask() |
Collection<ScheduledFuture<?>> |
getActivePeriodicScheduledTask() |
Collection<ScheduledFuture<?>> |
getActiveScheduledTasks() |
List<ScheduledFuture<?>> |
getAllNonPeriodicScheduledTask() |
List<ScheduledFuture<?>> |
getAllPeriodicScheduledTask() |
List<ScheduledFuture<?>> |
getAllScheduledTasks() |
<T> List<Future<T>> |
invokeAll(Collection<? extends Callable<T>> tasks) |
<T> List<Future<T>> |
invokeAll(Collection<? extends Callable<T>> tasks,
long timeout,
TimeUnit unit) |
<T> T |
invokeAny(Collection<? extends Callable<T>> tasks) |
<T> T |
invokeAny(Collection<? extends Callable<T>> tasks,
long timeout,
TimeUnit unit) |
boolean |
isShutdown() |
boolean |
isTerminated() |
int |
numQueuedRunnables()
Gets the number of Runnables currently queued.
|
<V> ScheduledFuture<V> |
schedule(Callable<V> callable,
long delay,
TimeUnit unit) |
ScheduledFuture<?> |
schedule(Runnable command,
long delay,
TimeUnit unit) |
ScheduledFuture<?> |
scheduleAtFixedRate(Runnable command,
long initialDelay,
long period,
TimeUnit unit) |
ScheduledFuture<?> |
scheduleWithFixedDelay(Runnable command,
long initialDelay,
long delay,
TimeUnit unit) |
void |
shutdown() |
List<Runnable> |
shutdownNow() |
<T> Future<T> |
submit(Callable<T> task) |
Future<?> |
submit(Runnable task) |
<T> Future<T> |
submit(Runnable task,
T result) |
void |
trigger()
Triggers the next task that was submitted for execution.
|
void |
trigger(Duration timeout)
Triggers the next task that was submitted for execution.
|
void |
triggerAll()
Triggers all
queuedRunnables . |
void |
triggerAllNonPeriodicTasks()
Executes all runnable and scheduled non-periodic tasks until none are left to run.
|
void |
triggerNonPeriodicScheduledTask()
Triggers a single non-periodically scheduled task.
|
void |
triggerNonPeriodicScheduledTasks() |
void |
triggerNonPeriodicScheduledTasks(Class<?> taskClazz) |
void |
triggerNonPeriodicScheduledTasksWithRecursion()
Triggers all non-periodically scheduled tasks.
|
void |
triggerPeriodicScheduledTasks() |
void |
triggerScheduledTasks()
Triggers all registered tasks.
|
public ManuallyTriggeredScheduledExecutorService()
public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit)
schedule
in interface ScheduledExecutorService
public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit)
schedule
in interface ScheduledExecutorService
public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)
scheduleAtFixedRate
in interface ScheduledExecutorService
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)
scheduleWithFixedDelay
in interface ScheduledExecutorService
public void shutdown()
shutdown
in interface ExecutorService
public List<Runnable> shutdownNow()
shutdownNow
in interface ExecutorService
public boolean isShutdown()
isShutdown
in interface ExecutorService
public boolean isTerminated()
isTerminated
in interface ExecutorService
public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException
awaitTermination
in interface ExecutorService
InterruptedException
public <T> Future<T> submit(Callable<T> task)
submit
in interface ExecutorService
public <T> Future<T> submit(Runnable task, T result)
submit
in interface ExecutorService
public Future<?> submit(Runnable task)
submit
in interface ExecutorService
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
invokeAll
in interface ExecutorService
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
invokeAll
in interface ExecutorService
public <T> T invokeAny(Collection<? extends Callable<T>> tasks)
invokeAny
in interface ExecutorService
public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
invokeAny
in interface ExecutorService
public void triggerAllNonPeriodicTasks()
triggerAll()
and triggerNonPeriodicScheduledTasks()
that allows making a test agnostic of how exactly a
runnable is passed to the executor.public void triggerAll()
queuedRunnables
.public void trigger(Duration timeout)
timeout
- The time to wait for a new task to be scheduled.IllegalStateException
- if no task was scheduled in the given amount of time.public void trigger()
IllegalStateException
- if no task was scheduled before calling this method.public int numQueuedRunnables()
public Collection<ScheduledFuture<?>> getActiveScheduledTasks()
public Collection<ScheduledFuture<?>> getActivePeriodicScheduledTask()
public Collection<ScheduledFuture<?>> getActiveNonPeriodicScheduledTask()
public List<ScheduledFuture<?>> getAllScheduledTasks()
public List<ScheduledFuture<?>> getAllPeriodicScheduledTask()
public List<ScheduledFuture<?>> getAllNonPeriodicScheduledTask()
public void triggerScheduledTasks()
public void triggerNonPeriodicScheduledTask()
NoSuchElementException
- If there is no such task.public void triggerNonPeriodicScheduledTasksWithRecursion()
triggerNonPeriodicScheduledTasks()
, if such a task schedules another non-periodically
schedule task, then this new task will also be triggered.public void triggerNonPeriodicScheduledTasks()
public void triggerNonPeriodicScheduledTasks(Class<?> taskClazz)
public void triggerPeriodicScheduledTasks()
Copyright © 2014–2024 The Apache Software Foundation. All rights reserved.