Class AsyncHelper
Async.
You need to create an instance of this class to use it. The main reason
to use this instead of Async directly is to have more control
over disabling asynchronous behavior, which should usually only be done
in test code.
By default, all operations are asynchronous like Async. You can call
setUnitTestAsyncMode(Async.Mode) with Async.Mode.DISABLED
to force synchronous behavior when calling AsyncHelper methods. Also like
Async, some methods ignore Async.Mode, and is noted in their Javadocs.
Using an instance of this class instead of Async directly will make it much
easier to test asynchronous code. As mentioned above, you can set the Async.Mode
to Async.Mode.DISABLED before each test to execute the code synchronously in
tests. Unlike using Async directly in tests, there are no problems using
AsyncHelper with multiple threads or parallel test execution. You can also
choose to use a mock AsyncHelper in tests and specify the desired behavior,
which means you are bypassing the asynchronous calls entirely. This may be appropriate
in some situations, for example, when the asynchronous behavior has been tested
in other tests, and you want to simplify test code. As always, the choice depends on
the situation.
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionExecute the givenRunnableasynchronously.<T> CompletableFuture<T>Execute the givenSupplierasynchronously to return a result, using the common fork join pool as the executor.<T> CompletableFuture<T>Execute the givenSupplierasynchronously to return a result, using the common fork join pool as the executor.Execute the givenRunnableasynchronously.voidUse for testing purposes to force synchronous behavior.<T> CompletableFuture<T>supplyAsync(Supplier<T> supplier) Execute the givenSupplierasynchronously to return a result, using the common fork join pool as the executor.<T> CompletableFuture<T>supplyAsync(Supplier<T> supplier, Executor executor) Execute the givenSupplierasynchronously to return a result, using the common fork join pool as the executor.<T> voidwaitFor(CompletableFuture<T> future, long timeout, TimeUnit unit) Helper method that waits for aCompletableFutureup to a specified timeout.<T> voidwaitForAll(Collection<CompletableFuture<T>> futures, long timeout, TimeUnit unit) Helper method that waits for a collection ofCompletableFutureof typeTup to a specified timeout.voidwaitForAllIgnoringType(Collection<CompletableFuture> futures, long timeout, TimeUnit unit) Helper method that waits for a collection ofCompletableFuturewith no explicit type up to a specified timeout.<T> CompletableFuture<T>withMaxTimeout(CompletableFuture<T> future, long timeout, TimeUnit unit) Wraps aCompletableFuturewith a timeout so that it can proceed asynchronously, but still have a maximum duration.<T> CompletableFuture<T>withMaxTimeout(CompletableFuture<T> future, long timeout, TimeUnit unit, ExecutorService executor) Wraps aCompletableFuturewith a timeout so that it can proceed asynchronously, but still have a maximum duration.
-
Constructor Details
-
AsyncHelper
public AsyncHelper()
-
-
Method Details
-
setUnitTestAsyncMode
Use for testing purposes to force synchronous behavior.- Parameters:
mode- enable or disable asynchronous behavior
-
runAsync
Execute the givenRunnableasynchronously. This uses the common fork join pool as the executor.This is an alias method for
doAsync(Runnable)to provide a way to avoid ambiguity in certain situations.- Parameters:
func- the code to run asynchronously- Returns:
- a
CompletableFuturewith no result - See Also:
-
runAsync
Execute the givenRunnableasynchronously using the givenExecutor.Essentially, wraps
CompletableFuture.runAsync(Runnable, Executor)but allowing synchronous behavior if mode isAsync.Mode.DISABLED.This is an alias method for
doAsync(Runnable, Executor)to provide a way to avoid ambiguity in certain situations.- Parameters:
func- the code to run asynchronouslyexecutor- theExecutorto use- Returns:
- a
CompletableFuturewith no result - See Also:
-
doAsync
Execute the givenRunnableasynchronously. This uses the common fork join pool as the executor.- Parameters:
func- the code to run asynchronously- Returns:
- a
CompletableFuturewith no result - See Also:
-
doAsync
Execute the givenRunnableasynchronously using the givenExecutor.Essentially, wraps
CompletableFuture.runAsync(Runnable, Executor)but allowing synchronous behavior if mode isAsync.Mode.DISABLED.- Parameters:
func- the code to run asynchronouslyexecutor- theExecutorto use- Returns:
- a
CompletableFuturewith no result - See Also:
-
supplyAsync
Execute the givenSupplierasynchronously to return a result, using the common fork join pool as the executor.This is an alias method for
doAsync(Supplier)to provide a way to avoid ambiguity in certain situations.- Type Parameters:
T- the type of object being supplied- Parameters:
supplier- the code to run asynchronously- Returns:
- the result returned by the supplier
- See Also:
-
supplyAsync
Execute the givenSupplierasynchronously to return a result, using the common fork join pool as the executor.Essentially, wraps
CompletableFuture.supplyAsync(Supplier, Executor)but allowing synchronous behavior if mode isAsync.Mode.DISABLED.This is an alias method for
doAsync(Supplier, Executor)to provide a way to avoid ambiguity in certain situations.- Type Parameters:
T- the type of object being supplied- Parameters:
supplier- the code to run asynchronouslyexecutor- theExecutorto use- Returns:
- the result returned by the supplier
- See Also:
-
doAsync
Execute the givenSupplierasynchronously to return a result, using the common fork join pool as the executor.- Type Parameters:
T- the type of object being supplied- Parameters:
supplier- the code to run asynchronously- Returns:
- the result returned by the supplier
- See Also:
-
doAsync
Execute the givenSupplierasynchronously to return a result, using the common fork join pool as the executor.Essentially, wraps
CompletableFuture.supplyAsync(Supplier, Executor)but allowing synchronous behavior if mode isAsync.Mode.DISABLED.- Type Parameters:
T- the type of object being supplied- Parameters:
supplier- the code to run asynchronouslyexecutor- theExecutorto use- Returns:
- the result returned by the supplier
- See Also:
-
waitFor
Helper method that waits for aCompletableFutureup to a specified timeout.Note that
Async.Modehas no effect on this method.- Type Parameters:
T- the result returned by the future- Parameters:
future- the CompletableFuture to wait fortimeout- the value of the timeout in the given unitunit- the time unit to use- Throws:
AsyncException- if any error occurs during asynchronous code execution
-
waitForAll
Helper method that waits for a collection ofCompletableFutureof typeTup to a specified timeout.Note that
Async.Modehas no effect on this method.- Type Parameters:
T- the result returned by the futures- Parameters:
futures- the CompletableFuture instances to wait fortimeout- the value of the timeout in the given unitunit- the time unit to use
-
waitForAllIgnoringType
public void waitForAllIgnoringType(Collection<CompletableFuture> futures, long timeout, TimeUnit unit) Helper method that waits for a collection ofCompletableFuturewith no explicit type up to a specified timeout.Note that
Async.Modehas no effect on this method.- Parameters:
futures- the CompletableFuture instances to wait fortimeout- the value of the timeout in the given unitunit- the time unit to use- Throws:
AsyncException- if any error occurs during asynchronous code execution- Implementation Note:
- Suppressed the IntelliJ and Sonar warnings about raw types
-
withMaxTimeout
public <T> CompletableFuture<T> withMaxTimeout(CompletableFuture<T> future, long timeout, TimeUnit unit) Wraps aCompletableFuturewith a timeout so that it can proceed asynchronously, but still have a maximum duration. Uses the common fork join pool as theExecutorService.Note that
Async.Modehas no effect on this method.- Type Parameters:
T- the result returned by the future- Parameters:
future- the CompletableFuture for which to apply the timeouttimeout- the value of the timeout in the given unitunit- the time unit to use- Returns:
- the original
CompletableFuturewrapped by a new one that applies the given timeout - Throws:
AsyncException- if any error occurs during asynchronous code execution, including timeout- See Also:
-
withMaxTimeout
public <T> CompletableFuture<T> withMaxTimeout(CompletableFuture<T> future, long timeout, TimeUnit unit, ExecutorService executor) Wraps aCompletableFuturewith a timeout so that it can proceed asynchronously, but still have a maximum duration. Uses the givenExecutorService.Note that
Async.Modehas no effect on this method.- Type Parameters:
T- the result returned by the future- Parameters:
future- the CompletableFuture for which to apply the timeouttimeout- the value of the timeout in the given unitunit- the time unit to useexecutor- theExecutorServiceto use- Returns:
- the original
CompletableFuturewrapped by a new one that applies the given timeout - Throws:
AsyncException- if any error occurs during asynchronous code execution, including timeout- See Also:
-