Class AsyncTaskManager
- All Implemented Interfaces:
AutoCloseable
This class takes care of creating Barriers
and BarrierTasks
and ensuring that
they get cleaned up.
This class implements AutoCloseable
so that it can be used in a try-with-resources block to make it easy to
ensure everything from your test is cleaned up.
Example:
try (AsyncTaskManager taskManager = new AsyncTaskManager()) {
BarrierTask<Void> task = taskManager.runBarrierTask(bean::testMethod);
task.assertNotAwaiting();
task.release();
task.assertSuccess();
}
Example test method:
public void testMethod(Barrier barrier) {
barrier.await();
return "OK"
}
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
A task which runs using a barrier -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic void
assertAllNotAwaiting
(Collection<? extends AsyncTaskManager.BarrierTask<?>> tasks) Assert that multiple tasks do not wait on their barriersvoid
close()
Close the AsyncTaskManager, opening all barriers and ensuring that all tasks completeCreate aBarrier
not associated with any task<T> AsyncTaskManager.BarrierTask<T>
runAsyncBarrierTask
(Function<Barrier, Future<? extends T>> task) Run an asynchronous task which awaits a barrier<T> AsyncTaskManager.BarrierTask<T>
runAsyncCsBarrierTask
(Function<Barrier, CompletionStage<? extends T>> task) Run an asynchronous task which awaits a barrierrunBarrierTask
(Consumer<Barrier> task) Run a task which awaits on a barrier
-
Constructor Details
-
AsyncTaskManager
public AsyncTaskManager()
-
-
Method Details
-
runBarrierTask
Run a task which awaits on a barrierSince the task is assumed to run synchronously and expected to await the barrier,
AsyncCaller
is used to call the task asynchronously.The returned
AsyncTaskManager.BarrierTask
can be used to release the barrier and assert the result of the task.- Parameters:
task
- the task to run- Returns:
- the BarrierTask
-
runAsyncBarrierTask
public <T> AsyncTaskManager.BarrierTask<T> runAsyncBarrierTask(Function<Barrier, Future<? extends T>> task) Run an asynchronous task which awaits a barrierThe task is assumed to run asynchronously and return a
Future
with its result, either by being a method annotated withAsynchronous
or by some other means.The returned
AsyncTaskManager.BarrierTask
can be used to release the barrier and assert the result of the task.- Type Parameters:
T
- the return type of the task- Parameters:
task
- the task to run- Returns:
- the BarrierTask
-
runAsyncCsBarrierTask
public <T> AsyncTaskManager.BarrierTask<T> runAsyncCsBarrierTask(Function<Barrier, CompletionStage<? extends T>> task) Run an asynchronous task which awaits a barrierThe task is assumed to run asynchronously and return a
CompletionStage
with its result, either by being a method annotated withAsynchronous
or by some other means.The returned
AsyncTaskManager.BarrierTask
can be used to release the barrier and assert the result of the task.- Type Parameters:
T
- the return type of the task- Parameters:
task
- the task to run- Returns:
- the BarrierTask
-
newBarrier
Create aBarrier
not associated with any taskThe AsyncTaskManager will ensure that
Barrier.open()
is called whenclose()
is called.- Returns:
- the newly created
Barrier
-
close
public void close()Close the AsyncTaskManager, opening all barriers and ensuring that all tasks complete- Specified by:
close
in interfaceAutoCloseable
-
assertAllNotAwaiting
public static void assertAllNotAwaiting(Collection<? extends AsyncTaskManager.BarrierTask<?>> tasks) Assert that multiple tasks do not wait on their barriersThis method always takes EXPECTED_FAIL_TIME_MS ms.
This method is quicker than calling
AsyncTaskManager.BarrierTask.assertNotAwaiting()
if you need to assert multiple tasks at the same time.
-