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 classA task which runs using a barrier -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidassertAllNotAwaiting(Collection<? extends AsyncTaskManager.BarrierTask<?>> tasks) Assert that multiple tasks do not wait on their barriersvoidclose()Close the AsyncTaskManager, opening all barriers and ensuring that all tasks completeCreate aBarriernot 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,
AsyncCalleris used to call the task asynchronously.The returned
AsyncTaskManager.BarrierTaskcan 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
Futurewith its result, either by being a method annotated withAsynchronousor by some other means.The returned
AsyncTaskManager.BarrierTaskcan 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
CompletionStagewith its result, either by being a method annotated withAsynchronousor by some other means.The returned
AsyncTaskManager.BarrierTaskcan 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 aBarriernot 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:
closein 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.
-