|
| 1 | +"""Tests for ConcurrencyLimit via Annotated-style type hints.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import asyncio |
| 6 | +import time |
| 7 | +from typing import Annotated |
| 8 | + |
| 9 | +import pytest |
| 10 | + |
| 11 | +from docket import ConcurrencyLimit, Docket, Worker |
| 12 | + |
| 13 | +from tests.concurrency_limits.overlap import assert_some_overlap |
| 14 | + |
| 15 | + |
| 16 | +async def test_annotated_concurrency_limit(docket: Docket, worker: Worker): |
| 17 | + """Annotated[int, ConcurrencyLimit(1)] limits concurrency per-parameter.""" |
| 18 | + results: list[str] = [] |
| 19 | + |
| 20 | + async def task(customer_id: Annotated[int, ConcurrencyLimit(1)]): |
| 21 | + results.append(f"start_{customer_id}") |
| 22 | + await asyncio.sleep(0.01) |
| 23 | + results.append(f"end_{customer_id}") |
| 24 | + |
| 25 | + await docket.add(task)(customer_id=1) |
| 26 | + await docket.add(task)(customer_id=1) |
| 27 | + |
| 28 | + await worker.run_until_finished() |
| 29 | + |
| 30 | + assert results == ["start_1", "end_1", "start_1", "end_1"] |
| 31 | + |
| 32 | + |
| 33 | +async def test_annotated_concurrency_different_values(docket: Docket, worker: Worker): |
| 34 | + """Different argument values get independent concurrency slots.""" |
| 35 | + execution_intervals: dict[int, tuple[float, float]] = {} |
| 36 | + |
| 37 | + async def task(customer_id: Annotated[int, ConcurrencyLimit(1)]): |
| 38 | + start = time.monotonic() |
| 39 | + await asyncio.sleep(0.1) |
| 40 | + end = time.monotonic() |
| 41 | + execution_intervals[customer_id] = (start, end) |
| 42 | + |
| 43 | + await docket.add(task)(customer_id=1) |
| 44 | + await docket.add(task)(customer_id=2) |
| 45 | + await docket.add(task)(customer_id=3) |
| 46 | + |
| 47 | + worker.concurrency = 10 |
| 48 | + await worker.run_until_finished() |
| 49 | + |
| 50 | + assert len(execution_intervals) == 3 |
| 51 | + intervals = list(execution_intervals.values()) |
| 52 | + assert_some_overlap(intervals, "Different customers should run concurrently") |
| 53 | + |
| 54 | + |
| 55 | +async def test_annotated_concurrency_max_concurrent(docket: Docket, worker: Worker): |
| 56 | + """max_concurrent>1 allows that many concurrent executions per value.""" |
| 57 | + active_tasks: list[int] = [] |
| 58 | + max_concurrent_seen = 0 |
| 59 | + lock = asyncio.Lock() |
| 60 | + |
| 61 | + async def task( |
| 62 | + db_name: str, |
| 63 | + task_id: Annotated[int, ConcurrencyLimit("db_name", max_concurrent=2)], |
| 64 | + ): |
| 65 | + nonlocal max_concurrent_seen |
| 66 | + async with lock: |
| 67 | + active_tasks.append(task_id) |
| 68 | + max_concurrent_seen = max(max_concurrent_seen, len(active_tasks)) |
| 69 | + await asyncio.sleep(0.1) |
| 70 | + async with lock: |
| 71 | + active_tasks.remove(task_id) |
| 72 | + |
| 73 | + for i in range(5): |
| 74 | + await docket.add(task)(db_name="postgres", task_id=i) |
| 75 | + |
| 76 | + worker.concurrency = 10 |
| 77 | + await worker.run_until_finished() |
| 78 | + |
| 79 | + assert max_concurrent_seen <= 2 |
| 80 | + |
| 81 | + |
| 82 | +async def test_two_annotated_concurrency_limits_rejected(docket: Docket): |
| 83 | + """ConcurrencyLimit.single=True prevents two annotations on one function.""" |
| 84 | + with pytest.raises(ValueError, match="Only one ConcurrencyLimit"): |
| 85 | + |
| 86 | + async def task( |
| 87 | + customer_id: Annotated[int, ConcurrencyLimit(1)], |
| 88 | + region: Annotated[str, ConcurrencyLimit(2)], |
| 89 | + ): ... # pragma: no cover |
| 90 | + |
| 91 | + await docket.add(task)(customer_id=1, region="us") |
| 92 | + |
| 93 | + |
| 94 | +async def test_single_conflict_annotation_and_default(docket: Docket): |
| 95 | + """single=True conflict detected across annotation and default-param styles.""" |
| 96 | + with pytest.raises(ValueError, match="Only one ConcurrencyLimit"): |
| 97 | + |
| 98 | + async def task( |
| 99 | + customer_id: Annotated[int, ConcurrencyLimit(1)], |
| 100 | + concurrency: ConcurrencyLimit = ConcurrencyLimit(max_concurrent=2), |
| 101 | + ): ... # pragma: no cover |
| 102 | + |
| 103 | + await docket.add(task)(customer_id=1) |
| 104 | + |
| 105 | + |
| 106 | +async def test_annotated_concurrency_keys_cleaned_up(docket: Docket, worker: Worker): |
| 107 | + """Concurrency keys from annotated deps are properly cleaned up.""" |
| 108 | + |
| 109 | + async def task(customer_id: Annotated[int, ConcurrencyLimit(1)]): |
| 110 | + pass |
| 111 | + |
| 112 | + await docket.add(task)(customer_id=42) |
| 113 | + await worker.run_until_finished() |
| 114 | + |
| 115 | + async with docket.redis() as redis: |
| 116 | + key = f"{docket.name}:concurrency:customer_id:42" |
| 117 | + assert await redis.exists(key) == 0 |
0 commit comments