|
1 | 1 | """Synthetic provider/acknowledgement boundaries; no Server or worker invocation.""" |
2 | 2 | from __future__ import annotations |
3 | 3 |
|
4 | | -from dataclasses import replace |
| 4 | +import pickle |
| 5 | +from dataclasses import asdict, replace |
5 | 6 | from pathlib import Path |
6 | 7 | from typing import Any |
| 8 | +from unittest.mock import AsyncMock |
7 | 9 |
|
8 | 10 | import pytest |
9 | | -from examples.checkpointed_batch import FAILURES, Batch, Progress, ReceiptStore, process_batch |
| 11 | +from examples.checkpointed_batch import ACTIVITY, FAILURES, Batch, BatchActivity, Progress, ReceiptStore, process_batch |
10 | 12 |
|
| 13 | +from durable_workflow import ActivityInfo, Client, Worker |
| 14 | +from durable_workflow._activity_process import CallbackInvocation |
11 | 15 | from durable_workflow.errors import NonRetryableError |
12 | 16 |
|
13 | 17 |
|
@@ -134,3 +138,37 @@ def test_existing_directory_and_partial_receipt_are_preserved(tmp_path: Path) -> |
134 | 138 | with pytest.raises(NonRetryableError, match="partial"): |
135 | 139 | store.commit_or_reconcile(business_id, fingerprint, 0, items[0]) |
136 | 140 | assert path.read_bytes() == b"partial" |
| 141 | + |
| 142 | + |
| 143 | +def test_actual_handler_survives_cooperative_preflight_and_callback_serialization(tmp_path: Path) -> None: |
| 144 | + store = ReceiptStore.create(tmp_path / "provider") |
| 145 | + handler = BatchActivity(store).execute |
| 146 | + worker = Worker(AsyncMock(spec=Client), task_queue="queue", activities=[handler]) |
| 147 | + worker._cooperative_cancellation_supported = True |
| 148 | + worker._validate_cooperative_activity_handlers() |
| 149 | + assert worker.activities[ACTIVITY] == handler |
| 150 | + invocation = CallbackInvocation( |
| 151 | + handler=worker.activities[ACTIVITY], args=(asdict(batch()),), |
| 152 | + info=ActivityInfo("task", ACTIVITY, "attempt", 2, "queue", "worker"), task={}, |
| 153 | + ) |
| 154 | + restored = pickle.loads(invocation.serialize()) |
| 155 | + assert isinstance(restored.handler.__self__, BatchActivity) |
| 156 | + assert restored.handler.__self__.store == store |
| 157 | + assert restored.args == (asdict(batch()),) |
| 158 | + assert inventory(store) == [] |
| 159 | + |
| 160 | + |
| 161 | +def test_nested_handler_is_refused_by_the_same_preflight_and_serializer() -> None: |
| 162 | + async def legacy_nested_handler(_body: dict[str, Any]) -> None: |
| 163 | + pass |
| 164 | + |
| 165 | + worker = Worker(AsyncMock(spec=Client), task_queue="queue", activities=[legacy_nested_handler]) |
| 166 | + worker._cooperative_cancellation_supported = True |
| 167 | + with pytest.raises(RuntimeError, match="spawn-compatible"): |
| 168 | + worker._validate_cooperative_activity_handlers() |
| 169 | + invocation = CallbackInvocation( |
| 170 | + handler=legacy_nested_handler, args=(), |
| 171 | + info=ActivityInfo("task", ACTIVITY, "attempt", 2, "queue", "worker"), task={}, |
| 172 | + ) |
| 173 | + with pytest.raises(ValueError, match="spawn-compatible"): |
| 174 | + invocation.serialize() |
0 commit comments