Skip to content

Commit c8d58bc

Browse files
committed
fix some workflow instance bugs
Signed-off-by: Shijie Sheng <liouvetren@gmail.com>
1 parent 9d94446 commit c8d58bc

File tree

5 files changed

+60
-18
lines changed

5 files changed

+60
-18
lines changed

cadence/_internal/workflow/workflow_intance.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from asyncio import Task
2-
from typing import Optional
2+
from typing import Any, Optional
33
from cadence._internal.workflow.deterministic_event_loop import DeterministicEventLoop
44
from cadence.api.v1.common_pb2 import Payload
55
from cadence.data_converter import DataConverter
@@ -19,7 +19,8 @@ def __init__(
1919
def start(self, input: Payload):
2020
if self._task is None:
2121
run_method = self._definition.get_run_method(self._instance)
22-
workflow_input = self._data_converter.from_data(input, [])
22+
# TODO handle multiple inputs
23+
workflow_input = self._data_converter.from_data(input, [Any])
2324
self._task = self._loop.create_task(run_method(*workflow_input))
2425

2526
def is_started(self) -> bool:

tests/cadence/_internal/workflow/test_workflow_engine.py

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
#!/usr/bin/env python3
22
from typing import List
33
import pytest
4+
from cadence.api.v1.common_pb2 import Payload
45
from cadence.api.v1.history_pb2 import (
6+
DecisionTaskCompletedEventAttributes,
7+
DecisionTaskScheduledEventAttributes,
8+
DecisionTaskStartedEventAttributes,
59
HistoryEvent,
10+
WorkflowExecutionCompletedEventAttributes,
11+
WorkflowExecutionStartedEventAttributes,
612
)
713
from cadence._internal.workflow.workflow_engine import WorkflowEngine
814
from cadence import workflow
915
from cadence.data_converter import DefaultDataConverter
1016
from cadence.workflow import WorkflowInfo, WorkflowDefinition, WorkflowDefinitionOptions
11-
from tests.cadence._internal.workflow.utils import create_mock_history_event
1217

1318

1419
class TestWorkflow:
@@ -28,15 +33,36 @@ def echo_workflow_definition(self) -> WorkflowDefinition:
2833

2934
@pytest.fixture
3035
def simple_workflow_events(self) -> List[HistoryEvent]:
31-
return create_mock_history_event(
32-
event_types=[
33-
"workflow_execution_started",
34-
"decision_task_scheduled",
35-
"decision_task_started",
36-
"decision_task_completed",
37-
"workflow_execution_completed",
38-
]
39-
)
36+
return [
37+
HistoryEvent(
38+
event_id=1,
39+
workflow_execution_started_event_attributes=WorkflowExecutionStartedEventAttributes(
40+
input=Payload(data=b'"test-input"')
41+
),
42+
),
43+
HistoryEvent(
44+
event_id=2,
45+
decision_task_scheduled_event_attributes=DecisionTaskScheduledEventAttributes(),
46+
),
47+
HistoryEvent(
48+
event_id=3,
49+
decision_task_started_event_attributes=DecisionTaskStartedEventAttributes(
50+
scheduled_event_id=2
51+
),
52+
),
53+
HistoryEvent(
54+
event_id=4,
55+
decision_task_completed_event_attributes=DecisionTaskCompletedEventAttributes(
56+
scheduled_event_id=2,
57+
),
58+
),
59+
HistoryEvent(
60+
event_id=5,
61+
workflow_execution_completed_event_attributes=WorkflowExecutionCompletedEventAttributes(
62+
result=Payload(data=b'"echo: test-input"')
63+
),
64+
),
65+
]
4066

4167
def test_process_simple_workflow(
4268
self,
@@ -45,8 +71,12 @@ def test_process_simple_workflow(
4571
):
4672
workflow_engine = create_workflow_engine(echo_workflow_definition)
4773
decision_result = workflow_engine.process_decision(simple_workflow_events[:3])
48-
4974
assert len(decision_result.decisions) == 1
75+
assert decision_result.decisions[
76+
0
77+
].complete_workflow_execution_decision_attributes.result == Payload(
78+
data=b'"echo: test-input"'
79+
)
5080

5181

5282
def create_workflow_engine(workflow_definition: WorkflowDefinition) -> WorkflowEngine:

tests/cadence/worker/test_decision_task_handler.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,10 @@ def test_initialization(self, mock_client, mock_registry):
8686

8787
@pytest.mark.asyncio
8888
async def test_handle_task_implementation_success(
89-
self, handler: DecisionTaskHandler, sample_decision_task: PollForDecisionTaskResponse, mock_registry
89+
self,
90+
handler: DecisionTaskHandler,
91+
sample_decision_task: PollForDecisionTaskResponse,
92+
mock_registry,
9093
):
9194
"""Test successful decision task handling."""
9295

@@ -117,7 +120,9 @@ async def run(self):
117120
mock_registry.get_workflow.assert_called_once_with("TestWorkflow")
118121

119122
# Verify workflow engine was created and used
120-
mock_engine.process_decision.assert_called_once_with(sample_decision_task.history.events)
123+
mock_engine.process_decision.assert_called_once_with(
124+
sample_decision_task.history.events
125+
)
121126

122127
# Verify response was sent
123128
handler._client.worker_stub.RespondDecisionTaskCompleted.assert_called_once()

tests/cadence/worker/test_decision_task_handler_integration.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,9 @@ async def test_handle_decision_task_success(
124124
await decision_task_handler._handle_task_implementation(decision_task)
125125

126126
# Verify the workflow engine was called
127-
mock_engine.process_decision.assert_called_once_with(decision_task.history.events)
127+
mock_engine.process_decision.assert_called_once_with(
128+
decision_task.history.events
129+
)
128130

129131
# Verify the response was sent
130132
mock_client.worker_stub.RespondDecisionTaskCompleted.assert_called_once()

tests/cadence/worker/test_task_handler_integration.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ async def run(self):
9595

9696
# Verify the complete flow
9797
mock_registry.get_workflow.assert_called_once_with("TestWorkflow")
98-
mock_engine.process_decision.assert_called_once_with(sample_decision_task.history.events)
98+
mock_engine.process_decision.assert_called_once_with(
99+
sample_decision_task.history.events
100+
)
99101
handler._client.worker_stub.RespondDecisionTaskCompleted.assert_called_once()
100102

101103
@pytest.mark.asyncio
@@ -277,7 +279,9 @@ async def run(self):
277279

278280
# Verify engine was created and used
279281
mock_engine_class.assert_called_once()
280-
mock_engine.process_decision.assert_called_once_with(sample_decision_task.history.events)
282+
mock_engine.process_decision.assert_called_once_with(
283+
sample_decision_task.history.events
284+
)
281285

282286
@pytest.mark.asyncio
283287
async def test_error_handling_with_context_cleanup(

0 commit comments

Comments
 (0)