Interface ExecHarness
- 
- All Superinterfaces:
- AutoCloseable
 
 public interface ExecHarness extends AutoCloseable A utility for testing asynchronous support/service code.An execution harness is backed by a thread pool. It is important to call close()when the object is no longer needed to shutdown this thread pool. Alternatively, if you are performing a single operation you can use one of the*singlestatic methods.- See Also:
- yield(Function),- yieldSingle(Function),- run(Action),- runSingle(Action)
 
- 
- 
Method SummaryAll Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description voidclose()Shuts down the thread pool backing this harness.default voidexecute(Operation operation)default voidexecute(Action<? super RegistrySpec> registry, Function<? super Execution,? extends Operation> function)default voidexecute(Function<? super Execution,? extends Operation> function)static voidexecuteSingle(Operation operation)static voidexecuteSingle(Action<? super RegistrySpec> registry, Function<? super Execution,? extends Operation> function)static voidexecuteSingle(Function<? super Execution,? extends Operation> function)default ExecStarterfork()ExecControllergetController()static ExecHarnessharness()Creates a new execution harness.static ExecHarnessharness(int numThreads)Create a harness with the provided number of computation threads.static ExecHarnessharness(Action<? super ExecControllerSpec> definition)Create a harness that is backed by an execution controller with the provided configurationdefault voidrun(Action<? super Execution> action)Initiates an execution and blocks until it completes.voidrun(Action<? super RegistrySpec> registry, Action<? super Execution> action)Initiates an execution and blocks until it completes.static voidrunSingle(Action<? super Execution> action)Convenient form ofrun(Action)that creates and closes a harness for the run.static voidrunSingle(Action<? super RegistrySpec> registry, Action<? super Execution> action)Convenient form ofrun(Action, Action)that creates and closes a harness for the run.<T> ExecResult<T>yield(Action<? super RegistrySpec> registry, Function<? super Execution,? extends Promise<T>> func)Synchronously returns a promised value.default <T> ExecResult<T>yield(Function<? super Execution,? extends Promise<T>> func)Synchronously returns a promised value.static <T> ExecResult<T>yieldSingle(Action<? super RegistrySpec> registry, Function<? super Execution,? extends Promise<T>> func)Creates an exec harness,executesthe given function with it before closing it, then returning execution result.static <T> ExecResult<T>yieldSingle(Function<? super Execution,? extends Promise<T>> func)Creates an exec harness,executesthe given function with it before closing it, then returning execution result.
 
- 
- 
- 
Method Detail- 
harnessstatic ExecHarness harness() Creates a new execution harness.
 When using Ratpack's RxJava integration, ExecHarness can be used to testimport ratpack.exec.Promise; import ratpack.test.exec.ExecHarness; import ratpack.exec.ExecResult; import static org.junit.jupiter.api.Assertions.assertEquals; public class Example { // An async callback based API static class AsyncApi { static interface Callback<T> { void receive(T value); } public <T> void returnAsync(T value, Callback<? super T> callback) { new Thread(() -> callback.receive(value)).run(); } } // Our service class that wraps the raw async API // In the real app this is created by the DI container (e.g. Guice) static class AsyncService { private final AsyncApi asyncApi = new AsyncApi(); // Our method under test public <T> Promise<T> promise(final T value) { return Promise.async(downstream -> asyncApi.returnAsync(value, downstream::success)); } } public static void main(String[] args) throws Throwable { // the harness must be close()'d when finished with to free resources try (ExecHarness harness = ExecHarness.harness()) { // set up the code under test final AsyncService service = new AsyncService(); // exercise the async code using the harness, blocking until the promised value is available ExecResult<String> result = harness.yield(execution -> service.promise("foo")); assertEquals("foo", result.getValue()); } } }rx.Observableinstances by first converting them to a promise. See theratpack.rx2.RxRatpack.single(Observable)documentation for an example of testing observables.- Returns:
- a new execution harness
 
 - 
harnessstatic ExecHarness harness(int numThreads) Create a harness with the provided number of computation threads.- Parameters:
- numThreads- the number of computation threads
- Returns:
- an execution harness
 
 - 
harnessstatic ExecHarness harness(Action<? super ExecControllerSpec> definition) Create a harness that is backed by an execution controller with the provided configuration- Parameters:
- definition- the controller specification
- Returns:
- an execution harness
- Since:
- 2.0
 
 - 
forkdefault ExecStarter fork() 
 - 
getControllerExecController getController() 
 - 
yielddefault <T> ExecResult<T> yield(Function<? super Execution,? extends Promise<T>> func) throws Exception Synchronously returns a promised value.The given function will execute in a separate thread. The calling thread will block, waiting for the promised value to be provided. - Type Parameters:
- T- the type of promised value
- Parameters:
- func- a function that exercises some code that returns a promise
- Returns:
- the result of the execution
- Throws:
- Exception- any thrown by the function
 
 - 
yield<T> ExecResult<T> yield(Action<? super RegistrySpec> registry, Function<? super Execution,? extends Promise<T>> func) throws Exception Synchronously returns a promised value.The given function will execute in a separate thread. The calling thread will block, waiting for the promised value to be provided. - Type Parameters:
- T- the type of promised value
- Parameters:
- registry- the intial contents of the execution registry
- func- a function that exercises some code that returns a promise
- Returns:
- the result of the execution
- Throws:
- Exception- any thrown by the function
 
 - 
yieldSinglestatic <T> ExecResult<T> yieldSingle(Function<? super Execution,? extends Promise<T>> func) throws Exception Creates an exec harness,executesthe given function with it before closing it, then returning execution result.- Type Parameters:
- T- the type of promised value
- Parameters:
- func- a function that exercises some code that returns a promise
- Returns:
- the result of the execution
- Throws:
- Exception- any thrown by the function, or the promise failure exception
 
 - 
yieldSinglestatic <T> ExecResult<T> yieldSingle(Action<? super RegistrySpec> registry, Function<? super Execution,? extends Promise<T>> func) throws Exception Creates an exec harness,executesthe given function with it before closing it, then returning execution result.- Type Parameters:
- T- the type of promised value
- Parameters:
- registry- the intial contents of the execution registry
- func- a function that exercises some code that returns a promise
- Returns:
- the result of the execution
- Throws:
- Exception- any thrown by the function, or the promise failure exception
 
 - 
rundefault void run(Action<? super Execution> action) throws Exception Initiates an execution and blocks until it completes. If an uncaught exception is thrown during the execution, it will be thrown by this method.This method is useful for testing an execution that has some detectable side effect, as this method does not return the “result” of the execution. - Parameters:
- action- the start of the execution
- Throws:
- Exception- any thrown during the execution that is not explicitly caught
- See Also:
- runSingle(Action),- yield(Function)
 
 - 
runvoid run(Action<? super RegistrySpec> registry, Action<? super Execution> action) throws Exception Initiates an execution and blocks until it completes. If an uncaught exception is thrown during the execution, it will be thrown by this method.This method is useful for testing an execution that has some detectable side effect, as this method does not return the “result” of the execution. - Parameters:
- registry- the intial contents of the execution registry
- action- the start of the execution
- Throws:
- Exception- any thrown during the execution that is not explicitly caught
- See Also:
- runSingle(Action),- yield(Function)
 
 - 
runSinglestatic void runSingle(Action<? super Execution> action) throws Exception Convenient form ofrun(Action)that creates and closes a harness for the run.- Parameters:
- action- the start of the execution
- Throws:
- Exception- any thrown during the execution that is not explicitly caught
- See Also:
- run(Action),- yield(Function)
 
 - 
runSinglestatic void runSingle(Action<? super RegistrySpec> registry, Action<? super Execution> action) throws Exception Convenient form ofrun(Action, Action)that creates and closes a harness for the run.- Parameters:
- registry- the intial contents of the execution registry
- action- the start of the execution
- Throws:
- Exception- any thrown during the execution that is not explicitly caught
- See Also:
- run(Action),- yield(Function)
 
 - 
executedefault void execute(Function<? super Execution,? extends Operation> function) throws Exception - Throws:
- Exception
 
 - 
executedefault void execute(Action<? super RegistrySpec> registry, Function<? super Execution,? extends Operation> function) throws Exception - Throws:
- Exception
 
 - 
executeSinglestatic void executeSingle(Function<? super Execution,? extends Operation> function) throws Exception - Throws:
- Exception
 
 - 
executeSinglestatic void executeSingle(Action<? super RegistrySpec> registry, Function<? super Execution,? extends Operation> function) throws Exception - Throws:
- Exception
 
 - 
closevoid close() Shuts down the thread pool backing this harness.- Specified by:
- closein interface- AutoCloseable
 
 
- 
 
-