public interface ExecHarness
extends ratpack.exec.ExecControl, java.lang.AutoCloseable
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 *single static methods.
yield(Function),
yieldSingle(Function),
run(Action),
runSingle(Action)| Modifier and Type | Method and Description |
|---|---|
default void |
addInterceptor(ratpack.exec.ExecInterceptor execInterceptor,
ratpack.func.NoArgAction continuation) |
default <T> ratpack.exec.Promise<T> |
blocking(java.util.concurrent.Callable<T> blockingOperation) |
void |
close()
Shuts down the thread pool backing this harness.
|
default ratpack.exec.ExecStarter |
exec() |
ratpack.exec.ExecControl |
getControl()
The execution control for the harness.
|
default ratpack.exec.ExecController |
getController() |
default ratpack.exec.Execution |
getExecution() |
static ExecHarness |
harness()
Creates a new execution harness.
|
static ExecHarness |
harness(int numThreads) |
default <T> ratpack.exec.Promise<T> |
promise(ratpack.func.Action<? super ratpack.exec.Fulfiller<T>> action) |
void |
run(ratpack.func.Action<? super ratpack.exec.ExecControl> action)
Initiates an execution and blocks until it completes.
|
static void |
runSingle(ratpack.func.Action<? super ratpack.exec.ExecControl> action)
Convenient form of
run(Action) that creates and closes a harness for the run. |
default <T> ratpack.stream.TransformablePublisher<T> |
stream(org.reactivestreams.Publisher<T> publisher) |
<T> ExecResult<T> |
yield(ratpack.func.Function<ratpack.exec.ExecControl,ratpack.exec.Promise<T>> func)
Synchronously returns a promised value.
|
static <T> ExecResult<T> |
yieldSingle(ratpack.func.Function<ratpack.exec.ExecControl,ratpack.exec.Promise<T>> func)
Creates an exec harness,
executes the given function with it before closing it, then returning execution result. |
static ExecHarness harness()
import ratpack.exec.ExecControl;
import ratpack.exec.Promise;
import ratpack.test.exec.ExecHarness;
import ratpack.test.exec.ExecResult;
import static org.junit.Assert.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 ExecControl execControl;
private final AsyncApi asyncApi = new AsyncApi();
public AsyncService(ExecControl execControl) {
this.execControl = execControl;
}
// Our method under test
public <T> Promise<T> promise(final T value) {
return execControl.promise(fulfiller -> asyncApi.returnAsync(value, fulfiller::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 using the exec control from the harness
final AsyncService service = new AsyncService(harness.getControl());
// 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());
}
}
}
When using Ratpack's RxJava integration, ExecHarness can be used to test rx.Observable instances by first converting them to a promise.
See the ratpack.rx.RxRatpack.asPromise(Observable) documentation for an example of testing observables.static ExecHarness harness(int numThreads)
<T> ExecResult<T> yield(ratpack.func.Function<ratpack.exec.ExecControl,ratpack.exec.Promise<T>> func) throws java.lang.Exception
The given function will execute in a separate thread. The calling thread will block, waiting for the promised value to be provided.
T - the type of promised valuefunc - a function that exercises some code that returns a promisejava.lang.Exception - any thrown by the function, or the promise failure exceptionstatic <T> ExecResult<T> yieldSingle(ratpack.func.Function<ratpack.exec.ExecControl,ratpack.exec.Promise<T>> func) throws java.lang.Exception
executes the given function with it before closing it, then returning execution result.T - the type of promised valuefunc - a function that exercises some code that returns a promisejava.lang.Exception - any thrown by the function, or the promise failure exceptionvoid run(ratpack.func.Action<? super ratpack.exec.ExecControl> action) throws java.lang.Exception
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.
action - the start of the executionjava.lang.Exception - any thrown during the execution that is not explicitly caughtrunSingle(Action),
yield(Function)static void runSingle(ratpack.func.Action<? super ratpack.exec.ExecControl> action)
throws java.lang.Exception
run(Action) that creates and closes a harness for the run.action - the start of the executionjava.lang.Exception - any thrown during the execution that is not explicitly caughtrun(Action),
yield(Function)ratpack.exec.ExecControl getControl()
Note that the execution harness implements ExecControl itself, simply delegating calls this the return of this method.
void close()
close in interface java.lang.AutoCloseabledefault ratpack.exec.ExecStarter exec()
exec in interface ratpack.exec.ExecControldefault ratpack.exec.Execution getExecution()
getExecution in interface ratpack.exec.ExecControldefault ratpack.exec.ExecController getController()
getController in interface ratpack.exec.ExecControldefault void addInterceptor(ratpack.exec.ExecInterceptor execInterceptor,
ratpack.func.NoArgAction continuation)
throws java.lang.Exception
addInterceptor in interface ratpack.exec.ExecControljava.lang.Exceptiondefault <T> ratpack.exec.Promise<T> blocking(java.util.concurrent.Callable<T> blockingOperation)
blocking in interface ratpack.exec.ExecControldefault <T> ratpack.exec.Promise<T> promise(ratpack.func.Action<? super ratpack.exec.Fulfiller<T>> action)
promise in interface ratpack.exec.ExecControldefault <T> ratpack.stream.TransformablePublisher<T> stream(org.reactivestreams.Publisher<T> publisher)
stream in interface ratpack.exec.ExecControl