Skip to content

Commit f1da395

Browse files
Add test coverage for starting a child workflow from a cancelled workflow context
1 parent 6c961a0 commit f1da395

1 file changed

Lines changed: 60 additions & 0 deletions

File tree

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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 cancelled scope"));
35+
36+
}
37+
}
38+
39+
@WorkflowInterface
40+
public interface TestParentWorkflow {
41+
@WorkflowMethod
42+
void execute();
43+
}
44+
45+
public static class TestParentWorkflowImpl implements TestParentWorkflow {
46+
@Override
47+
public void execute() {
48+
TestWorkflows.NoArgsWorkflow child =
49+
Workflow.newChildWorkflowStub(TestWorkflows.NoArgsWorkflow.class);
50+
CancellationScope scope = Workflow.newCancellationScope(child::execute);
51+
scope.cancel();
52+
scope.run();
53+
}
54+
}
55+
56+
public static class TestChildWorkflowImpl implements TestWorkflows.NoArgsWorkflow {
57+
@Override
58+
public void execute() {}
59+
}
60+
}

0 commit comments

Comments
 (0)