Skip to content

Commit

Permalink
Merge pull request #1515 from annamalai87/test-enhancement
Browse files Browse the repository at this point in the history
executingSingle with registry
  • Loading branch information
johnrengelman committed Apr 27, 2020
2 parents 5c2f987 + 0360ac4 commit 5a822db
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
23 changes: 23 additions & 0 deletions ratpack-exec/src/test/groovy/ratpack/exec/OperationSpec.groovy
Expand Up @@ -16,6 +16,8 @@

package ratpack.exec

import ratpack.handling.Context
import ratpack.registry.RegistrySpec
import ratpack.test.exec.ExecHarness
import spock.lang.AutoCleanup
import spock.lang.Specification
Expand Down Expand Up @@ -45,6 +47,27 @@ class OperationSpec extends Specification {
events == [1, 2, 3, 4]
}

def "An operation that needs execution context"() {

given: "a context"
Context context = Mock {
1 * get(Integer) >> 1
}
Closure<RegistrySpec> registry = { RegistrySpec registrySpec ->
registrySpec.add(context)
}

when: "executing with the registry"
exec.executeSingle(registry) { Execution execution ->
async(execution.current().get(Context).get(Integer))
.next { events << it }
.operation()
}

then:
events == [1]
}

private <T> Promise<T> async(T t) {
Promise.async { f -> Thread.start { f.success(t) } }
}
Expand Down
10 changes: 10 additions & 0 deletions ratpack-test/src/main/java/ratpack/test/exec/ExecHarness.java
Expand Up @@ -229,6 +229,10 @@ default void execute(Function<? super Execution, ? extends Operation> function)
yield(e -> function.apply(e).promise()).getValueOrThrow();
}

default void execute(Action<? super RegistrySpec> registry, Function<? super Execution, ? extends Operation> function) throws Exception {
yield(registry, e -> function.apply(e).promise()).getValueOrThrow();
}

default void execute(Operation operation) throws Exception {
execute(e -> operation);
}
Expand All @@ -239,6 +243,12 @@ static void executeSingle(Function<? super Execution, ? extends Operation> funct
}
}

static void executeSingle(Action<? super RegistrySpec> registry, Function<? super Execution, ? extends Operation> function) throws Exception {
try (ExecHarness harness = harness()) {
harness.execute(registry, function);
}
}

static void executeSingle(Operation operation) throws Exception {
try (ExecHarness harness = harness()) {
harness.execute(operation);
Expand Down

0 comments on commit 5a822db

Please sign in to comment.