Class AsyncTaskManager

java.lang.Object
org.eclipse.microprofile.fault.tolerance.tck.util.AsyncTaskManager
All Implemented Interfaces:
AutoCloseable

public class AsyncTaskManager extends Object implements AutoCloseable
Utility for running concurrent tasks which need to wait

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"
 }
 
 
  • Constructor Details

    • AsyncTaskManager

      public AsyncTaskManager()
  • Method Details

    • runBarrierTask

      public AsyncTaskManager.BarrierTask<Void> runBarrierTask(Consumer<Barrier> task)
      Run a task which awaits on a barrier

      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.

      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 barrier

      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.

      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 barrier

      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.

      Type Parameters:
      T - the return type of the task
      Parameters:
      task - the task to run
      Returns:
      the BarrierTask
    • newBarrier

      public Barrier newBarrier()
      Create a Barrier not associated with any task

      The AsyncTaskManager will ensure that Barrier.open() is called when close() 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 interface AutoCloseable
    • assertAllNotAwaiting

      public static void assertAllNotAwaiting(Collection<? extends AsyncTaskManager.BarrierTask<?>> tasks)
      Assert that multiple tasks do not wait on their barriers

      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.