|
7 | 7 | import inspect |
8 | 8 | import json |
9 | 9 | import logging |
| 10 | +import subprocess |
| 11 | +import sys |
10 | 12 | from collections.abc import AsyncGenerator, Generator |
11 | 13 | from datetime import datetime, timedelta |
12 | | -from functools import partial, wraps |
| 14 | +from functools import partial, update_wrapper, wraps |
13 | 15 | from typing import Any |
14 | 16 |
|
15 | 17 | import pytest |
16 | 18 |
|
17 | 19 | from durabletask import entities, task, worker |
18 | | -from durabletask.internal import helpers |
| 20 | +from durabletask.internal import helpers, shared |
19 | 21 | from durabletask.internal import orchestrator_service_pb2 as pb |
20 | 22 | from durabletask.serialization import JsonDataConverter |
21 | 23 |
|
@@ -92,6 +94,75 @@ async def __call__( |
92 | 94 | assert json.loads(complete.result.value) == 4 |
93 | 95 |
|
94 | 96 |
|
| 97 | +@pytest.mark.parametrize("shape", [ |
| 98 | + "function", "partial", "bound-method", "callable", "partial-callable", |
| 99 | + "async-callable", "nested-callable", |
| 100 | +]) |
| 101 | +def test_async_orchestrator_is_recognized_before_its_synchronous_wrapped_function(shape: str) -> None: |
| 102 | + def base(ctx: task.OrchestrationContext, value: int) -> int: |
| 103 | + return value + 1 |
| 104 | + |
| 105 | + @wraps(base) |
| 106 | + async def asynchronous(ctx: task.OrchestrationContext, value: int) -> int: |
| 107 | + return base(ctx, value) |
| 108 | + |
| 109 | + @wraps(asynchronous) |
| 110 | + def traced(ctx: task.OrchestrationContext, value: int) -> Any: |
| 111 | + return asynchronous(ctx, value) |
| 112 | + |
| 113 | + class DecoratedCallable: |
| 114 | + def base(self, ctx: task.OrchestrationContext, value: int) -> int: |
| 115 | + return value + 1 |
| 116 | + |
| 117 | + async def asynchronous(self, ctx: task.OrchestrationContext, value: int) -> int: |
| 118 | + return self.base(ctx, value) |
| 119 | + |
| 120 | + def __call__(self, ctx: task.OrchestrationContext, value: int) -> Any: |
| 121 | + return self.asynchronous(ctx, value) |
| 122 | + |
| 123 | + update_wrapper(DecoratedCallable.asynchronous, DecoratedCallable.base) |
| 124 | + update_wrapper(DecoratedCallable.__call__, DecoratedCallable.asynchronous) |
| 125 | + |
| 126 | + class AsyncCallable(DecoratedCallable): |
| 127 | + async def __call__(self, ctx: task.OrchestrationContext, value: int) -> int: |
| 128 | + return self.base(ctx, value) |
| 129 | + |
| 130 | + update_wrapper(AsyncCallable.__call__, AsyncCallable.base) |
| 131 | + |
| 132 | + def wrap_callable(target: task.Orchestrator[Any, Any]) -> task.Orchestrator[Any, Any]: |
| 133 | + class Wrapper: |
| 134 | + def __call__(self, ctx: task.OrchestrationContext, value: int) -> Any: |
| 135 | + return target(ctx, value) |
| 136 | + |
| 137 | + update_wrapper(Wrapper.__call__, target) |
| 138 | + return Wrapper() |
| 139 | + |
| 140 | + decorated = DecoratedCallable() |
| 141 | + nested: task.Orchestrator[Any, Any] = traced |
| 142 | + # Each __call__ lookup produces a temporary bound method. |
| 143 | + for _ in range(32): |
| 144 | + nested = wrap_callable(nested) |
| 145 | + variants = { |
| 146 | + "function": traced, |
| 147 | + "partial": partial(traced), |
| 148 | + "bound-method": decorated.__call__, |
| 149 | + "callable": decorated, |
| 150 | + "partial-callable": partial(decorated), |
| 151 | + "async-callable": AsyncCallable(), |
| 152 | + "nested-callable": nested, |
| 153 | + } |
| 154 | + fn = variants[shape] |
| 155 | + assert shared.is_async_callable(fn) |
| 156 | + complete = _completion(_execute(fn, [], _start())) |
| 157 | + assert complete.orchestrationStatus == pb.ORCHESTRATION_STATUS_COMPLETED |
| 158 | + assert json.loads(complete.result.value) == 4 |
| 159 | + |
| 160 | + |
| 161 | +@pytest.mark.parametrize("fn", [int, len, list[int].append, object().__str__]) |
| 162 | +def test_synchronous_builtin_callables_are_not_classified_as_async(fn: Any) -> None: |
| 163 | + assert not shared.is_async_callable(fn) |
| 164 | + |
| 165 | + |
95 | 166 | def test_unmarked_synchronous_wrapper_returning_coroutine_is_rejected_and_closed() -> None: |
96 | 167 | created: list[Any] = [] |
97 | 168 |
|
@@ -150,6 +221,47 @@ def wrapper(ctx: task.OrchestrationContext, value: int) -> int: |
150 | 221 | assert "wrapper loop" in complete.failureDetails.errorMessage |
151 | 222 |
|
152 | 223 |
|
| 224 | +def _assert_mixed_wrapper_cycle_fails(shape: str) -> None: |
| 225 | + def wrapper(ctx: task.OrchestrationContext, value: int) -> int: |
| 226 | + return value |
| 227 | + |
| 228 | + if shape == "partial": |
| 229 | + wrapper.__wrapped__ = partial(wrapper) # type: ignore[attr-defined] |
| 230 | + else: |
| 231 | + class CallableWrapper: |
| 232 | + def __call__(self, ctx: task.OrchestrationContext, value: int) -> int: |
| 233 | + return value |
| 234 | + |
| 235 | + wrapper.__wrapped__ = CallableWrapper() # type: ignore[attr-defined] |
| 236 | + CallableWrapper.__call__.__wrapped__ = partial(wrapper) # type: ignore[attr-defined] |
| 237 | + |
| 238 | + # No input: dispatch must detect the cycle without relying on type discovery. |
| 239 | + history = [ |
| 240 | + helpers.new_orchestrator_started_event(NOW), |
| 241 | + helpers.new_execution_started_event("test", INSTANCE_ID), |
| 242 | + ] |
| 243 | + complete = _completion(_execute(wrapper, [], history)) |
| 244 | + assert complete.orchestrationStatus == pb.ORCHESTRATION_STATUS_FAILED |
| 245 | + assert complete.failureDetails.errorType == "builtins.ValueError" |
| 246 | + assert "wrapper loop" in complete.failureDetails.errorMessage |
| 247 | + with pytest.raises(ValueError, match="wrapper loop"): |
| 248 | + shared.is_async_callable(wrapper) |
| 249 | + |
| 250 | + |
| 251 | +@pytest.mark.parametrize("shape", ["partial", "callable-partial"]) |
| 252 | +def test_mixed_wrapper_cycle_fails_within_bounded_worker_dispatch(shape: str) -> None: |
| 253 | + result = subprocess.run( |
| 254 | + [ |
| 255 | + sys.executable, "-c", |
| 256 | + "import runpy, sys; " |
| 257 | + "runpy.run_path(sys.argv[1])[sys.argv[2]](sys.argv[3])", |
| 258 | + __file__, _assert_mixed_wrapper_cycle_fails.__name__, shape, |
| 259 | + ], |
| 260 | + capture_output=True, text=True, timeout=10, |
| 261 | + ) |
| 262 | + assert result.returncode == 0, result.stdout + result.stderr |
| 263 | + |
| 264 | + |
153 | 265 | def test_await_is_concrete_and_completed_tasks_do_not_suspend() -> None: |
154 | 266 | assert "__await__" not in task.Task.__abstractmethods__ |
155 | 267 |
|
|
0 commit comments