|
| 1 | +import pytest |
| 2 | +from unittest.mock import Mock, AsyncMock |
| 3 | + |
| 4 | +from cadence.client import Client |
| 5 | +from cadence.api.v1.common_pb2 import WorkflowExecution |
| 6 | +from cadence.api.v1.history_pb2 import HistoryEvent, History |
| 7 | +from cadence.api.v1.service_worker_pb2 import PollForDecisionTaskResponse |
| 8 | +from cadence.api.v1.service_workflow_pb2 import GetWorkflowExecutionHistoryResponse |
| 9 | +from cadence._internal.workflow.history_event_iterator import iterate_history_events |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture |
| 13 | +def mock_client(): |
| 14 | + """Create a mock client with workflow_stub.""" |
| 15 | + client = Mock(spec=Client) |
| 16 | + client.workflow_stub = AsyncMock() |
| 17 | + client.domain = "test-domain" |
| 18 | + return client |
| 19 | + |
| 20 | + |
| 21 | +@pytest.fixture |
| 22 | +def mock_workflow_execution(): |
| 23 | + """Create a mock workflow execution.""" |
| 24 | + return WorkflowExecution( |
| 25 | + workflow_id="test-workflow-id", |
| 26 | + run_id="test-run-id" |
| 27 | + ) |
| 28 | + |
| 29 | + |
| 30 | +def create_history_event(event_id: int) -> HistoryEvent: |
| 31 | + return HistoryEvent(event_id=event_id) |
| 32 | + |
| 33 | + |
| 34 | +async def test_iterate_history_events_single_page_no_next_token(mock_client, mock_workflow_execution): |
| 35 | + """Test iterating over a single page of events with no next page token.""" |
| 36 | + # Create test events |
| 37 | + events = [ |
| 38 | + create_history_event(1), |
| 39 | + create_history_event(2), |
| 40 | + create_history_event(3) |
| 41 | + ] |
| 42 | + |
| 43 | + # Create decision task response with events but no next page token |
| 44 | + decision_task = PollForDecisionTaskResponse( |
| 45 | + history=History(events=events), |
| 46 | + next_page_token=b"", # Empty token means no more pages |
| 47 | + workflow_execution=mock_workflow_execution |
| 48 | + ) |
| 49 | + |
| 50 | + # Iterate and collect events |
| 51 | + result_events = [e async for e in iterate_history_events(decision_task, mock_client)] |
| 52 | + |
| 53 | + # Verify all events were returned |
| 54 | + assert len(result_events) == 3 |
| 55 | + assert result_events[0].event_id == 1 |
| 56 | + assert result_events[1].event_id == 2 |
| 57 | + assert result_events[2].event_id == 3 |
| 58 | + |
| 59 | + # Verify no additional API calls were made |
| 60 | + mock_client.workflow_stub.GetWorkflowExecutionHistory.assert_not_called() |
| 61 | + |
| 62 | + |
| 63 | +async def test_iterate_history_events_empty_events(mock_client, mock_workflow_execution): |
| 64 | + """Test iterating over empty events list.""" |
| 65 | + # Create decision task response with no events |
| 66 | + decision_task = PollForDecisionTaskResponse( |
| 67 | + history=History(events=[]), |
| 68 | + next_page_token=b"", |
| 69 | + workflow_execution=mock_workflow_execution |
| 70 | + ) |
| 71 | + |
| 72 | + # Iterate and collect events |
| 73 | + result_events = [e async for e in iterate_history_events(decision_task, mock_client)] |
| 74 | + |
| 75 | + # Verify no events were returned |
| 76 | + assert len(result_events) == 0 |
| 77 | + |
| 78 | + # Verify no additional API calls were made |
| 79 | + mock_client.workflow_stub.GetWorkflowExecutionHistory.assert_not_called() |
| 80 | + |
| 81 | +async def test_iterate_history_events_multiple_pages(mock_client, mock_workflow_execution): |
| 82 | + """Test iterating over multiple pages of events.""" |
| 83 | + |
| 84 | + # Create decision task response with first page and next page token |
| 85 | + decision_task = PollForDecisionTaskResponse( |
| 86 | + history=History(events=[ |
| 87 | + create_history_event(1), |
| 88 | + create_history_event(2) |
| 89 | + ]), |
| 90 | + next_page_token=b"page2_token", |
| 91 | + workflow_execution=mock_workflow_execution |
| 92 | + ) |
| 93 | + |
| 94 | + # Mock the subsequent API calls |
| 95 | + second_response = GetWorkflowExecutionHistoryResponse( |
| 96 | + history=History(events=[ |
| 97 | + create_history_event(3), |
| 98 | + create_history_event(4) |
| 99 | + ]), |
| 100 | + next_page_token=b"page3_token" |
| 101 | + ) |
| 102 | + |
| 103 | + third_response = GetWorkflowExecutionHistoryResponse( |
| 104 | + history=History(events=[ |
| 105 | + create_history_event(5) |
| 106 | + ]), |
| 107 | + next_page_token=b"" # No more pages |
| 108 | + ) |
| 109 | + |
| 110 | + # Configure mock to return responses in sequence |
| 111 | + mock_client.workflow_stub.GetWorkflowExecutionHistory.side_effect = [ |
| 112 | + second_response, |
| 113 | + third_response |
| 114 | + ] |
| 115 | + |
| 116 | + # Iterate and collect events |
| 117 | + result_events = [e async for e in iterate_history_events(decision_task, mock_client)] |
| 118 | + |
| 119 | + # Verify all events from all pages were returned |
| 120 | + assert len(result_events) == 5 |
| 121 | + assert result_events[0].event_id == 1 |
| 122 | + assert result_events[1].event_id == 2 |
| 123 | + assert result_events[2].event_id == 3 |
| 124 | + assert result_events[3].event_id == 4 |
| 125 | + assert result_events[4].event_id == 5 |
| 126 | + |
| 127 | + # Verify correct API calls were made |
| 128 | + assert mock_client.workflow_stub.GetWorkflowExecutionHistory.call_count == 2 |
| 129 | + |
| 130 | + # Verify first API call |
| 131 | + first_call = mock_client.workflow_stub.GetWorkflowExecutionHistory.call_args_list[0] |
| 132 | + first_request = first_call[0][0] |
| 133 | + assert first_request.domain == "test-domain" |
| 134 | + assert first_request.workflow_execution == mock_workflow_execution |
| 135 | + assert first_request.next_page_token == b"page2_token" |
| 136 | + assert first_request.page_size == 1000 |
| 137 | + |
| 138 | + # Verify second API call |
| 139 | + second_call = mock_client.workflow_stub.GetWorkflowExecutionHistory.call_args_list[1] |
| 140 | + second_request = second_call[0][0] |
| 141 | + assert second_request.domain == "test-domain" |
| 142 | + assert second_request.workflow_execution == mock_workflow_execution |
| 143 | + assert second_request.next_page_token == b"page3_token" |
| 144 | + assert second_request.page_size == 1000 |
| 145 | + |
| 146 | +async def test_iterate_history_events_single_page_with_next_token_then_empty(mock_client, mock_workflow_execution): |
| 147 | + """Test case where first page has next token but second page is empty.""" |
| 148 | + # Create first page of events |
| 149 | + first_page_events = [ |
| 150 | + create_history_event(1), |
| 151 | + create_history_event(2) |
| 152 | + ] |
| 153 | + |
| 154 | + # Create decision task response with first page and next page token |
| 155 | + decision_task = PollForDecisionTaskResponse( |
| 156 | + history=History(events=first_page_events), |
| 157 | + next_page_token=b"page2_token", |
| 158 | + workflow_execution=mock_workflow_execution |
| 159 | + ) |
| 160 | + |
| 161 | + # Mock the second API call to return empty page |
| 162 | + second_response = GetWorkflowExecutionHistoryResponse( |
| 163 | + history=History(events=[]), |
| 164 | + next_page_token=b"" # No more pages |
| 165 | + ) |
| 166 | + |
| 167 | + mock_client.workflow_stub.GetWorkflowExecutionHistory.return_value = second_response |
| 168 | + |
| 169 | + # Iterate and collect events |
| 170 | + result_events = [e async for e in iterate_history_events(decision_task, mock_client)] |
| 171 | + |
| 172 | + # Verify only first page events were returned |
| 173 | + assert len(result_events) == 2 |
| 174 | + assert result_events[0].event_id == 1 |
| 175 | + assert result_events[1].event_id == 2 |
| 176 | + |
| 177 | + # Verify one API call was made |
| 178 | + assert mock_client.workflow_stub.GetWorkflowExecutionHistory.call_count == 1 |
0 commit comments