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);
}
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);
}
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);
}
Aggregations