11package io .temporal .testserver .functional ;
22
3+ import io .grpc .Status ;
4+ import io .grpc .StatusRuntimeException ;
35import io .temporal .api .common .v1 .WorkflowExecution ;
4- import io .temporal .api .workflowservice .v1 .DescribeWorkflowExecutionRequest ;
5- import io .temporal .api .workflowservice .v1 .RequestCancelWorkflowExecutionRequest ;
6- import io .temporal .api .workflowservice .v1 .TerminateWorkflowExecutionRequest ;
6+ import io .temporal .api .workflowservice .v1 .*;
77import io .temporal .client .WorkflowClient ;
88import io .temporal .client .WorkflowFailedException ;
99import io .temporal .client .WorkflowOptions ;
1717import io .temporal .workflow .WorkflowMethod ;
1818import java .time .Duration ;
1919import java .util .UUID ;
20+ import javax .annotation .Nullable ;
2021import org .junit .Assert ;
2122import org .junit .Rule ;
2223import org .junit .Test ;
2324
2425public class FirstExecutionRunIdSupportTest {
26+ private static final String BAD_RUN_ID = UUID .randomUUID ().toString ();
2527
2628 @ Rule
2729 public SDKTestWorkflowRule testWorkflowRule =
@@ -33,21 +35,36 @@ public class FirstExecutionRunIdSupportTest {
3335 public void requestCancelWorkflowExecutionUsesFirstExecutionRunId () {
3436 WorkflowHandle handle = startWorkflow ("cancel-first-execution-" + UUID .randomUUID ());
3537
36- testWorkflowRule
37- .getWorkflowClient ()
38- .getWorkflowServiceStubs ()
39- .blockingStub ()
40- .requestCancelWorkflowExecution (
41- RequestCancelWorkflowExecutionRequest .newBuilder ()
42- .setNamespace (testWorkflowRule .getWorkflowClient ().getOptions ().getNamespace ())
43- .setWorkflowExecution (
38+ StatusRuntimeException sre =
39+ Assert .assertThrows (
40+ StatusRuntimeException .class ,
41+ () ->
42+ requestCancelWorkflowExecution (
43+ WorkflowExecution .newBuilder ()
44+ .setWorkflowId (handle .execution .getWorkflowId ())
45+ .setRunId (BAD_RUN_ID )
46+ .build (),
47+ BAD_RUN_ID ));
48+ Assert .assertEquals (
49+ "Cancel with bad runID should fail" , Status .NOT_FOUND .getCode (), sre .getStatus ().getCode ());
50+
51+ sre =
52+ Assert .assertThrows (
53+ StatusRuntimeException .class ,
54+ () ->
55+ requestCancelWorkflowExecution (
4456 WorkflowExecution .newBuilder ()
4557 .setWorkflowId (handle .execution .getWorkflowId ())
46- .build ())
47- .setFirstExecutionRunId (handle .execution .getRunId ())
48- .setIdentity ("test-client" )
49- .build ());
58+ .build (),
59+ BAD_RUN_ID ));
60+ Assert .assertEquals (
61+ "Cancel with bad firstExecutionRunId should fail" ,
62+ Status .NOT_FOUND .getCode (),
63+ sre .getStatus ().getCode ());
5064
65+ requestCancelWorkflowExecution (
66+ WorkflowExecution .newBuilder ().setWorkflowId (handle .execution .getWorkflowId ()).build (),
67+ handle .execution .getRunId ());
5168 WorkflowFailedException failure =
5269 Assert .assertThrows (WorkflowFailedException .class , () -> handle .stub .getResult (Void .class ));
5370 Assert .assertTrue (failure .getCause () instanceof CanceledFailure );
@@ -57,21 +74,38 @@ public void requestCancelWorkflowExecutionUsesFirstExecutionRunId() {
5774 public void terminateWorkflowExecutionUsesFirstExecutionRunId () {
5875 WorkflowHandle handle = startWorkflow ("terminate-first-execution-" + UUID .randomUUID ());
5976
60- testWorkflowRule
61- .getWorkflowClient ()
62- .getWorkflowServiceStubs ()
63- .blockingStub ()
64- .terminateWorkflowExecution (
65- TerminateWorkflowExecutionRequest .newBuilder ()
66- .setNamespace (testWorkflowRule .getWorkflowClient ().getOptions ().getNamespace ())
67- .setWorkflowExecution (
77+ StatusRuntimeException sre =
78+ Assert .assertThrows (
79+ StatusRuntimeException .class ,
80+ () ->
81+ terminateWorkflowExecution (
82+ WorkflowExecution .newBuilder ()
83+ .setWorkflowId (handle .execution .getWorkflowId ())
84+ .setRunId (BAD_RUN_ID )
85+ .build (),
86+ null ));
87+ Assert .assertEquals (
88+ "Terminate with bad runID should fail" ,
89+ Status .NOT_FOUND .getCode (),
90+ sre .getStatus ().getCode ());
91+
92+ sre =
93+ Assert .assertThrows (
94+ StatusRuntimeException .class ,
95+ () ->
96+ terminateWorkflowExecution (
6897 WorkflowExecution .newBuilder ()
6998 .setWorkflowId (handle .execution .getWorkflowId ())
70- .build ())
71- .setFirstExecutionRunId (handle .execution .getRunId ())
72- .setReason ("terminated for test" )
73- .setIdentity ("test-client" )
74- .build ());
99+ .build (),
100+ BAD_RUN_ID ));
101+ Assert .assertEquals (
102+ "Terminate with bad firstExecutionRunId should fail" ,
103+ Status .NOT_FOUND .getCode (),
104+ sre .getStatus ().getCode ());
105+
106+ terminateWorkflowExecution (
107+ WorkflowExecution .newBuilder ().setWorkflowId (handle .execution .getWorkflowId ()).build (),
108+ handle .execution .getRunId ());
75109
76110 WorkflowFailedException failure =
77111 Assert .assertThrows (WorkflowFailedException .class , () -> handle .stub .getResult (Void .class ));
@@ -86,27 +120,102 @@ public void requestCancelWorkflowExecutionUsesFirstExecutionRunIdAfterContinueAs
86120 String continuedRunId = awaitContinuedRun (handle );
87121 Assert .assertNotEquals (handle .firstExecution .getRunId (), continuedRunId );
88122
89- testWorkflowRule
90- .getWorkflowClient ()
91- .getWorkflowServiceStubs ()
92- .blockingStub ()
93- .requestCancelWorkflowExecution (
94- RequestCancelWorkflowExecutionRequest .newBuilder ()
95- .setNamespace (testWorkflowRule .getWorkflowClient ().getOptions ().getNamespace ())
96- .setWorkflowExecution (
123+ StatusRuntimeException sre =
124+ Assert .assertThrows (
125+ StatusRuntimeException .class ,
126+ () ->
127+ requestCancelWorkflowExecution (
97128 WorkflowExecution .newBuilder ()
98129 .setWorkflowId (handle .firstExecution .getWorkflowId ())
99- .setRunId (handle .firstExecution .getRunId ())
100- .build ())
101- .setFirstExecutionRunId (handle .firstExecution .getRunId ())
102- .setIdentity ("test-client" )
103- .build ());
130+ .build (),
131+ continuedRunId ));
132+ // Using continued runId as firstExecutionRunId should fail
133+ Assert .assertEquals (
134+ "Cancel with wrong firstExecutionRunId should fail" ,
135+ Status .NOT_FOUND .getCode (),
136+ sre .getStatus ().getCode ());
137+
138+ requestCancelWorkflowExecution (
139+ WorkflowExecution .newBuilder ()
140+ .setWorkflowId (handle .firstExecution .getWorkflowId ())
141+ .setRunId (handle .firstExecution .getRunId ())
142+ .build (),
143+ handle .firstExecution .getRunId ());
104144
105145 WorkflowFailedException failure =
106146 Assert .assertThrows (WorkflowFailedException .class , () -> handle .stub .getResult (Void .class ));
107147 Assert .assertTrue (failure .getCause () instanceof CanceledFailure );
108148 }
109149
150+ @ Test
151+ public void terminateWorkflowExecutionUsesFirstExecutionRunIdAfterContinueAsNew () {
152+ ContinueAsNewWorkflowHandle handle =
153+ startContinueAsNewWorkflow ("cancel-first-execution-after-continue-" + UUID .randomUUID ());
154+
155+ String continuedRunId = awaitContinuedRun (handle );
156+ Assert .assertNotEquals (handle .firstExecution .getRunId (), continuedRunId );
157+
158+ StatusRuntimeException sre =
159+ Assert .assertThrows (
160+ StatusRuntimeException .class ,
161+ () ->
162+ terminateWorkflowExecution (
163+ WorkflowExecution .newBuilder ()
164+ .setWorkflowId (handle .firstExecution .getWorkflowId ())
165+ .build (),
166+ continuedRunId ));
167+ // Using continued runId as firstExecutionRunId should fail
168+ Assert .assertEquals (
169+ "Terminate with wrong firstExecutionRunId should fail" ,
170+ Status .NOT_FOUND .getCode (),
171+ sre .getStatus ().getCode ());
172+
173+ terminateWorkflowExecution (
174+ WorkflowExecution .newBuilder ()
175+ .setWorkflowId (handle .firstExecution .getWorkflowId ())
176+ .setRunId (handle .firstExecution .getRunId ())
177+ .build (),
178+ handle .firstExecution .getRunId ());
179+
180+ WorkflowFailedException failure =
181+ Assert .assertThrows (WorkflowFailedException .class , () -> handle .stub .getResult (Void .class ));
182+ Assert .assertTrue (failure .getCause () instanceof TerminatedFailure );
183+ }
184+
185+ private RequestCancelWorkflowExecutionResponse requestCancelWorkflowExecution (
186+ WorkflowExecution exec , @ Nullable String firstRunId ) {
187+ RequestCancelWorkflowExecutionRequest .Builder cancelBuilder =
188+ RequestCancelWorkflowExecutionRequest .newBuilder ()
189+ .setNamespace (testWorkflowRule .getWorkflowClient ().getOptions ().getNamespace ())
190+ .setWorkflowExecution (exec )
191+ .setIdentity ("test-client" );
192+ if (firstRunId != null ) {
193+ cancelBuilder .setFirstExecutionRunId (firstRunId );
194+ }
195+ return testWorkflowRule
196+ .getWorkflowClient ()
197+ .getWorkflowServiceStubs ()
198+ .blockingStub ()
199+ .requestCancelWorkflowExecution (cancelBuilder .build ());
200+ }
201+
202+ private TerminateWorkflowExecutionResponse terminateWorkflowExecution (
203+ WorkflowExecution exec , @ Nullable String firstRunId ) {
204+ TerminateWorkflowExecutionRequest .Builder terminateBuilder =
205+ TerminateWorkflowExecutionRequest .newBuilder ()
206+ .setNamespace (testWorkflowRule .getWorkflowClient ().getOptions ().getNamespace ())
207+ .setWorkflowExecution (exec )
208+ .setIdentity ("test-client" );
209+ if (firstRunId != null ) {
210+ terminateBuilder .setFirstExecutionRunId (firstRunId );
211+ }
212+ return testWorkflowRule
213+ .getWorkflowClient ()
214+ .getWorkflowServiceStubs ()
215+ .blockingStub ()
216+ .terminateWorkflowExecution (terminateBuilder .build ());
217+ }
218+
110219 private WorkflowHandle startWorkflow (String workflowId ) {
111220 WorkflowOptions options =
112221 WorkflowOptions .newBuilder ()
0 commit comments