|
| 1 | +import asyncio |
| 2 | +from datetime import timedelta |
| 3 | + |
| 4 | + |
| 5 | +from cadence import workflow, Registry |
| 6 | +from cadence.api.v1.history_pb2 import EventFilterType |
| 7 | +from cadence.api.v1.service_workflow_pb2 import ( |
| 8 | + GetWorkflowExecutionHistoryRequest, |
| 9 | + GetWorkflowExecutionHistoryResponse, |
| 10 | +) |
| 11 | +from tests.integration_tests.helper import CadenceHelper, DOMAIN_NAME |
| 12 | + |
| 13 | +reg = Registry() |
| 14 | + |
| 15 | + |
| 16 | +@reg.activity() |
| 17 | +async def echo(message: str) -> str: |
| 18 | + return message |
| 19 | + |
| 20 | + |
| 21 | +@reg.workflow() |
| 22 | +class SingleActivity: |
| 23 | + @workflow.run |
| 24 | + async def run(self, message: str) -> str: |
| 25 | + return await echo.with_options( |
| 26 | + schedule_to_close_timeout=timedelta(seconds=10) |
| 27 | + ).execute(message) |
| 28 | + |
| 29 | + |
| 30 | +@reg.workflow() |
| 31 | +class MultipleActivities: |
| 32 | + @workflow.run |
| 33 | + async def run(self, message: str) -> str: |
| 34 | + await echo.with_options( |
| 35 | + schedule_to_close_timeout=timedelta(seconds=10) |
| 36 | + ).execute("first") |
| 37 | + |
| 38 | + second = await echo.with_options( |
| 39 | + schedule_to_close_timeout=timedelta(seconds=10) |
| 40 | + ).execute(message) |
| 41 | + |
| 42 | + await echo.with_options( |
| 43 | + schedule_to_close_timeout=timedelta(seconds=10) |
| 44 | + ).execute("third") |
| 45 | + |
| 46 | + return second |
| 47 | + |
| 48 | + |
| 49 | +@reg.workflow() |
| 50 | +class ParallelActivities: |
| 51 | + @workflow.run |
| 52 | + async def run(self, message: str) -> str: |
| 53 | + first = echo.with_options( |
| 54 | + schedule_to_close_timeout=timedelta(seconds=10) |
| 55 | + ).execute("first") |
| 56 | + |
| 57 | + second = echo.with_options( |
| 58 | + schedule_to_close_timeout=timedelta(seconds=10) |
| 59 | + ).execute(message) |
| 60 | + |
| 61 | + first_res, second_res = await asyncio.gather( |
| 62 | + first, second, return_exceptions=True |
| 63 | + ) |
| 64 | + |
| 65 | + return second_res |
| 66 | + |
| 67 | + |
| 68 | +async def test_single_activity(helper: CadenceHelper): |
| 69 | + async with helper.worker(reg) as worker: |
| 70 | + execution = await worker.client.start_workflow( |
| 71 | + "SingleActivity", |
| 72 | + "hello world", |
| 73 | + task_list=worker.task_list, |
| 74 | + execution_start_to_close_timeout=timedelta(seconds=10), |
| 75 | + ) |
| 76 | + |
| 77 | + response: GetWorkflowExecutionHistoryResponse = await worker.client.workflow_stub.GetWorkflowExecutionHistory( |
| 78 | + GetWorkflowExecutionHistoryRequest( |
| 79 | + domain=DOMAIN_NAME, |
| 80 | + workflow_execution=execution, |
| 81 | + wait_for_new_event=True, |
| 82 | + history_event_filter_type=EventFilterType.EVENT_FILTER_TYPE_CLOSE_EVENT, |
| 83 | + skip_archival=True, |
| 84 | + ) |
| 85 | + ) |
| 86 | + |
| 87 | + assert ( |
| 88 | + '"hello world"' |
| 89 | + == response.history.events[ |
| 90 | + -1 |
| 91 | + ].workflow_execution_completed_event_attributes.result.data.decode() |
| 92 | + ) |
| 93 | + |
| 94 | + |
| 95 | +async def test_multiple_activities(helper: CadenceHelper): |
| 96 | + async with helper.worker(reg) as worker: |
| 97 | + execution = await worker.client.start_workflow( |
| 98 | + "MultipleActivities", |
| 99 | + "hello world", |
| 100 | + task_list=worker.task_list, |
| 101 | + execution_start_to_close_timeout=timedelta(seconds=10), |
| 102 | + ) |
| 103 | + |
| 104 | + response: GetWorkflowExecutionHistoryResponse = await worker.client.workflow_stub.GetWorkflowExecutionHistory( |
| 105 | + GetWorkflowExecutionHistoryRequest( |
| 106 | + domain=DOMAIN_NAME, |
| 107 | + workflow_execution=execution, |
| 108 | + wait_for_new_event=True, |
| 109 | + history_event_filter_type=EventFilterType.EVENT_FILTER_TYPE_CLOSE_EVENT, |
| 110 | + skip_archival=True, |
| 111 | + ) |
| 112 | + ) |
| 113 | + |
| 114 | + assert ( |
| 115 | + '"hello world"' |
| 116 | + == response.history.events[ |
| 117 | + -1 |
| 118 | + ].workflow_execution_completed_event_attributes.result.data.decode() |
| 119 | + ) |
| 120 | + |
| 121 | + |
| 122 | +async def test_parallel_activities(helper: CadenceHelper): |
| 123 | + async with helper.worker(reg) as worker: |
| 124 | + execution = await worker.client.start_workflow( |
| 125 | + "ParallelActivities", |
| 126 | + "hello world", |
| 127 | + task_list=worker.task_list, |
| 128 | + execution_start_to_close_timeout=timedelta(seconds=10), |
| 129 | + ) |
| 130 | + |
| 131 | + response: GetWorkflowExecutionHistoryResponse = await worker.client.workflow_stub.GetWorkflowExecutionHistory( |
| 132 | + GetWorkflowExecutionHistoryRequest( |
| 133 | + domain=DOMAIN_NAME, |
| 134 | + workflow_execution=execution, |
| 135 | + wait_for_new_event=True, |
| 136 | + history_event_filter_type=EventFilterType.EVENT_FILTER_TYPE_CLOSE_EVENT, |
| 137 | + skip_archival=True, |
| 138 | + ) |
| 139 | + ) |
| 140 | + |
| 141 | + assert ( |
| 142 | + '"hello world"' |
| 143 | + == response.history.events[ |
| 144 | + -1 |
| 145 | + ].workflow_execution_completed_event_attributes.result.data.decode() |
| 146 | + ) |
0 commit comments