|
| 1 | +"""Integration tests for the plugin invocation payload surfaces. |
| 2 | +
|
| 3 | +Exercises `InvocationInfo.execution_input` / `InvocationEndInfo.execution_result` |
| 4 | +through complete `durable_execution()` invocations -- across the decorator, the |
| 5 | +plugin executor, and the invocation hooks -- including a suspend/replay pair |
| 6 | +where the suspending invocation has no result and the replay carries the |
| 7 | +terminal one, and the isolation guarantee between the plugin view and the user |
| 8 | +handler's event. |
| 9 | +""" |
| 10 | + |
| 11 | +from __future__ import annotations |
| 12 | + |
| 13 | +from typing import Any |
| 14 | +from unittest.mock import Mock, patch |
| 15 | + |
| 16 | +from aws_durable_execution_sdk_python.config import Duration |
| 17 | +from aws_durable_execution_sdk_python.context import DurableContext |
| 18 | +from aws_durable_execution_sdk_python.execution import ( |
| 19 | + InvocationStatus, |
| 20 | + durable_execution, |
| 21 | +) |
| 22 | +from aws_durable_execution_sdk_python.lambda_service import ( |
| 23 | + CheckpointOutput, |
| 24 | + CheckpointUpdatedExecutionState, |
| 25 | + Operation, |
| 26 | + OperationStatus, |
| 27 | + OperationType, |
| 28 | +) |
| 29 | +from aws_durable_execution_sdk_python.plugin import DurableInstrumentationPlugin |
| 30 | +from tests.test_helpers import operation_id_sequence |
| 31 | + |
| 32 | + |
| 33 | +class _PayloadRecordingPlugin(DurableInstrumentationPlugin): |
| 34 | + """Records the payload surfaces seen on each invocation hook.""" |
| 35 | + |
| 36 | + def __init__(self) -> None: |
| 37 | + self.starts: list[Any] = [] |
| 38 | + self.ends: list[tuple[str, Any, str | None]] = [] |
| 39 | + |
| 40 | + def on_invocation_start(self, info) -> None: |
| 41 | + self.starts.append(info.execution_input) |
| 42 | + |
| 43 | + def on_invocation_end(self, info) -> None: |
| 44 | + self.ends.append( |
| 45 | + (info.status.value, info.execution_input, info.execution_result) |
| 46 | + ) |
| 47 | + |
| 48 | + |
| 49 | +def _lambda_context() -> Mock: |
| 50 | + ctx = Mock() |
| 51 | + ctx.aws_request_id = "test-request-id" |
| 52 | + ctx.client_context = None |
| 53 | + ctx.identity = None |
| 54 | + ctx._epoch_deadline_time_in_ms = 0 # noqa: SLF001 |
| 55 | + ctx.invoked_function_arn = "test-arn" |
| 56 | + ctx.tenant_id = None |
| 57 | + return ctx |
| 58 | + |
| 59 | + |
| 60 | +def _event(input_payload: str, extra_operations: list[dict] | None = None) -> dict: |
| 61 | + """Build an invocation event carrying the given execution input payload.""" |
| 62 | + execution_operation = { |
| 63 | + "Id": "execution-1", |
| 64 | + "Type": "EXECUTION", |
| 65 | + "Status": "STARTED", |
| 66 | + "ExecutionDetails": {"InputPayload": input_payload}, |
| 67 | + } |
| 68 | + return { |
| 69 | + "DurableExecutionArn": "test-arn/execution-1", |
| 70 | + "CheckpointToken": "test-token", |
| 71 | + "InitialExecutionState": { |
| 72 | + "Operations": [execution_operation, *(extra_operations or [])], |
| 73 | + "NextMarker": "", |
| 74 | + }, |
| 75 | + "LocalRunner": True, |
| 76 | + } |
| 77 | + |
| 78 | + |
| 79 | +def _tracking_checkpoint(initial_operations: list[Operation] | None = None): |
| 80 | + """Checkpoint mock that accumulates operations, as the service would. |
| 81 | +
|
| 82 | + A stub returning an empty execution state is not enough for suspending |
| 83 | + paths: after the WAIT START is checkpointed the SDK re-reads the operation |
| 84 | + from the returned state, so it must be present. |
| 85 | + """ |
| 86 | + operations: list[Operation] = list(initial_operations or []) |
| 87 | + |
| 88 | + def mock_checkpoint( |
| 89 | + durable_execution_arn, # noqa: ARG001 |
| 90 | + checkpoint_token, # noqa: ARG001 |
| 91 | + updates, |
| 92 | + client_token="token", # noqa: S107, ARG001 |
| 93 | + ) -> CheckpointOutput: |
| 94 | + for update in updates: |
| 95 | + operations.append( |
| 96 | + Operation( |
| 97 | + operation_id=update.operation_id, |
| 98 | + operation_type=update.operation_type, |
| 99 | + status=OperationStatus.STARTED, |
| 100 | + parent_id=update.parent_id, |
| 101 | + ) |
| 102 | + ) |
| 103 | + return CheckpointOutput( |
| 104 | + checkpoint_token="new_token", # noqa: S106 |
| 105 | + new_execution_state=CheckpointUpdatedExecutionState( |
| 106 | + operations=operations.copy() |
| 107 | + ), |
| 108 | + ) |
| 109 | + |
| 110 | + return mock_checkpoint |
| 111 | + |
| 112 | + |
| 113 | +def test_plugin_sees_execution_input_and_result_end_to_end(): |
| 114 | + """A completing invocation surfaces the input on both hooks and the result.""" |
| 115 | + plugin = _PayloadRecordingPlugin() |
| 116 | + |
| 117 | + @durable_execution(plugins=[plugin]) |
| 118 | + def my_handler(event: Any, context: DurableContext) -> dict: # noqa: ARG001 |
| 119 | + return {"greeting": f"Hello, {event['name']}!"} |
| 120 | + |
| 121 | + with patch( |
| 122 | + "aws_durable_execution_sdk_python.execution.LambdaClient" |
| 123 | + ) as mock_client_class: |
| 124 | + mock_client = Mock() |
| 125 | + mock_client.checkpoint = _tracking_checkpoint() |
| 126 | + mock_client_class.initialize_client.return_value = mock_client |
| 127 | + |
| 128 | + result = my_handler(_event('{"name": "World"}'), _lambda_context()) |
| 129 | + |
| 130 | + assert result["Status"] == InvocationStatus.SUCCEEDED.value |
| 131 | + |
| 132 | + # Start hook: the deserialized input, not the raw payload string. |
| 133 | + assert plugin.starts == [{"name": "World"}] |
| 134 | + |
| 135 | + # End hook: the same input, plus the serialized result. |
| 136 | + assert len(plugin.ends) == 1 |
| 137 | + status, end_input, end_result = plugin.ends[0] |
| 138 | + assert status == InvocationStatus.SUCCEEDED.value |
| 139 | + assert end_input == {"name": "World"} |
| 140 | + assert end_result == '{"greeting": "Hello, World!"}' |
| 141 | + |
| 142 | + |
| 143 | +def test_plugin_payload_surfaces_on_suspending_invocation(): |
| 144 | + """A suspending invocation carries the input but no execution result.""" |
| 145 | + plugin = _PayloadRecordingPlugin() |
| 146 | + |
| 147 | + @durable_execution(plugins=[plugin]) |
| 148 | + def my_handler(event: Any, context: DurableContext) -> str: |
| 149 | + context.wait(Duration.from_seconds(60)) |
| 150 | + return f"done-{event['name']}" |
| 151 | + |
| 152 | + with patch( |
| 153 | + "aws_durable_execution_sdk_python.execution.LambdaClient" |
| 154 | + ) as mock_client_class: |
| 155 | + mock_client = Mock() |
| 156 | + mock_client.checkpoint = _tracking_checkpoint() |
| 157 | + mock_client_class.initialize_client.return_value = mock_client |
| 158 | + |
| 159 | + result = my_handler(_event('{"name": "World"}'), _lambda_context()) |
| 160 | + |
| 161 | + assert result["Status"] == InvocationStatus.PENDING.value |
| 162 | + assert plugin.starts == [{"name": "World"}] |
| 163 | + |
| 164 | + status, end_input, end_result = plugin.ends[0] |
| 165 | + assert status == InvocationStatus.PENDING.value |
| 166 | + # The input is still reported on a non-terminal invocation-end. |
| 167 | + assert end_input == {"name": "World"} |
| 168 | + # But a suspending invocation produced no execution result. |
| 169 | + assert end_result is None |
| 170 | + |
| 171 | + |
| 172 | +def test_plugin_payload_surfaces_on_replay_invocation(): |
| 173 | + """A replay past a completed wait carries the input and the terminal result.""" |
| 174 | + plugin = _PayloadRecordingPlugin() |
| 175 | + |
| 176 | + @durable_execution(plugins=[plugin]) |
| 177 | + def my_handler(event: Any, context: DurableContext) -> str: |
| 178 | + context.wait(Duration.from_seconds(60)) |
| 179 | + return f"done-{event['name']}" |
| 180 | + |
| 181 | + # The wait completed externally while the execution was suspended. |
| 182 | + completed_wait = { |
| 183 | + "Id": next(operation_id_sequence()), |
| 184 | + "Type": OperationType.WAIT.value, |
| 185 | + "Status": OperationStatus.SUCCEEDED.value, |
| 186 | + } |
| 187 | + |
| 188 | + with patch( |
| 189 | + "aws_durable_execution_sdk_python.execution.LambdaClient" |
| 190 | + ) as mock_client_class: |
| 191 | + mock_client = Mock() |
| 192 | + mock_client.checkpoint = _tracking_checkpoint() |
| 193 | + mock_client_class.initialize_client.return_value = mock_client |
| 194 | + |
| 195 | + result = my_handler( |
| 196 | + _event('{"name": "World"}', extra_operations=[completed_wait]), |
| 197 | + _lambda_context(), |
| 198 | + ) |
| 199 | + |
| 200 | + assert result["Status"] == InvocationStatus.SUCCEEDED.value |
| 201 | + # The input is carried identically across invocations of one execution. |
| 202 | + assert plugin.starts == [{"name": "World"}] |
| 203 | + |
| 204 | + status, end_input, end_result = plugin.ends[0] |
| 205 | + assert status == InvocationStatus.SUCCEEDED.value |
| 206 | + assert end_input == {"name": "World"} |
| 207 | + assert end_result == '"done-World"' |
| 208 | + |
| 209 | + |
| 210 | +def test_plugin_execution_input_is_isolated_from_handler_end_to_end(): |
| 211 | + """The plugin's input view and the handler's event must not alias. |
| 212 | +
|
| 213 | + durable_execution() hands one mutable object to both, so the plugin view is |
| 214 | + deep-copied. Without that a plugin could alter execution behaviour, and a |
| 215 | + handler could retroactively change what the frozen hook info reports. |
| 216 | + """ |
| 217 | + |
| 218 | + class _MutatingPlugin(DurableInstrumentationPlugin): |
| 219 | + def __init__(self) -> None: |
| 220 | + self.end_inputs: list[Any] = [] |
| 221 | + |
| 222 | + def on_invocation_start(self, info) -> None: |
| 223 | + info.execution_input["injected_by_plugin"] = True |
| 224 | + info.execution_input["nested"]["items"].append("from_plugin") |
| 225 | + |
| 226 | + def on_invocation_end(self, info) -> None: |
| 227 | + self.end_inputs.append(info.execution_input) |
| 228 | + |
| 229 | + plugin = _MutatingPlugin() |
| 230 | + handler_saw: dict[str, Any] = {} |
| 231 | + |
| 232 | + @durable_execution(plugins=[plugin]) |
| 233 | + def my_handler(event: Any, context: DurableContext) -> str: # noqa: ARG001 |
| 234 | + handler_saw.update( |
| 235 | + {"top": dict(event), "nested_items": list(event["nested"]["items"])} |
| 236 | + ) |
| 237 | + event["injected_by_handler"] = True |
| 238 | + return "ok" |
| 239 | + |
| 240 | + with patch( |
| 241 | + "aws_durable_execution_sdk_python.execution.LambdaClient" |
| 242 | + ) as mock_client_class: |
| 243 | + mock_client = Mock() |
| 244 | + mock_client.checkpoint = _tracking_checkpoint() |
| 245 | + mock_client_class.initialize_client.return_value = mock_client |
| 246 | + |
| 247 | + my_handler( |
| 248 | + _event('{"name": "World", "nested": {"items": ["original"]}}'), |
| 249 | + _lambda_context(), |
| 250 | + ) |
| 251 | + |
| 252 | + # The plugin's mutations never reached the handler, at any depth. |
| 253 | + assert "injected_by_plugin" not in handler_saw["top"] |
| 254 | + assert handler_saw["nested_items"] == ["original"] |
| 255 | + # The handler's mutation never reached the end hook. |
| 256 | + assert "injected_by_handler" not in plugin.end_inputs[0] |
0 commit comments