Skip to content

Commit 7404b79

Browse files
Support strict_exception_groups with timeouts
1 parent 8c147fa commit 7404b79

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

pytest_trio/plugin.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from trio.testing import MockClock
1414
from _pytest.outcomes import Skipped, XFailed
1515
# pytest_timeout_set_timer needs to be imported here for pluggy
16-
from .timeout import set_timeout, pytest_timeout_set_timer as pytest_timeout_set_timer
16+
from .timeout import set_timeout, TimeoutTriggeredException, pytest_timeout_set_timer as pytest_timeout_set_timer
1717

1818
if sys.version_info[:2] < (3, 11):
1919
from exceptiongroup import BaseExceptionGroup
@@ -362,6 +362,8 @@ def wrapper(**kwargs):
362362
ex = queue.pop()
363363
if isinstance(ex, BaseExceptionGroup):
364364
queue.extend(ex.exceptions)
365+
elif isinstance(ex, TimeoutTriggeredException):
366+
pytest.fail(str(ex), pytrace=False)
365367
else:
366368
leaves.append(ex)
367369
if len(leaves) == 1:
@@ -372,6 +374,8 @@ def wrapper(**kwargs):
372374
# Since our leaf exceptions don't consist of exactly one 'magic'
373375
# skipped or xfailed exception, re-raise the whole group.
374376
raise
377+
except TimeoutTriggeredException as ex:
378+
pytest.fail(str(ex), pytrace=False)
375379

376380
return wrapper
377381

pytest_trio/timeout.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ def pytest_timeout_set_timer(
4848
# No need for pytest_timeout_cancel_timer as we detect that the test loop has exited
4949

5050

51+
class TimeoutTriggeredException(Exception):
52+
pass
53+
54+
5155
def trio_timeout_thread():
5256
async def run_timeouts():
5357
async with trio.open_nursery() as nursery:
@@ -87,7 +91,7 @@ async def execute_timeout() -> None:
8791
stack = "\n".join(format_recursive_nursery_stack(nursery) + ["Timeout reached"])
8892

8993
async def report():
90-
pytest.fail(stack, pytrace=False)
94+
raise TimeoutTriggeredException(stack)
9195

9296
nursery.start_soon(report)
9397

0 commit comments

Comments
 (0)