Skip to content

Commit c5c3ce7

Browse files
Add more tests and simplify mutable state lookup
1 parent a5d84d0 commit c5c3ce7

2 files changed

Lines changed: 152 additions & 67 deletions

File tree

temporal-test-server/src/main/java/io/temporal/internal/testservice/TestWorkflowService.java

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -280,33 +280,9 @@ private TestWorkflowMutableState getMutableState(
280280
: new WorkflowChainId(namespace, execution.getWorkflowId(), firstExecutionRunId);
281281

282282
if (workflowChainId != null) {
283-
TestWorkflowMutableState mutableStateByFirstRunId = getMutableState(workflowChainId, false);
284-
if (mutableStateByFirstRunId != null) {
285-
return mutableStateByFirstRunId;
286-
}
287-
}
288-
289-
TestWorkflowMutableState mutableState = getMutableState(executionId, false);
290-
if (mutableState != null) {
291-
if (workflowChainId == null) {
292-
return mutableState;
293-
}
294-
WorkflowExecution mutableStateExecution = mutableState.getExecutionId().getExecution();
295-
if (mutableStateExecution.getRunId().equals(execution.getRunId())
296-
&& firstExecutionRunId.equals(mutableState.getFirstExecutionRunId())) {
297-
return mutableState;
298-
}
299-
}
300-
301-
if (failNotExists) {
302-
if (workflowChainId != null) {
303-
throw Status.NOT_FOUND
304-
.withDescription("Execution not found in mutable state: " + workflowChainId)
305-
.asRuntimeException();
306-
}
307-
return getMutableState(executionId, true);
283+
return getMutableState(workflowChainId, true);
308284
}
309-
return null;
285+
return getMutableState(executionId, failNotExists);
310286
}
311287

312288
@Override

temporal-test-server/src/test/java/io/temporal/testserver/functional/FirstExecutionRunIdSupportTest.java

Lines changed: 150 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package io.temporal.testserver.functional;
22

3+
import io.grpc.Status;
4+
import io.grpc.StatusRuntimeException;
35
import 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.*;
77
import io.temporal.client.WorkflowClient;
88
import io.temporal.client.WorkflowFailedException;
99
import io.temporal.client.WorkflowOptions;
@@ -17,11 +17,13 @@
1717
import io.temporal.workflow.WorkflowMethod;
1818
import java.time.Duration;
1919
import java.util.UUID;
20+
import javax.annotation.Nullable;
2021
import org.junit.Assert;
2122
import org.junit.Rule;
2223
import org.junit.Test;
2324

2425
public 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

Comments
 (0)