Search in sources :

Example 1 with RegistrySpec

use of ratpack.registry.RegistrySpec in project ratpack by ratpack.

the class DefaultExecHarness method run.

@Override
public void run(Action<? super RegistrySpec> registry, Action<? super Execution> action) throws Exception {
    final AtomicReference<Throwable> thrown = new AtomicReference<>();
    final CountDownLatch latch = new CountDownLatch(1);
    controller.fork().onError(thrown::set).register(registry).onComplete(e -> latch.countDown()).start(action::execute);
    latch.await();
    Throwable throwable = thrown.get();
    if (throwable != null) {
        throw Exceptions.toException(throwable);
    }
}
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) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 2 with RegistrySpec

use of ratpack.registry.RegistrySpec 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)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 ratpack.exec (ratpack.exec)2 ResultBackedExecResult (ratpack.exec.internal.ResultBackedExecResult)2 Action (ratpack.func.Action)2 Function (ratpack.func.Function)2 RegistrySpec (ratpack.registry.RegistrySpec)2 ExecHarness (ratpack.test.exec.ExecHarness)2 Exceptions (ratpack.util.Exceptions)2