|
| 1 | +from __future__ import annotations |
| 2 | +from typing import Optional |
| 3 | +import warnings |
| 4 | +import threading |
| 5 | +import trio |
| 6 | +import pytest |
| 7 | +import pytest_timeout |
| 8 | +from .traceback_format import format_recursive_nursery_stack |
| 9 | + |
| 10 | + |
| 11 | +pytest_timeout_settings = pytest.StashKey[pytest_timeout.Settings]() |
| 12 | +send_timeout_callable = None |
| 13 | +send_timeout_callable_ready_event = threading.Event() |
| 14 | + |
| 15 | + |
| 16 | +def set_timeout(item: pytest.Item) -> None: |
| 17 | + try: |
| 18 | + settings = item.stash[pytest_timeout_settings] |
| 19 | + except KeyError: |
| 20 | + # No timeout or not our timeout |
| 21 | + return |
| 22 | + |
| 23 | + if settings.func_only: |
| 24 | + warnings.warn( |
| 25 | + "Function only timeouts are not supported for trio based timeouts" |
| 26 | + ) |
| 27 | + |
| 28 | + global send_timeout_callable |
| 29 | + |
| 30 | + # Shouldn't be racy, as xdist uses different processes |
| 31 | + if send_timeout_callable is None: |
| 32 | + threading.Thread(target=trio_timeout_thread, daemon=True).start() |
| 33 | + |
| 34 | + send_timeout_callable_ready_event.wait() |
| 35 | + |
| 36 | + send_timeout_callable(settings.timeout) |
| 37 | + |
| 38 | + |
| 39 | +@pytest.hookimpl() |
| 40 | +def pytest_timeout_set_timer( |
| 41 | + item: pytest.Item, settings: pytest_timeout.Settings |
| 42 | +) -> Optional[bool]: |
| 43 | + if item.get_closest_marker("trio") is not None and item.config.getini("trio_timeout"): |
| 44 | + item.stash[pytest_timeout_settings] = settings |
| 45 | + return True |
| 46 | + |
| 47 | + |
| 48 | +# No need for pytest_timeout_cancel_timer as we detect that the test loop has exited |
| 49 | + |
| 50 | + |
| 51 | +def trio_timeout_thread(): |
| 52 | + async def run_timeouts(): |
| 53 | + async with trio.open_nursery() as nursery: |
| 54 | + token = trio.lowlevel.current_trio_token() |
| 55 | + |
| 56 | + async def wait_timeout(token: trio.TrioToken, timeout: float) -> None: |
| 57 | + await trio.sleep(timeout) |
| 58 | + |
| 59 | + try: |
| 60 | + token.run_sync_soon( |
| 61 | + lambda: trio.lowlevel.spawn_system_task(execute_timeout) |
| 62 | + ) |
| 63 | + except RuntimeError: |
| 64 | + # test has finished |
| 65 | + pass |
| 66 | + |
| 67 | + def send_timeout(timeout: float): |
| 68 | + test_token = trio.lowlevel.current_trio_token() |
| 69 | + token.run_sync_soon( |
| 70 | + lambda: nursery.start_soon(wait_timeout, test_token, timeout) |
| 71 | + ) |
| 72 | + |
| 73 | + global send_timeout_callable |
| 74 | + send_timeout_callable = send_timeout |
| 75 | + send_timeout_callable_ready_event.set() |
| 76 | + |
| 77 | + await trio.sleep_forever() |
| 78 | + |
| 79 | + trio.run(run_timeouts) |
| 80 | + |
| 81 | + |
| 82 | +async def execute_timeout() -> None: |
| 83 | + if pytest_timeout.is_debugging(): |
| 84 | + return |
| 85 | + |
| 86 | + nursery = get_test_nursery() |
| 87 | + stack = "\n".join(format_recursive_nursery_stack(nursery) + ["Timeout reached"]) |
| 88 | + |
| 89 | + async def report(): |
| 90 | + pytest.fail(stack, pytrace=False) |
| 91 | + |
| 92 | + nursery.start_soon(report) |
| 93 | + |
| 94 | + |
| 95 | +def get_test_nursery() -> trio.Nursery: |
| 96 | + task = trio.lowlevel.current_task().parent_nursery.parent_task |
| 97 | + |
| 98 | + for nursery in task.child_nurseries: |
| 99 | + for task in nursery.child_tasks: |
| 100 | + if task.name.startswith("pytest_trio.plugin._trio_test_runner_factory"): |
| 101 | + return task.child_nurseries[0] |
| 102 | + |
| 103 | + raise Exception("Could not find test nursery") |
0 commit comments