Skip to content

Commit 2299189

Browse files
authored
[TestEnv] populate cronschedule in history start event and correct isCron in the list workflow response (#790)
Issue TestEnv doesn't populate cronschedule in history for cron-workflows. This is blocking testing interceptors. Changes populate cronschedule on the start event correct isCron in list workflows
1 parent 4dcdb45 commit 2299189

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed

src/main/java/com/uber/cadence/internal/testservice/StateMachines.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,7 @@ private static void startWorkflow(
549549
if (data.continuedExecutionRunId.isPresent()) {
550550
a.setContinuedExecutionRunId(data.continuedExecutionRunId.get());
551551
}
552+
a.setCronSchedule(data.cronSchedule);
552553
a.setLastCompletionResult(data.lastCompletionResult);
553554
a.setMemo(request.getMemo());
554555
a.setSearchAttributes((request.getSearchAttributes()));

src/main/java/com/uber/cadence/internal/testservice/TestWorkflowStoreImpl.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,8 @@ public List<WorkflowExecutionInfo> listWorkflows(
424424
.setExecution(executionId.getExecution())
425425
.setHistoryLength(history.size())
426426
.setStartTime(history.get(0).getTimestamp())
427+
.setIsCron(
428+
history.get(0).getWorkflowExecutionStartedEventAttributes().isSetCronSchedule())
427429
.setType(
428430
history.get(0).getWorkflowExecutionStartedEventAttributes().getWorkflowType());
429431
result.add(info);
@@ -442,6 +444,8 @@ public List<WorkflowExecutionInfo> listWorkflows(
442444
.setExecution(executionId.getExecution())
443445
.setHistoryLength(history.size())
444446
.setStartTime(history.get(0).getTimestamp())
447+
.setIsCron(
448+
history.get(0).getWorkflowExecutionStartedEventAttributes().isSetCronSchedule())
445449
.setType(
446450
history.get(0).getWorkflowExecutionStartedEventAttributes().getWorkflowType())
447451
.setCloseStatus(
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package com.uber.cadence.workflow;
2+
3+
import static junit.framework.TestCase.assertTrue;
4+
import static org.junit.Assert.*;
5+
6+
import com.uber.cadence.*;
7+
import com.uber.cadence.client.WorkflowClient;
8+
import com.uber.cadence.common.CronSchedule;
9+
import com.uber.cadence.testing.TestWorkflowEnvironment;
10+
import com.uber.cadence.worker.Worker;
11+
import java.time.Duration;
12+
import org.junit.After;
13+
import org.junit.Assert;
14+
import org.junit.Before;
15+
import org.junit.Test;
16+
17+
public class TestEnvironmentWorkflowTest {
18+
static final String TASK_LIST = "tasklist";
19+
static final String CRON_WORKFLOW_ID = "cron_workflow";
20+
21+
private TestWorkflowEnvironment testEnv;
22+
private Worker worker;
23+
private WorkflowClient workflowClient;
24+
25+
public interface CronW {
26+
@WorkflowMethod(
27+
executionStartToCloseTimeoutSeconds = 10,
28+
workflowId = CRON_WORKFLOW_ID,
29+
taskList = TASK_LIST
30+
)
31+
@CronSchedule("* * * * *")
32+
void cron();
33+
}
34+
35+
public static class CronWImpl implements CronW {
36+
@Override
37+
public void cron() {}
38+
}
39+
40+
@Before
41+
public void setUp() {
42+
testEnv = TestWorkflowEnvironment.newInstance();
43+
worker = testEnv.newWorker(TASK_LIST);
44+
worker.registerWorkflowImplementationTypes(CronWImpl.class);
45+
workflowClient = testEnv.newWorkflowClient();
46+
47+
testEnv.start();
48+
}
49+
50+
@After
51+
public void tearDown() {
52+
testEnv.close();
53+
}
54+
55+
@Test
56+
public void testCronWorkflow() {
57+
CronW workflow = workflowClient.newWorkflowStub(CronW.class);
58+
WorkflowExecution execution = WorkflowClient.start(workflow::cron);
59+
assertEquals(CRON_WORKFLOW_ID, execution.getWorkflowId());
60+
61+
// start event should have cron schedule
62+
GetWorkflowExecutionHistoryRequest getRequest =
63+
new GetWorkflowExecutionHistoryRequest()
64+
.setDomain(testEnv.getDomain())
65+
.setExecution(
66+
new WorkflowExecution()
67+
.setWorkflowId(execution.getWorkflowId())
68+
.setRunId(execution.getRunId()))
69+
.setHistoryEventFilterType(HistoryEventFilterType.ALL_EVENT);
70+
try {
71+
GetWorkflowExecutionHistoryResponse response =
72+
workflowClient.getService().GetWorkflowExecutionHistory(getRequest);
73+
assertEquals(
74+
"* * * * *",
75+
response
76+
.getHistory()
77+
.getEvents()
78+
.get(0)
79+
.getWorkflowExecutionStartedEventAttributes()
80+
.getCronSchedule());
81+
} catch (Exception e) {
82+
fail("no exception expected: " + e.getMessage());
83+
}
84+
85+
// sleep for 61 seconds on server and should expect 2 completed runs
86+
testEnv.sleep(Duration.ofSeconds(61));
87+
ListClosedWorkflowExecutionsRequest listRequest =
88+
new ListClosedWorkflowExecutionsRequest()
89+
.setDomain(testEnv.getDomain())
90+
.setExecutionFilter(new WorkflowExecutionFilter().setWorkflowId(CRON_WORKFLOW_ID));
91+
try {
92+
ListClosedWorkflowExecutionsResponse listResponse =
93+
testEnv.getWorkflowService().ListClosedWorkflowExecutions(listRequest);
94+
Assert.assertEquals(2, listResponse.getExecutions().size());
95+
for (WorkflowExecutionInfo e : listResponse.getExecutions()) {
96+
assertTrue(e.isIsCron());
97+
assertEquals(WorkflowExecutionCloseStatus.CONTINUED_AS_NEW, e.getCloseStatus());
98+
}
99+
} catch (Exception e) {
100+
fail("no exception expected: " + e.getMessage());
101+
}
102+
}
103+
}

0 commit comments

Comments
 (0)