public class AsyncTaskManager extends Object implements 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"
}
Modifier and Type | Class and Description |
---|---|
static class |
AsyncTaskManager.BarrierTask<T>
A task which runs using a barrier
|
Constructor and Description |
---|
AsyncTaskManager() |
Modifier and Type | Method and Description |
---|---|
static void |
assertAllNotAwaiting(Collection<? extends AsyncTaskManager.BarrierTask<?>> tasks)
Assert that multiple tasks do not wait on their barriers
|
void |
close()
Close the AsyncTaskManager, opening all barriers and ensuring that all tasks complete
|
Barrier |
newBarrier()
Create a
Barrier 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 barrier
|
AsyncTaskManager.BarrierTask<Void> |
runBarrierTask(Consumer<Barrier> task)
Run a task which awaits on a barrier
|
public AsyncTaskManager.BarrierTask<Void> runBarrierTask(Consumer<Barrier> task)
Since 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.
task
- the task to runpublic <T> AsyncTaskManager.BarrierTask<T> runAsyncBarrierTask(Function<Barrier,Future<? extends T>> task)
The task is assumed to run asynchronously and return a Future
with its result, either by being a method
annotated with Asynchronous
or by some other means.
The returned AsyncTaskManager.BarrierTask
can be used to release the barrier and assert the result of the task.
T
- the return type of the tasktask
- the task to runpublic <T> AsyncTaskManager.BarrierTask<T> runAsyncCsBarrierTask(Function<Barrier,CompletionStage<? extends T>> task)
The task is assumed to run asynchronously and return a CompletionStage
with its result, either by being a
method annotated with Asynchronous
or by some other means.
The returned AsyncTaskManager.BarrierTask
can be used to release the barrier and assert the result of the task.
T
- the return type of the tasktask
- the task to runpublic Barrier newBarrier()
Barrier
not associated with any task
The AsyncTaskManager will ensure that Barrier.open()
is called when close()
is
called.
Barrier
public void close()
close
in interface AutoCloseable
public static void assertAllNotAwaiting(Collection<? extends AsyncTaskManager.BarrierTask<?>> tasks)
This 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.
Copyright © 2016 – 2022 Eclipse Foundation. All rights reserved.
Use is subject to license terms.