Search in sources :

Example 1 with CancellationScope

use of com.uber.cadence.workflow.CancellationScope in project cadence-client by uber-java.

the class DeterministicRunnerTest method testExplicitThreadCancellation.

@Test
public void testExplicitThreadCancellation() throws Throwable {
    trace.add("init");
    DeterministicRunner d = new DeterministicRunnerImpl(() -> {
        trace.add("root started");
        CompletablePromise<String> threadDone = Workflow.newPromise();
        CancellationScope scope = Workflow.newCancellationScope(() -> {
            Async.procedure(() -> {
                trace.add("thread started");
                Promise<String> cancellation = CancellationScope.current().getCancellationRequest();
                WorkflowInternal.await("reason1", () -> CancellationScope.current().isCancelRequested());
                threadDone.completeFrom(cancellation);
                trace.add("thread done: " + cancellation.get());
            });
        });
        trace.add("root before cancel");
        scope.cancel("from root");
        threadDone.get();
        trace.add("root done");
    });
    d.runUntilAllBlocked();
    assertTrue(d.stackTrace(), d.isDone());
    String[] expected = new String[] { "init", "root started", "root before cancel", "thread started", "thread done: from root", "root done" };
    trace.setExpected(expected);
}
Also used : CancellationScope(com.uber.cadence.workflow.CancellationScope) WorkflowTest(com.uber.cadence.workflow.WorkflowTest) Test(org.junit.Test)

Example 2 with CancellationScope

use of com.uber.cadence.workflow.CancellationScope in project cadence-client by uber-java.

the class DeterministicRunnerTest method testExplicitDetachedScopeCancellation.

@Test
public void testExplicitDetachedScopeCancellation() throws Throwable {
    trace.add("init");
    DeterministicRunner d = new DeterministicRunnerImpl(() -> {
        trace.add("root started");
        CompletablePromise<Void> var = Workflow.newPromise();
        CancellationScope scope = Workflow.newDetachedCancellationScope(() -> {
            trace.add("scope started");
            var.completeFrom(newTimer(300));
            trace.add("scope done");
        });
        trace.add("root before cancel");
        scope.cancel("from root");
        try {
            var.get();
            trace.add("after get");
        } catch (CancellationException e) {
            trace.add("scope cancelled");
        }
        trace.add("root done");
    });
    d.runUntilAllBlocked();
    assertTrue(trace.toString(), d.isDone());
    String[] expected = new String[] { "init", "root started", "scope started", "scope done", "root before cancel", "timer cancelled", "scope cancelled", "root done" };
    trace.setExpected(expected);
}
Also used : CancellationScope(com.uber.cadence.workflow.CancellationScope) CancellationException(java.util.concurrent.CancellationException) WorkflowTest(com.uber.cadence.workflow.WorkflowTest) Test(org.junit.Test)

Example 3 with CancellationScope

use of com.uber.cadence.workflow.CancellationScope in project cadence-client by uber-java.

the class DeterministicRunnerTest method testExplicitScopeCancellation.

@Test
public void testExplicitScopeCancellation() throws Throwable {
    trace.add("init");
    DeterministicRunner d = new DeterministicRunnerImpl(() -> {
        trace.add("root started");
        CompletablePromise<Void> var = Workflow.newPromise();
        CancellationScope scope = Workflow.newCancellationScope(() -> {
            trace.add("scope started");
            var.completeFrom(newTimer(300));
            trace.add("scope done");
        });
        trace.add("root before cancel");
        scope.cancel("from root");
        try {
            var.get();
            trace.add("after get");
        } catch (CancellationException e) {
            trace.add("scope cancelled");
        }
        trace.add("root done");
    });
    d.runUntilAllBlocked();
    assertTrue(trace.toString(), d.isDone());
    String[] expected = new String[] { "init", "root started", "scope started", "scope done", "root before cancel", "timer cancelled", "scope cancelled", "root done" };
    trace.setExpected(expected);
}
Also used : CancellationScope(com.uber.cadence.workflow.CancellationScope) CancellationException(java.util.concurrent.CancellationException) WorkflowTest(com.uber.cadence.workflow.WorkflowTest) Test(org.junit.Test)

Aggregations

CancellationScope (com.uber.cadence.workflow.CancellationScope)3 WorkflowTest (com.uber.cadence.workflow.WorkflowTest)3 Test (org.junit.Test)3 CancellationException (java.util.concurrent.CancellationException)2