|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from collections.abc import AsyncIterator, Generator |
| 4 | +from functools import partial, wraps |
| 5 | +from typing import Any |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +from durable_workflow import Client, Worker, workflow |
| 10 | +from durable_workflow.testing import WorkflowEnvironment |
| 11 | + |
| 12 | + |
| 13 | +class CoroutineWorkflow: |
| 14 | + async def run(self, ctx: Any) -> str: |
| 15 | + return "never executed" |
| 16 | + |
| 17 | + |
| 18 | +class AsyncGeneratorWorkflow: |
| 19 | + async def run(self, ctx: Any) -> AsyncIterator[str]: |
| 20 | + yield "unsupported" |
| 21 | + |
| 22 | + |
| 23 | +class AsyncCallable: |
| 24 | + async def __call__(self, ctx: Any) -> str: |
| 25 | + return "unsupported" |
| 26 | + |
| 27 | + |
| 28 | +class CallableWorkflow: |
| 29 | + run = AsyncCallable() |
| 30 | + |
| 31 | + |
| 32 | +class PartialWorkflow: |
| 33 | + run = staticmethod(partial(CoroutineWorkflow.run, object())) |
| 34 | + |
| 35 | + |
| 36 | +ASYNC_DEFINITIONS = [CoroutineWorkflow, AsyncGeneratorWorkflow, CallableWorkflow, PartialWorkflow] |
| 37 | + |
| 38 | + |
| 39 | +@pytest.mark.parametrize("definition", ASYNC_DEFINITIONS) |
| 40 | +def test_definition_rejects_async_entrypoints(definition: type) -> None: |
| 41 | + with pytest.raises(ValueError, match="unsupported_workflow_entrypoint.*synchronous"): |
| 42 | + workflow.defn(name="invalid-async")(definition) |
| 43 | + |
| 44 | + |
| 45 | +@pytest.mark.parametrize("definition", ASYNC_DEFINITIONS) |
| 46 | +def test_worker_rejects_undecorated_async_entrypoints_before_network(definition: type) -> None: |
| 47 | + with pytest.raises(ValueError, match=definition.__name__): |
| 48 | + Worker(Client("http://127.0.0.1:1"), task_queue="unused", workflows=[definition]) |
| 49 | + |
| 50 | + |
| 51 | +@pytest.mark.parametrize("definition", ASYNC_DEFINITIONS) |
| 52 | +def test_direct_replay_rejects_async_without_creating_a_coroutine(definition: type) -> None: |
| 53 | + with pytest.raises(ValueError, match="unsupported_workflow_entrypoint"): |
| 54 | + workflow.replay(definition, [], []) |
| 55 | + |
| 56 | + |
| 57 | +def test_test_environment_revalidates_entrypoint_after_decoration() -> None: |
| 58 | + @workflow.defn(name="changed-entrypoint") |
| 59 | + class ChangedWorkflow: |
| 60 | + def run(self, ctx: Any) -> str: |
| 61 | + return "valid" |
| 62 | + |
| 63 | + ChangedWorkflow.run = CoroutineWorkflow.run # type: ignore[assignment] |
| 64 | + with pytest.raises(ValueError, match="changed-entrypoint"): |
| 65 | + WorkflowEnvironment().register_workflow(ChangedWorkflow) |
| 66 | + |
| 67 | + |
| 68 | +def test_wrapped_and_inherited_async_entrypoints_are_rejected() -> None: |
| 69 | + class InheritedWorkflow(CoroutineWorkflow): |
| 70 | + pass |
| 71 | + |
| 72 | + class WrappedWorkflow: |
| 73 | + @wraps(CoroutineWorkflow.run) |
| 74 | + def run(self, ctx: Any) -> Any: |
| 75 | + return CoroutineWorkflow.run(self, ctx) |
| 76 | + |
| 77 | + for definition in [InheritedWorkflow, WrappedWorkflow]: |
| 78 | + with pytest.raises(ValueError, match="unsupported_workflow_entrypoint"): |
| 79 | + workflow.replay(definition, [], []) |
| 80 | + |
| 81 | + |
| 82 | +@pytest.mark.parametrize("generator", [False, True]) |
| 83 | +def test_runtime_awaitable_result_is_not_completed(generator: bool) -> None: |
| 84 | + class AwaitableResultWorkflow: |
| 85 | + def run(self, ctx: Any) -> Any: |
| 86 | + return CoroutineWorkflow.run(self, ctx) |
| 87 | + |
| 88 | + class GeneratorAwaitableResultWorkflow: |
| 89 | + def run(self, ctx: Any) -> Generator[Any, Any, Any]: |
| 90 | + if False: |
| 91 | + yield None |
| 92 | + return CoroutineWorkflow.run(self, ctx) |
| 93 | + |
| 94 | + definition = GeneratorAwaitableResultWorkflow if generator else AwaitableResultWorkflow |
| 95 | + with pytest.raises(ValueError, match="unsupported_workflow_result"): |
| 96 | + workflow.replay(definition, [], []) |
| 97 | + |
| 98 | + |
| 99 | +def test_synchronous_and_generator_workflows_remain_supported() -> None: |
| 100 | + @workflow.defn(name="valid-sync") |
| 101 | + class SynchronousWorkflow: |
| 102 | + def run(self, ctx: Any) -> str: |
| 103 | + return "done" |
| 104 | + |
| 105 | + @workflow.defn(name="valid-generator") |
| 106 | + class GeneratorWorkflow: |
| 107 | + def run(self, ctx: Any) -> Generator[Any, Any, str]: |
| 108 | + if False: |
| 109 | + yield None |
| 110 | + return "done" |
| 111 | + |
| 112 | + Worker(Client("http://127.0.0.1:1"), task_queue="unused", workflows=[SynchronousWorkflow, GeneratorWorkflow]) |
| 113 | + environment = WorkflowEnvironment() |
| 114 | + for definition in [SynchronousWorkflow, GeneratorWorkflow]: |
| 115 | + assert environment.execute_workflow(definition) == "done" |
| 116 | + assert workflow.replay(definition, [], []).commands[0].result == "done" # type: ignore[union-attr] |
0 commit comments