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