Search in sources :

Example 1 with ResultBackedExecResult

use of ratpack.exec.internal.ResultBackedExecResult in project ratpack by ratpack.

the class DefaultExecHarness method yield.

@Override
public <T> ExecResult<T> yield(Action<? super RegistrySpec> registry, final Function<? super Execution, ? extends Promise<T>> func) throws Exception {
    AtomicReference<ExecResult<T>> reference = new AtomicReference<>();
    CountDownLatch latch = new CountDownLatch(1);
    controller.fork().register(registry).onError(throwable -> reference.set(new ResultBackedExecResult<>(Result.<T>error(throwable)))).onComplete(exec -> latch.countDown()).start(execution -> {
        reference.set(ExecResult.complete());
        Promise<T> promise = func.apply(execution);
        if (promise == null) {
            reference.set(null);
        } else {
            promise.then(t -> reference.set(new ResultBackedExecResult<>(Result.success(t))));
        }
    });
    latch.await();
    return reference.get();
}
Also used : Function(ratpack.func.Function) CountDownLatch(java.util.concurrent.CountDownLatch) RegistrySpec(ratpack.registry.RegistrySpec) ratpack.exec(ratpack.exec) Action(ratpack.func.Action) ExecHarness(ratpack.test.exec.ExecHarness) Exceptions(ratpack.util.Exceptions) ResultBackedExecResult(ratpack.exec.internal.ResultBackedExecResult) AtomicReference(java.util.concurrent.atomic.AtomicReference) ResultBackedExecResult(ratpack.exec.internal.ResultBackedExecResult) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ResultBackedExecResult(ratpack.exec.internal.ResultBackedExecResult)

Aggregations

CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 ratpack.exec (ratpack.exec)1 ResultBackedExecResult (ratpack.exec.internal.ResultBackedExecResult)1 Action (ratpack.func.Action)1 Function (ratpack.func.Function)1 RegistrySpec (ratpack.registry.RegistrySpec)1 ExecHarness (ratpack.test.exec.ExecHarness)1 Exceptions (ratpack.util.Exceptions)1