Package org.apache.flink.core.testutils
Class ManuallyTriggeredScheduledExecutorService
- java.lang.Object
-
- org.apache.flink.core.testutils.ManuallyTriggeredScheduledExecutorService
-
- All Implemented Interfaces:
Executor,ExecutorService,ScheduledExecutorService
public class ManuallyTriggeredScheduledExecutorService extends Object implements ScheduledExecutorService
SimpleScheduledExecutorServiceimplementation 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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classManuallyTriggeredScheduledExecutorService.RunnableCaller<T>
-
Constructor Summary
Constructors Constructor Description ManuallyTriggeredScheduledExecutorService()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanawaitTermination(long timeout, TimeUnit unit)voidexecute(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> TinvokeAny(Collection<? extends Callable<T>> tasks)<T> TinvokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)booleanisShutdown()booleanisTerminated()intnumQueuedRunnables()Gets the number of Runnables currently queued.ScheduledFuture<?>schedule(Runnable command, long delay, TimeUnit unit)<V> ScheduledFuture<V>schedule(Callable<V> callable, long delay, TimeUnit unit)ScheduledFuture<?>scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)ScheduledFuture<?>scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)voidshutdown()List<Runnable>shutdownNow()Future<?>submit(Runnable task)<T> Future<T>submit(Runnable task, T result)<T> Future<T>submit(Callable<T> task)voidtrigger()Triggers the next task that was submitted for execution.voidtrigger(Duration timeout)Triggers the next task that was submitted for execution.voidtriggerAll()Triggers allqueuedRunnables.voidtriggerAllNonPeriodicTasks()Executes all runnable and scheduled non-periodic tasks until none are left to run.voidtriggerNonPeriodicScheduledTask()Triggers a single non-periodically scheduled task.voidtriggerNonPeriodicScheduledTasks()voidtriggerNonPeriodicScheduledTasks(Class<?> taskClazz)voidtriggerNonPeriodicScheduledTasksWithRecursion()Triggers all non-periodically scheduled tasks.voidtriggerPeriodicScheduledTasks()voidtriggerScheduledTasks()Triggers all registered tasks.
-
-
-
Method Detail
-
schedule
public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit)
- Specified by:
schedulein interfaceScheduledExecutorService
-
schedule
public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit)
- Specified by:
schedulein interfaceScheduledExecutorService
-
scheduleAtFixedRate
public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)
- Specified by:
scheduleAtFixedRatein interfaceScheduledExecutorService
-
scheduleWithFixedDelay
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)
- Specified by:
scheduleWithFixedDelayin interfaceScheduledExecutorService
-
shutdown
public void shutdown()
- Specified by:
shutdownin interfaceExecutorService
-
shutdownNow
public List<Runnable> shutdownNow()
- Specified by:
shutdownNowin interfaceExecutorService
-
isShutdown
public boolean isShutdown()
- Specified by:
isShutdownin interfaceExecutorService
-
isTerminated
public boolean isTerminated()
- Specified by:
isTerminatedin interfaceExecutorService
-
awaitTermination
public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException- Specified by:
awaitTerminationin interfaceExecutorService- Throws:
InterruptedException
-
submit
public <T> Future<T> submit(Callable<T> task)
- Specified by:
submitin interfaceExecutorService
-
submit
public <T> Future<T> submit(Runnable task, T result)
- Specified by:
submitin interfaceExecutorService
-
submit
public Future<?> submit(Runnable task)
- Specified by:
submitin interfaceExecutorService
-
invokeAll
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
- Specified by:
invokeAllin interfaceExecutorService
-
invokeAll
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
- Specified by:
invokeAllin interfaceExecutorService
-
invokeAny
public <T> T invokeAny(Collection<? extends Callable<T>> tasks)
- Specified by:
invokeAnyin interfaceExecutorService
-
invokeAny
public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
- Specified by:
invokeAnyin interfaceExecutorService
-
triggerAllNonPeriodicTasks
public void triggerAllNonPeriodicTasks()
Executes all runnable and scheduled non-periodic tasks until none are left to run. This is essentially a combination oftriggerAll()andtriggerNonPeriodicScheduledTasks()that allows making a test agnostic of how exactly a runnable is passed to the executor.
-
triggerAll
public void triggerAll()
Triggers allqueuedRunnables.
-
trigger
public void trigger(Duration timeout)
Triggers the next task that was submitted for execution. The method blocks the given amount of time if no task is scheduled, yet.- Parameters:
timeout- The time to wait for a new task to be scheduled.- Throws:
IllegalStateException- if no task was scheduled in the given amount of time.
-
trigger
public void trigger()
Triggers the next task that was submitted for execution.- Throws:
IllegalStateException- if no task was scheduled before calling this method.
-
numQueuedRunnables
public int numQueuedRunnables()
Gets the number of Runnables currently queued.
-
getActiveScheduledTasks
public Collection<ScheduledFuture<?>> getActiveScheduledTasks()
-
getActivePeriodicScheduledTask
public Collection<ScheduledFuture<?>> getActivePeriodicScheduledTask()
-
getActiveNonPeriodicScheduledTask
public Collection<ScheduledFuture<?>> getActiveNonPeriodicScheduledTask()
-
getAllScheduledTasks
public List<ScheduledFuture<?>> getAllScheduledTasks()
-
getAllPeriodicScheduledTask
public List<ScheduledFuture<?>> getAllPeriodicScheduledTask()
-
getAllNonPeriodicScheduledTask
public List<ScheduledFuture<?>> getAllNonPeriodicScheduledTask()
-
triggerScheduledTasks
public void triggerScheduledTasks()
Triggers all registered tasks.
-
triggerNonPeriodicScheduledTask
public void triggerNonPeriodicScheduledTask()
Triggers a single non-periodically scheduled task.- Throws:
NoSuchElementException- If there is no such task.
-
triggerNonPeriodicScheduledTasksWithRecursion
public void triggerNonPeriodicScheduledTasksWithRecursion()
Triggers all non-periodically scheduled tasks. In contrast totriggerNonPeriodicScheduledTasks(), if such a task schedules another non-periodically schedule task, then this new task will also be triggered.
-
triggerNonPeriodicScheduledTasks
public void triggerNonPeriodicScheduledTasks()
-
triggerNonPeriodicScheduledTasks
public void triggerNonPeriodicScheduledTasks(Class<?> taskClazz)
-
triggerPeriodicScheduledTasks
public void triggerPeriodicScheduledTasks()
-
-