|
| 1 | +package io.temporal.workflow.cancellationTests; |
| 2 | + |
| 3 | +import static org.junit.Assert.*; |
| 4 | + |
| 5 | +import io.temporal.api.common.v1.WorkflowExecution; |
| 6 | +import io.temporal.client.WorkflowClient; |
| 7 | +import io.temporal.client.WorkflowFailedException; |
| 8 | +import io.temporal.client.WorkflowStub; |
| 9 | +import io.temporal.failure.CanceledFailure; |
| 10 | +import io.temporal.testing.internal.SDKTestWorkflowRule; |
| 11 | +import io.temporal.workflow.ExternalWorkflowStub; |
| 12 | +import io.temporal.workflow.Workflow; |
| 13 | +import io.temporal.workflow.shared.TestWorkflows; |
| 14 | +import org.junit.Rule; |
| 15 | +import org.junit.Test; |
| 16 | + |
| 17 | +public class ExternalWorkflowCancelTest { |
| 18 | + @Rule |
| 19 | + public SDKTestWorkflowRule testWorkflowRule = |
| 20 | + SDKTestWorkflowRule.newBuilder() |
| 21 | + .setWorkflowTypes(CancellableWorkflowImpl.class, CancelExternalWorkflowImpl.class) |
| 22 | + .build(); |
| 23 | + |
| 24 | + @Test |
| 25 | + public void cancelExternalWorkflow() { |
| 26 | + TestWorkflows.TestWorkflow1 cancellable = |
| 27 | + testWorkflowRule.newWorkflowStubTimeoutOptions(TestWorkflows.TestWorkflow1.class); |
| 28 | + WorkflowClient.start(cancellable::execute, "ignored"); |
| 29 | + WorkflowExecution execution = WorkflowStub.fromTyped(cancellable).getExecution(); |
| 30 | + |
| 31 | + TestWorkflows.TestWorkflowStringArg canceler = |
| 32 | + testWorkflowRule.newWorkflowStubTimeoutOptions(TestWorkflows.TestWorkflowStringArg.class); |
| 33 | + canceler.execute(execution.getWorkflowId()); |
| 34 | + |
| 35 | + WorkflowStub cancellableStub = WorkflowStub.fromTyped(cancellable); |
| 36 | + try { |
| 37 | + cancellableStub.getResult(String.class); |
| 38 | + fail("unreachable"); |
| 39 | + } catch (WorkflowFailedException e) { |
| 40 | + assertTrue(e.getCause() instanceof CanceledFailure); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + public static class CancellableWorkflowImpl implements TestWorkflows.TestWorkflow1 { |
| 45 | + @Override |
| 46 | + public String execute(String input) { |
| 47 | + Workflow.await(() -> false); |
| 48 | + return "done"; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + public static class CancelExternalWorkflowImpl implements TestWorkflows.TestWorkflowStringArg { |
| 53 | + @Override |
| 54 | + public void execute(String targetWorkflowId) { |
| 55 | + TestWorkflows.TestWorkflow1 target = |
| 56 | + Workflow.newExternalWorkflowStub(TestWorkflows.TestWorkflow1.class, targetWorkflowId); |
| 57 | + ExternalWorkflowStub.fromTyped(target).cancel(); |
| 58 | + } |
| 59 | + } |
| 60 | +} |
0 commit comments