Search in sources :

Example 1 with CancellationScope

use of io.temporal.workflow.CancellationScope in project sdk-java by temporalio.

the class DeterministicRunnerTest method testExplicitDetachedScopeCancellation.

@Test
public void testExplicitDetachedScopeCancellation() {
    trace.add("init");
    DeterministicRunner d = new DeterministicRunnerImpl(threadPool, DummySyncWorkflowContext.newDummySyncWorkflowContext(), () -> {
        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");
        });
        scope.run();
        trace.add("root before cancel");
        scope.cancel("from root");
        try {
            var.get();
            trace.add("after get");
        } catch (CanceledFailure e) {
            trace.add("scope canceled");
        }
        trace.add("root done");
    });
    d.runUntilAllBlocked(DeterministicRunner.DEFAULT_DEADLOCK_DETECTION_TIMEOUT_MS);
    assertTrue(trace.toString(), d.isDone());
    String[] expected = new String[] { "init", "root started", "scope started", "scope done", "root before cancel", "timer canceled", "scope canceled", "root done" };
    trace.setExpected(expected);
}
Also used : CancellationScope(io.temporal.workflow.CancellationScope) CanceledFailure(io.temporal.failure.CanceledFailure)

Example 2 with CancellationScope

use of io.temporal.workflow.CancellationScope in project sdk-java by temporalio.

the class DeterministicRunnerTest method testExplicitThreadCancellation.

@Test
public void testExplicitThreadCancellation() {
    trace.add("init");
    DeterministicRunner d = new DeterministicRunnerImpl(threadPool, DummySyncWorkflowContext.newDummySyncWorkflowContext(), () -> {
        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();
                WorkflowThread.await("reason1", () -> CancellationScope.current().isCancelRequested());
                threadDone.completeFrom(cancellation);
                trace.add("thread done: " + cancellation.get());
            });
        });
        scope.run();
        trace.add("root before cancel");
        scope.cancel("from root");
        threadDone.get();
        trace.add("root done");
    });
    d.runUntilAllBlocked(DeterministicRunner.DEFAULT_DEADLOCK_DETECTION_TIMEOUT_MS);
    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(io.temporal.workflow.CancellationScope)

Example 3 with CancellationScope

use of io.temporal.workflow.CancellationScope in project sdk-java by temporalio.

the class DeterministicRunnerTest method testExplicitScopeCancellation.

@Test
public void testExplicitScopeCancellation() {
    trace.add("init");
    DeterministicRunner d = new DeterministicRunnerImpl(threadPool, DummySyncWorkflowContext.newDummySyncWorkflowContext(), () -> {
        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");
        });
        scope.run();
        trace.add("root before cancel");
        scope.cancel("from root");
        try {
            var.get();
            trace.add("after get");
        } catch (CanceledFailure e) {
            trace.add("scope canceled");
        }
        trace.add("root done");
    });
    d.runUntilAllBlocked(DeterministicRunner.DEFAULT_DEADLOCK_DETECTION_TIMEOUT_MS);
    assertTrue(trace.toString(), d.isDone());
    String[] expected = new String[] { "init", "root started", "scope started", "scope done", "root before cancel", "timer canceled", "scope canceled", "root done" };
    trace.setExpected(expected);
}
Also used : CancellationScope(io.temporal.workflow.CancellationScope) CanceledFailure(io.temporal.failure.CanceledFailure)

Aggregations

CancellationScope (io.temporal.workflow.CancellationScope)3 CanceledFailure (io.temporal.failure.CanceledFailure)2