|
| 1 | +""" |
| 2 | +Unit tests for DurableCallbackHandler |
| 3 | +""" |
| 4 | + |
| 5 | +import threading |
| 6 | +from unittest import TestCase |
| 7 | +from unittest.mock import Mock, patch |
| 8 | + |
| 9 | +from parameterized import parameterized |
| 10 | + |
| 11 | +from samcli.lib.utils.durable_callback_handler import ( |
| 12 | + DurableCallbackHandler, |
| 13 | + CHOICE_SUCCESS, |
| 14 | + CHOICE_FAILURE, |
| 15 | + CHOICE_HEARTBEAT, |
| 16 | + CHOICE_STOP, |
| 17 | +) |
| 18 | + |
| 19 | + |
| 20 | +class TestDurableCallbackHandler(TestCase): |
| 21 | + def setUp(self): |
| 22 | + self.mock_client = Mock() |
| 23 | + self.handler = DurableCallbackHandler(self.mock_client) |
| 24 | + |
| 25 | + def test_init(self): |
| 26 | + """Test handler initializes with client and empty prompted callbacks set""" |
| 27 | + self.assertEqual(self.handler.client, self.mock_client) |
| 28 | + self.assertEqual(self.handler._prompted_callbacks, set()) |
| 29 | + |
| 30 | + @parameterized.expand( |
| 31 | + [ |
| 32 | + ( |
| 33 | + "with_pending_callback", |
| 34 | + { |
| 35 | + "Events": [ |
| 36 | + { |
| 37 | + "Id": 1, |
| 38 | + "EventType": "CallbackStarted", |
| 39 | + "CallbackStartedDetails": {"CallbackId": "callback-123"}, |
| 40 | + }, |
| 41 | + {"Id": 2, "EventType": "StepStarted"}, |
| 42 | + ] |
| 43 | + }, |
| 44 | + "callback-123", |
| 45 | + ), |
| 46 | + ( |
| 47 | + "with_completed_callback", |
| 48 | + { |
| 49 | + "Events": [ |
| 50 | + { |
| 51 | + "Id": 1, |
| 52 | + "EventType": "CallbackStarted", |
| 53 | + "CallbackStartedDetails": {"CallbackId": "callback-123"}, |
| 54 | + }, |
| 55 | + {"Id": 1, "EventType": "CallbackCompleted"}, |
| 56 | + ] |
| 57 | + }, |
| 58 | + None, |
| 59 | + ), |
| 60 | + ( |
| 61 | + "no_callbacks", |
| 62 | + {"Events": [{"Id": 1, "EventType": "StepStarted"}]}, |
| 63 | + None, |
| 64 | + ), |
| 65 | + ] |
| 66 | + ) |
| 67 | + def test_check_for_pending_callbacks(self, name, history_response, expected_callback_id): |
| 68 | + """Test checking for pending callbacks""" |
| 69 | + self.mock_client.get_durable_execution_history.return_value = history_response |
| 70 | + |
| 71 | + callback_id = self.handler.check_for_pending_callbacks("test-arn") |
| 72 | + |
| 73 | + self.assertEqual(callback_id, expected_callback_id) |
| 74 | + self.mock_client.get_durable_execution_history.assert_called_once_with("test-arn") |
| 75 | + |
| 76 | + def test_check_for_pending_callbacks_handles_exception(self): |
| 77 | + """Test that exceptions during callback check are handled gracefully""" |
| 78 | + self.mock_client.get_durable_execution_history.side_effect = Exception("API error") |
| 79 | + |
| 80 | + callback_id = self.handler.check_for_pending_callbacks("test-arn") |
| 81 | + |
| 82 | + self.assertIsNone(callback_id) |
| 83 | + |
| 84 | + @parameterized.expand( |
| 85 | + [ |
| 86 | + ( |
| 87 | + "success", |
| 88 | + CHOICE_SUCCESS, |
| 89 | + ["test result"], |
| 90 | + "send_callback_success", |
| 91 | + {"callback_id": "callback-123", "result": "test result"}, |
| 92 | + True, |
| 93 | + ), |
| 94 | + ( |
| 95 | + "failure", |
| 96 | + CHOICE_FAILURE, |
| 97 | + ["Error occurred", "CustomError"], |
| 98 | + "send_callback_failure", |
| 99 | + {"callback_id": "callback-123", "error_message": "Error occurred", "error_type": "CustomError"}, |
| 100 | + True, |
| 101 | + ), |
| 102 | + ( |
| 103 | + "heartbeat", |
| 104 | + CHOICE_HEARTBEAT, |
| 105 | + [], |
| 106 | + "send_callback_heartbeat", |
| 107 | + {"callback_id": "callback-123"}, |
| 108 | + False, |
| 109 | + ), |
| 110 | + ( |
| 111 | + "stop_execution", |
| 112 | + CHOICE_STOP, |
| 113 | + ["Execution stopped by user", "StopError"], |
| 114 | + "stop_durable_execution", |
| 115 | + { |
| 116 | + "durable_execution_arn": "test-arn", |
| 117 | + "error_message": "Execution stopped by user", |
| 118 | + "error_type": "StopError", |
| 119 | + }, |
| 120 | + True, |
| 121 | + ), |
| 122 | + ] |
| 123 | + ) |
| 124 | + @patch("samcli.lib.utils.durable_callback_handler.click.prompt") |
| 125 | + @patch("samcli.lib.utils.durable_callback_handler.click.echo") |
| 126 | + def test_prompt_callback_response( |
| 127 | + self, |
| 128 | + name, |
| 129 | + choice, |
| 130 | + prompt_responses, |
| 131 | + method_name, |
| 132 | + expected_call_args, |
| 133 | + expected_result, |
| 134 | + mock_echo, |
| 135 | + mock_prompt, |
| 136 | + ): |
| 137 | + """Test prompting for different callback response types""" |
| 138 | + mock_prompt.side_effect = [choice] + prompt_responses |
| 139 | + |
| 140 | + result = self.handler.prompt_callback_response("test-arn", "callback-123") |
| 141 | + |
| 142 | + self.assertEqual(result, expected_result) |
| 143 | + lambda_client_api_call = getattr(self.mock_client, method_name) |
| 144 | + lambda_client_api_call.assert_called_once_with(**expected_call_args) |
| 145 | + self.assertIn("callback-123", self.handler._prompted_callbacks) |
| 146 | + |
| 147 | + def test_prompt_callback_response_only_prompts_once(self): |
| 148 | + """Test that callback is only prompted once per ID""" |
| 149 | + self.handler._prompted_callbacks.add("callback-123") |
| 150 | + |
| 151 | + result = self.handler.prompt_callback_response("test-arn", "callback-123") |
| 152 | + |
| 153 | + self.assertFalse(result) |
| 154 | + self.mock_client.send_callback_success.assert_not_called() |
| 155 | + |
| 156 | + @parameterized.expand( |
| 157 | + [ |
| 158 | + ("before_prompt", True, False), |
| 159 | + ("after_selection", False, True), |
| 160 | + ] |
| 161 | + ) |
| 162 | + @patch("samcli.lib.utils.durable_callback_handler.click.prompt") |
| 163 | + @patch("samcli.lib.utils.durable_callback_handler.click.echo") |
| 164 | + def test_prompt_callback_response_checks_execution_complete( |
| 165 | + self, name, set_before_prompt, set_during_prompt, mock_echo, mock_prompt |
| 166 | + ): |
| 167 | + """Test that prompt respects execution_complete event""" |
| 168 | + execution_complete = threading.Event() |
| 169 | + |
| 170 | + if set_before_prompt: |
| 171 | + execution_complete.set() |
| 172 | + elif set_during_prompt: |
| 173 | + |
| 174 | + def prompt_side_effect(*args, **kwargs): |
| 175 | + execution_complete.set() |
| 176 | + return CHOICE_SUCCESS |
| 177 | + |
| 178 | + mock_prompt.side_effect = prompt_side_effect |
| 179 | + |
| 180 | + result = self.handler.prompt_callback_response("test-arn", "callback-123", execution_complete) |
| 181 | + |
| 182 | + self.assertFalse(result) |
| 183 | + self.mock_client.send_callback_success.assert_not_called() |
| 184 | + if set_before_prompt: |
| 185 | + mock_prompt.assert_not_called() |
| 186 | + |
| 187 | + @patch("samcli.lib.utils.durable_callback_handler.click.prompt") |
| 188 | + @patch("samcli.lib.utils.durable_callback_handler.click.echo") |
| 189 | + def test_prompt_callback_response_handles_exception(self, mock_echo, mock_prompt): |
| 190 | + """Test that exceptions during callback send are handled gracefully""" |
| 191 | + mock_prompt.return_value = CHOICE_HEARTBEAT |
| 192 | + |
| 193 | + self.mock_client.send_callback_heartbeat.side_effect = Exception("API error") |
| 194 | + |
| 195 | + result = self.handler.prompt_callback_response("test-arn", "callback-123") |
| 196 | + |
| 197 | + self.assertFalse(result) |
0 commit comments