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