|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import json |
| 4 | +from pathlib import Path |
| 5 | +from typing import Any |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +from durable_workflow import Replayer, workflow |
| 10 | +from durable_workflow.workflow import CompleteWorkflow, ScheduleActivity, WorkflowContext |
| 11 | + |
| 12 | +FIXTURE_PATH = Path(__file__).parent / "fixtures" / "golden_history" / "python-sdk-v0.json" |
| 13 | + |
| 14 | + |
| 15 | +@workflow.defn(name="golden.single-activity") |
| 16 | +class GoldenSingleActivityWorkflow: |
| 17 | + def run(self, ctx: WorkflowContext, name: str): # type: ignore[no-untyped-def] |
| 18 | + return (yield ctx.schedule_activity("golden.greet", [name])) |
| 19 | + |
| 20 | + |
| 21 | +@workflow.defn(name="golden.signal-wait") |
| 22 | +class GoldenSignalWaitWorkflow: |
| 23 | + def __init__(self) -> None: |
| 24 | + self.approved_by: str | None = None |
| 25 | + |
| 26 | + @workflow.signal("approve") |
| 27 | + def approve(self, approved_by: str) -> None: |
| 28 | + self.approved_by = approved_by |
| 29 | + |
| 30 | + def run(self, ctx: WorkflowContext): # type: ignore[no-untyped-def] |
| 31 | + satisfied = yield ctx.wait_condition( |
| 32 | + lambda: self.approved_by is not None, |
| 33 | + key="approval", |
| 34 | + timeout=30, |
| 35 | + ) |
| 36 | + if not satisfied: |
| 37 | + return "timed-out" |
| 38 | + |
| 39 | + activity_result = yield ctx.schedule_activity("golden.notify", [self.approved_by]) |
| 40 | + return { |
| 41 | + "approved_by": self.approved_by, |
| 42 | + "activity_result": activity_result, |
| 43 | + } |
| 44 | + |
| 45 | + |
| 46 | +@workflow.defn(name="golden.timeout-wait") |
| 47 | +class GoldenTimeoutWaitWorkflow: |
| 48 | + def run(self, ctx: WorkflowContext): # type: ignore[no-untyped-def] |
| 49 | + if not (yield ctx.wait_condition(lambda: False, key="approval", timeout=5)): |
| 50 | + return "timed-out" |
| 51 | + |
| 52 | + return "satisfied" |
| 53 | + |
| 54 | + |
| 55 | +@workflow.defn(name="golden.version-marker") |
| 56 | +class GoldenVersionMarkerWorkflow: |
| 57 | + def run(self, ctx: WorkflowContext): # type: ignore[no-untyped-def] |
| 58 | + version = yield ctx.get_version("golden-version", 1, 2) |
| 59 | + activity = "golden.version-new-path" if version >= 2 else "golden.version-old-path" |
| 60 | + return (yield ctx.schedule_activity(activity, [])) |
| 61 | + |
| 62 | + |
| 63 | +def _golden_cases() -> list[dict[str, Any]]: |
| 64 | + with FIXTURE_PATH.open() as handle: |
| 65 | + cases = json.load(handle) |
| 66 | + |
| 67 | + assert isinstance(cases, list) |
| 68 | + return cases |
| 69 | + |
| 70 | + |
| 71 | +@pytest.mark.parametrize("case", _golden_cases(), ids=lambda case: str(case["name"])) |
| 72 | +def test_golden_history_replay_contract(case: dict[str, Any]) -> None: |
| 73 | + replayer = Replayer( |
| 74 | + workflows=[ |
| 75 | + GoldenSingleActivityWorkflow, |
| 76 | + GoldenSignalWaitWorkflow, |
| 77 | + GoldenTimeoutWaitWorkflow, |
| 78 | + GoldenVersionMarkerWorkflow, |
| 79 | + ], |
| 80 | + ) |
| 81 | + |
| 82 | + outcome = replayer.replay( |
| 83 | + case["history"], |
| 84 | + case["start_input"], |
| 85 | + workflow_type=case["workflow_type"], |
| 86 | + ) |
| 87 | + |
| 88 | + assert len(outcome.commands) == 1 |
| 89 | + expected = case["expected"] |
| 90 | + command = outcome.commands[0] |
| 91 | + |
| 92 | + if expected["command_type"] == "CompleteWorkflow": |
| 93 | + assert isinstance(command, CompleteWorkflow) |
| 94 | + assert command.result == expected["result"] |
| 95 | + |
| 96 | + return |
| 97 | + |
| 98 | + if expected["command_type"] == "ScheduleActivity": |
| 99 | + assert isinstance(command, ScheduleActivity) |
| 100 | + assert command.activity_type == expected["activity_type"] |
| 101 | + assert command.arguments == expected["arguments"] |
| 102 | + |
| 103 | + return |
| 104 | + |
| 105 | + raise AssertionError(f"Unsupported golden expected command type: {expected['command_type']}") |
0 commit comments