Skip to content

Commit ef5848e

Browse files
Add more tests
1 parent 245dce2 commit ef5848e

File tree

2 files changed

+84
-6
lines changed

2 files changed

+84
-6
lines changed

temporal-sdk/src/test/java/io/temporal/client/functional/WorkflowStubFirstExecutionRunIdTest.java

Lines changed: 69 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.temporal.client.functional;
22

3+
import io.temporal.api.enums.v1.EventType;
34
import io.temporal.client.*;
45
import io.temporal.testing.internal.SDKTestWorkflowRule;
56
import io.temporal.workflow.Workflow;
@@ -12,10 +13,7 @@
1213
public class WorkflowStubFirstExecutionRunIdTest {
1314
@Rule
1415
public SDKTestWorkflowRule testWorkflowRule =
15-
SDKTestWorkflowRule.newBuilder()
16-
.setUseExternalService(true)
17-
.setWorkflowTypes(AwaitingWorkflow.class)
18-
.build();
16+
SDKTestWorkflowRule.newBuilder().setWorkflowTypes(AwaitingWorkflow.class).build();
1917

2018
@Test
2119
public void terminateFollowFirstRunId() throws InterruptedException {
@@ -52,6 +50,73 @@ public void terminateFollowFirstRunId() throws InterruptedException {
5250
() -> untyped.getResult(String.class));
5351
}
5452

53+
@Test
54+
public void cancelFollowFirstRunId() throws InterruptedException {
55+
TestWorkflows.TestWorkflow1 workflow =
56+
testWorkflowRule.newWorkflowStub(TestWorkflows.TestWorkflow1.class);
57+
WorkflowClient.start(workflow::execute, "input1");
58+
WorkflowStub untyped = WorkflowStub.fromTyped(workflow);
59+
// TODO wait for the continue as new to be visible
60+
Thread.sleep(1000);
61+
testWorkflowRule
62+
.getWorkflowClient()
63+
.newUntypedWorkflowStub(
64+
Optional.empty(),
65+
WorkflowTargetOptions.newBuilder().setWorkflowExecution(untyped.getExecution()).build())
66+
.cancel();
67+
testWorkflowRule.assertNoHistoryEvent(
68+
untyped.getExecution().getWorkflowId(),
69+
untyped.getExecution().getRunId(),
70+
EventType.EVENT_TYPE_WORKFLOW_EXECUTION_CANCEL_REQUESTED);
71+
72+
testWorkflowRule
73+
.getWorkflowClient()
74+
.newUntypedWorkflowStub(
75+
Optional.empty(),
76+
WorkflowTargetOptions.newBuilder()
77+
.setWorkflowExecution(untyped.getExecution())
78+
.setFirstExecutionRunId(untyped.getExecution().getRunId())
79+
.build())
80+
.cancel();
81+
Assert.assertThrows(
82+
"Workflow should be cancelled",
83+
WorkflowFailedException.class,
84+
() -> untyped.getResult(String.class));
85+
testWorkflowRule.assertHistoryEvent(
86+
untyped.getExecution().getWorkflowId(),
87+
EventType.EVENT_TYPE_WORKFLOW_EXECUTION_CANCEL_REQUESTED);
88+
}
89+
90+
@Test
91+
public void signalRespectRunId() throws InterruptedException {
92+
TestWorkflows.TestWorkflow1 workflow =
93+
testWorkflowRule.newWorkflowStub(TestWorkflows.TestWorkflow1.class);
94+
WorkflowClient.start(workflow::execute, "input1");
95+
WorkflowStub untyped = WorkflowStub.fromTyped(workflow);
96+
// TODO wait for the continue as new to be visible
97+
Thread.sleep(1000);
98+
Assert.assertThrows(
99+
WorkflowNotFoundException.class,
100+
() ->
101+
testWorkflowRule
102+
.getWorkflowClient()
103+
.newUntypedWorkflowStub(
104+
Optional.empty(),
105+
WorkflowTargetOptions.newBuilder()
106+
.setWorkflowExecution(untyped.getExecution())
107+
.build())
108+
.signal("signal"));
109+
110+
testWorkflowRule
111+
.getWorkflowClient()
112+
.newUntypedWorkflowStub(
113+
Optional.empty(),
114+
WorkflowTargetOptions.newBuilder()
115+
.setWorkflowId(untyped.getExecution().getWorkflowId())
116+
.build())
117+
.signal("signal");
118+
}
119+
55120
public static class AwaitingWorkflow implements TestWorkflows.TestWorkflow1 {
56121

57122
@Override

temporal-testing/src/main/java/io/temporal/testing/internal/SDKTestWorkflowRule.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,10 @@ public WorkflowExecutionHistory getExecutionHistory(String workflowId) {
293293
return testWorkflowRule.getWorkflowClient().fetchHistory(workflowId);
294294
}
295295

296+
public WorkflowExecutionHistory getExecutionHistory(String workflowId, String runId) {
297+
return testWorkflowRule.getWorkflowClient().fetchHistory(workflowId, runId);
298+
}
299+
296300
/** Returns list of all events of the given EventType found in the history. */
297301
public List<HistoryEvent> getHistoryEvents(String workflowId, EventType eventType) {
298302
List<HistoryEvent> result = new ArrayList<>();
@@ -318,7 +322,11 @@ public HistoryEvent getHistoryEvent(String workflowId, EventType eventType) {
318322

319323
/** Asserts that an event of the given EventType is found in the history. */
320324
public void assertHistoryEvent(String workflowId, EventType eventType) {
321-
History history = getExecutionHistory(workflowId).getHistory();
325+
assertHistoryEvent(workflowId, null, eventType);
326+
}
327+
328+
public void assertHistoryEvent(String workflowId, String runId, EventType eventType) {
329+
History history = getExecutionHistory(workflowId, runId).getHistory();
322330
for (HistoryEvent event : history.getEventsList()) {
323331
if (eventType == event.getEventType()) {
324332
return;
@@ -329,7 +337,12 @@ public void assertHistoryEvent(String workflowId, EventType eventType) {
329337

330338
/** Asserts that an event of the given EventType is not found in the history. */
331339
public void assertNoHistoryEvent(String workflowId, EventType eventType) {
332-
History history = getExecutionHistory(workflowId).getHistory();
340+
assertNoHistoryEvent(workflowId, null, eventType);
341+
}
342+
343+
/** Asserts that an event of the given EventType is not found in the history. */
344+
public void assertNoHistoryEvent(String workflowId, String runId, EventType eventType) {
345+
History history = getExecutionHistory(workflowId, runId).getHistory();
333346
assertNoHistoryEvent(history, eventType);
334347
}
335348

0 commit comments

Comments
 (0)