|
| 1 | +"""Exercise ordinary workers and count their real HTTP poll requests.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import asyncio |
| 6 | +import importlib.metadata |
| 7 | +import json |
| 8 | +import uuid |
| 9 | +from collections import Counter |
| 10 | +from typing import Any |
| 11 | + |
| 12 | +import httpx |
| 13 | +import pytest |
| 14 | + |
| 15 | +from durable_workflow import Client, Worker, activity, workflow |
| 16 | + |
| 17 | + |
| 18 | +@activity.defn(name="tests.task-family.echo") |
| 19 | +async def echo(value: str) -> str: |
| 20 | + return f"echo:{value}" |
| 21 | + |
| 22 | + |
| 23 | +@workflow.defn(name="tests.task-family.activity") |
| 24 | +class ActivityWorkflow: |
| 25 | + def run(self, ctx: Any, value: str) -> Any: |
| 26 | + return (yield ctx.schedule_activity("tests.task-family.echo", [value])) |
| 27 | + |
| 28 | + |
| 29 | +@workflow.defn(name="tests.task-family.timer") |
| 30 | +class TimerWorkflow: |
| 31 | + def run(self, ctx: Any, value: str) -> Any: |
| 32 | + yield ctx.sleep(2) |
| 33 | + return f"timer:{value}" |
| 34 | + |
| 35 | + |
| 36 | +def record_polls(client: Client) -> Counter[str]: |
| 37 | + counts: Counter[str] = Counter() |
| 38 | + |
| 39 | + async def record(request: httpx.Request) -> None: |
| 40 | + for family in ("workflow", "activity", "query"): |
| 41 | + if request.url.path.endswith(f"/worker/{family}-tasks/poll"): |
| 42 | + counts[family] += 1 |
| 43 | + |
| 44 | + client._http.event_hooks["request"].append(record) |
| 45 | + return counts |
| 46 | + |
| 47 | + |
| 48 | +@pytest.mark.asyncio |
| 49 | +@pytest.mark.parametrize("mode", ["split", "mixed", "timer"]) |
| 50 | +@pytest.mark.parametrize("entry_point", ["run", "run_until"]) |
| 51 | +async def test_workers_execute_without_polling_empty_task_families( |
| 52 | + server_url: str, server_token: str, mode: str, entry_point: str, |
| 53 | +) -> None: |
| 54 | + suffix = uuid.uuid4().hex[:12] |
| 55 | + queue = f"task-family-{suffix}" |
| 56 | + async with ( |
| 57 | + Client(server_url, token=server_token, namespace="default") as control, |
| 58 | + Client(server_url, token=server_token, namespace="default") as workflow_client, |
| 59 | + Client(server_url, token=server_token, namespace="default") as activity_client, |
| 60 | + ): |
| 61 | + workflow_polls = record_polls(workflow_client) |
| 62 | + activity_polls = record_polls(activity_client) |
| 63 | + workflow_definition = TimerWorkflow if mode == "timer" else ActivityWorkflow |
| 64 | + worker = Worker( |
| 65 | + workflow_client, task_queue=queue, |
| 66 | + workflows=[workflow_definition], |
| 67 | + activities=[echo] if mode == "mixed" else [], |
| 68 | + poll_timeout=0.5, shutdown_timeout=10, |
| 69 | + ) |
| 70 | + workers = [worker] |
| 71 | + if mode == "split": |
| 72 | + workers.append(Worker( |
| 73 | + activity_client, task_queue=queue, workflows=[], activities=[echo], |
| 74 | + poll_timeout=0.5, shutdown_timeout=10, |
| 75 | + )) |
| 76 | + |
| 77 | + handle = await control.start_workflow( |
| 78 | + workflow_type="tests.task-family.timer" if mode == "timer" else "tests.task-family.activity", |
| 79 | + task_queue=queue, workflow_id=f"task-family-{suffix}", input=[suffix], |
| 80 | + ) |
| 81 | + runners = [asyncio.create_task( |
| 82 | + selected.run() if entry_point == "run" else selected.run_until( |
| 83 | + workflow_id=handle.workflow_id, timeout=30, poll_interval=0.05, |
| 84 | + ) |
| 85 | + ) for selected in workers] |
| 86 | + try: |
| 87 | + expected = f"timer:{suffix}" if mode == "timer" else f"echo:{suffix}" |
| 88 | + assert await handle.result(timeout=30, poll_interval=0.1) == expected |
| 89 | + if entry_point == "run_until": |
| 90 | + terminal = await asyncio.wait_for(asyncio.gather(*runners), timeout=10) |
| 91 | + assert all((result.status or "").lower() == "completed" for result in terminal) |
| 92 | + finally: |
| 93 | + await asyncio.gather(*(selected.stop() for selected in workers)) |
| 94 | + await asyncio.wait_for(asyncio.gather(*runners), timeout=10) |
| 95 | + |
| 96 | + assert workflow_polls["workflow"] > 0 |
| 97 | + assert (workflow_polls["activity"] > 0) is (mode == "mixed") |
| 98 | + if mode == "split": |
| 99 | + assert activity_polls["activity"] > 0 |
| 100 | + assert activity_polls["workflow"] == 0 |
| 101 | + assert activity_polls["query"] == 0 |
| 102 | + print(json.dumps({ |
| 103 | + "sdk_version": importlib.metadata.version("durable-workflow"), |
| 104 | + "mode": mode, "entry_point": entry_point, "workflow_id": handle.workflow_id, |
| 105 | + "workflow_worker_polls": dict(workflow_polls), |
| 106 | + "activity_worker_polls": dict(activity_polls), "outcome": "pass", |
| 107 | + }), flush=True) |
0 commit comments