|
| 1 | +package io.temporal.workflow.childWorkflowTests; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertTrue; |
| 4 | +import static org.junit.Assert.fail; |
| 5 | + |
| 6 | +import io.temporal.client.WorkflowFailedException; |
| 7 | +import io.temporal.failure.CanceledFailure; |
| 8 | +import io.temporal.testing.internal.SDKTestWorkflowRule; |
| 9 | +import io.temporal.workflow.CancellationScope; |
| 10 | +import io.temporal.workflow.Workflow; |
| 11 | +import io.temporal.workflow.WorkflowInterface; |
| 12 | +import io.temporal.workflow.WorkflowMethod; |
| 13 | +import io.temporal.workflow.shared.TestWorkflows; |
| 14 | +import org.junit.Rule; |
| 15 | +import org.junit.Test; |
| 16 | + |
| 17 | +public class ChildWorkflowStartInCancelledScopeTest { |
| 18 | + |
| 19 | + @Rule |
| 20 | + public SDKTestWorkflowRule testWorkflowRule = |
| 21 | + SDKTestWorkflowRule.newBuilder() |
| 22 | + .setWorkflowTypes(TestParentWorkflowImpl.class, TestChildWorkflowImpl.class) |
| 23 | + .build(); |
| 24 | + |
| 25 | + @Test |
| 26 | + public void testStartChildInCancelledScope() { |
| 27 | + TestParentWorkflow workflow = testWorkflowRule.newWorkflowStub(TestParentWorkflow.class); |
| 28 | + try { |
| 29 | + workflow.execute(); |
| 30 | + fail("unreachable"); |
| 31 | + } catch (WorkflowFailedException e) { |
| 32 | + assertTrue(e.getCause() instanceof CanceledFailure); |
| 33 | + CanceledFailure failure = (CanceledFailure) e.getCause(); |
| 34 | + assertTrue(failure.getOriginalMessage().contains("execute called from a canceled scope")); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + @WorkflowInterface |
| 39 | + public interface TestParentWorkflow { |
| 40 | + @WorkflowMethod |
| 41 | + void execute(); |
| 42 | + } |
| 43 | + |
| 44 | + public static class TestParentWorkflowImpl implements TestParentWorkflow { |
| 45 | + @Override |
| 46 | + public void execute() { |
| 47 | + TestWorkflows.NoArgsWorkflow child = |
| 48 | + Workflow.newChildWorkflowStub(TestWorkflows.NoArgsWorkflow.class); |
| 49 | + CancellationScope scope = Workflow.newCancellationScope(child::execute); |
| 50 | + scope.cancel(); |
| 51 | + scope.run(); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + public static class TestChildWorkflowImpl implements TestWorkflows.NoArgsWorkflow { |
| 56 | + @Override |
| 57 | + public void execute() {} |
| 58 | + } |
| 59 | +} |
0 commit comments