Class AsyncHelper

java.lang.Object
org.kiwiproject.concurrent.AsyncHelper

public class AsyncHelper extends Object
This helper class wraps the static utility functions in 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: