|
| 1 | +"""Worker.run installs worker dependencies while loading task collections.""" |
| 2 | + |
| 3 | +import sys |
| 4 | +from contextlib import asynccontextmanager |
| 5 | +from contextvars import ContextVar |
| 6 | +from types import ModuleType |
| 7 | +from typing import AsyncGenerator |
| 8 | +from uuid import uuid4 |
| 9 | + |
| 10 | +import pytest |
| 11 | + |
| 12 | +from docket import CurrentExecution, Depends, Docket, Execution, Worker |
| 13 | +from docket.execution import ExecutionState |
| 14 | + |
| 15 | + |
| 16 | +@pytest.mark.parametrize("collection_kind", ["mapping", "list", "tuple"]) |
| 17 | +@pytest.mark.parametrize("by_path", [False, True]) |
| 18 | +async def test_run_dependencies_bracket_tasks( |
| 19 | + monkeypatch: pytest.MonkeyPatch, collection_kind: str, by_path: bool |
| 20 | +) -> None: |
| 21 | + events: list[tuple[str, str]] = [] |
| 22 | + task_context: ContextVar[str] = ContextVar("task_context") |
| 23 | + |
| 24 | + @asynccontextmanager |
| 25 | + async def logging_context( |
| 26 | + execution: Execution = CurrentExecution(), |
| 27 | + ) -> AsyncGenerator[None, None]: |
| 28 | + assert task_context.get(None) is None |
| 29 | + events.append(("enter", execution.key)) |
| 30 | + token = task_context.set(execution.key) |
| 31 | + try: |
| 32 | + yield |
| 33 | + finally: |
| 34 | + events.append(("exit", execution.key)) |
| 35 | + task_context.reset(token) |
| 36 | + |
| 37 | + async def task(fail: bool) -> None: |
| 38 | + events.append(("task", task_context.get())) |
| 39 | + if fail: |
| 40 | + raise ValueError("task failed") |
| 41 | + |
| 42 | + dependency = Depends(logging_context) |
| 43 | + if collection_kind == "mapping": |
| 44 | + dependencies = {"logging": dependency} |
| 45 | + elif collection_kind == "tuple": |
| 46 | + dependencies = (dependency,) |
| 47 | + else: |
| 48 | + dependencies = [dependency] |
| 49 | + |
| 50 | + module = ModuleType("run_dependencies_fixture") |
| 51 | + setattr(module, "tasks", [task]) |
| 52 | + setattr(module, "dependencies", dependencies) |
| 53 | + monkeypatch.setitem(sys.modules, module.__name__, module) |
| 54 | + |
| 55 | + url = f"memory://{uuid4()}" |
| 56 | + async with Docket(name="run-dependencies", url=url) as docket: |
| 57 | + successful = await docket.add(task, key="successful")(False) |
| 58 | + failed = await docket.add(task, key="failed")(True) |
| 59 | + |
| 60 | + await Worker.run( |
| 61 | + docket_name=docket.name, |
| 62 | + url=url, |
| 63 | + tasks=[f"{module.__name__}:tasks"], |
| 64 | + dependencies=f"{module.__name__}:dependencies" if by_path else dependencies, |
| 65 | + concurrency=1, |
| 66 | + schedule_automatic_tasks=False, |
| 67 | + until_finished=True, |
| 68 | + ) |
| 69 | + |
| 70 | + await successful.sync() |
| 71 | + await failed.sync() |
| 72 | + |
| 73 | + for key in ("successful", "failed"): |
| 74 | + assert [event for event in events if event[1] == key] == [ |
| 75 | + ("enter", key), |
| 76 | + ("task", key), |
| 77 | + ("exit", key), |
| 78 | + ] |
| 79 | + assert task_context.get(None) is None |
| 80 | + assert successful.state == ExecutionState.COMPLETED |
| 81 | + assert failed.state == ExecutionState.FAILED |
| 82 | + |
| 83 | + |
| 84 | +@pytest.mark.parametrize( |
| 85 | + ("path", "error"), |
| 86 | + [ |
| 87 | + ("missing_worker_dependencies:dependencies", ModuleNotFoundError), |
| 88 | + ("docket.tasks:missing_dependencies", AttributeError), |
| 89 | + ], |
| 90 | +) |
| 91 | +async def test_run_dependency_import_errors(path: str, error: type[Exception]) -> None: |
| 92 | + with pytest.raises(error): |
| 93 | + await Worker.run(url="memory://", dependencies=path) |
| 94 | + |
| 95 | + |
| 96 | +@pytest.mark.parametrize("by_path", [False, True]) |
| 97 | +async def test_run_rejects_bare_dependency_functions( |
| 98 | + monkeypatch: pytest.MonkeyPatch, by_path: bool |
| 99 | +) -> None: |
| 100 | + def dependency() -> None: ... |
| 101 | + |
| 102 | + module = ModuleType("invalid_dependencies_fixture") |
| 103 | + setattr(module, "dependencies", [dependency]) |
| 104 | + monkeypatch.setitem(sys.modules, module.__name__, module) |
| 105 | + |
| 106 | + with pytest.raises(TypeError, match="must be a Dependency instance"): |
| 107 | + await Worker.run( |
| 108 | + url=f"memory://{uuid4()}", |
| 109 | + tasks=[], |
| 110 | + dependencies=f"{module.__name__}:dependencies" if by_path else [dependency], |
| 111 | + schedule_automatic_tasks=False, |
| 112 | + until_finished=True, |
| 113 | + ) |
0 commit comments