Skip to content

Commit a742092

Browse files
add continue as new test
1 parent 6bcb938 commit a742092

1 file changed

Lines changed: 106 additions & 0 deletions

File tree

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package io.temporal.workflow.versionTests;
2+
3+
import static io.temporal.internal.history.VersionMarkerUtils.TEMPORAL_CHANGE_VERSION;
4+
import static org.junit.Assert.assertEquals;
5+
6+
import io.temporal.api.common.v1.WorkflowExecution;
7+
import io.temporal.api.enums.v1.EventType;
8+
import io.temporal.api.history.v1.HistoryEvent;
9+
import io.temporal.client.WorkflowClient;
10+
import io.temporal.client.WorkflowStub;
11+
import io.temporal.common.WorkflowExecutionHistory;
12+
import io.temporal.internal.history.VersionMarkerUtils;
13+
import io.temporal.testing.internal.SDKTestWorkflowRule;
14+
import io.temporal.workflow.Workflow;
15+
import io.temporal.workflow.WorkflowInterface;
16+
import io.temporal.workflow.WorkflowMethod;
17+
import java.util.Collections;
18+
import java.util.List;
19+
import java.util.Optional;
20+
import java.util.stream.Collectors;
21+
import org.junit.Rule;
22+
import org.junit.Test;
23+
24+
public class GetVersionContinueAsNewTest extends BaseVersionTest {
25+
public GetVersionContinueAsNewTest(boolean setVersioningFlag, boolean upsertVersioningSA) {
26+
super(setVersioningFlag, upsertVersioningSA);
27+
}
28+
29+
@Rule
30+
public SDKTestWorkflowRule testWorkflowRule =
31+
SDKTestWorkflowRule.newBuilder()
32+
.setWorkflowTypes(getDefaultWorkflowImplementationOptions(), TestWorkflowImpl.class)
33+
.setUseExternalService(true)
34+
.build();
35+
36+
@Test
37+
public void versionNotCarriedOverOnContinueAsNew() {
38+
TestWorkflow workflow = testWorkflowRule.newWorkflowStubTimeoutOptions(TestWorkflow.class);
39+
// Start workflow to obtain the first run id
40+
WorkflowExecution run1 = WorkflowClient.start(workflow::execute, 2);
41+
// Wait for workflow completion
42+
WorkflowStub untyped = WorkflowStub.fromTyped(workflow);
43+
untyped.getResult(Void.class);
44+
// Point the untyped stub to the latest execution
45+
untyped =
46+
testWorkflowRule
47+
.getWorkflowClient()
48+
.newUntypedWorkflowStub(
49+
run1.getWorkflowId(), Optional.of(run1.getRunId()), Optional.empty());
50+
WorkflowStub latestUntyped =
51+
testWorkflowRule.getWorkflowClient().newUntypedWorkflowStub(run1.getWorkflowId());
52+
WorkflowExecution run2 = latestUntyped.getExecution();
53+
54+
WorkflowExecutionHistory history1 =
55+
testWorkflowRule.getWorkflowClient().fetchHistory(run1.getWorkflowId(), run1.getRunId());
56+
List<HistoryEvent> markers1 =
57+
history1.getEvents().stream()
58+
.filter(e -> e.getEventType() == EventType.EVENT_TYPE_MARKER_RECORDED)
59+
.collect(Collectors.toList());
60+
assertEquals(1, markers1.size());
61+
assertEquals(
62+
2,
63+
VersionMarkerUtils.getVersion(markers1.get(0).getMarkerRecordedEventAttributes())
64+
.intValue());
65+
if (upsertVersioningSA) {
66+
assertEquals(
67+
Collections.singletonList("change-2"),
68+
untyped.describe().getTypedSearchAttributes().get(TEMPORAL_CHANGE_VERSION));
69+
}
70+
71+
WorkflowExecutionHistory history2 =
72+
testWorkflowRule.getWorkflowClient().fetchHistory(run2.getWorkflowId(), run2.getRunId());
73+
List<HistoryEvent> markers2 =
74+
history2.getEvents().stream()
75+
.filter(e -> e.getEventType() == EventType.EVENT_TYPE_MARKER_RECORDED)
76+
.collect(Collectors.toList());
77+
assertEquals(1, markers2.size());
78+
assertEquals(
79+
1,
80+
VersionMarkerUtils.getVersion(markers2.get(0).getMarkerRecordedEventAttributes())
81+
.intValue());
82+
if (upsertVersioningSA) {
83+
assertEquals(
84+
Collections.singletonList("change-1"),
85+
latestUntyped.describe().getTypedSearchAttributes().get(TEMPORAL_CHANGE_VERSION));
86+
}
87+
}
88+
89+
@WorkflowInterface
90+
public interface TestWorkflow {
91+
@WorkflowMethod
92+
void execute(int runs);
93+
}
94+
95+
public static class TestWorkflowImpl implements TestWorkflow {
96+
@Override
97+
public void execute(int runs) {
98+
int version = Workflow.getVersion("change", Workflow.DEFAULT_VERSION, runs);
99+
assertEquals(runs, version);
100+
if (runs > 1) {
101+
TestWorkflow next = Workflow.newContinueAsNewStub(TestWorkflow.class);
102+
next.execute(runs - 1);
103+
}
104+
}
105+
}
106+
}

0 commit comments

Comments
 (0)