userver: en/testsuite/plugins/asyncexc.py Source File
Loading...
Searching...
No Matches
asyncexc.py
1"""
2Async exceptions handler.
3
4Handle excpetions in background coroutines.
5"""
6
7import pytest
8
9from testsuite.utils import traceback
10
11
12class BaseError(Exception):
13 pass
14
15
17 pass
18
19
20__tracebackhide__ = traceback.hide(BaseError)
21
22
23@pytest.fixture
24def _asyncexc():
25 errors: list[Exception] = []
26 try:
27 yield errors
28 finally:
29 _raise_if_any(errors)
30
31
32@pytest.fixture
33def asyncexc_append(_asyncexc):
34 """Register background exception.
35
36 @ingroup userver_testsuite_fixtures
37 Part of the [yandex-taxi-testsuite](https://github.com/yandex/yandex-taxi-testsuite/blob/develop/testsuite/plugins/asyncexc.py#L33)
38 """
39 return _asyncexc.append
40
41
42@pytest.fixture
43def asyncexc_check(_asyncexc):
44 """Raise in case there are background exceptions.
45
46 @ingroup userver_testsuite_fixtures
47 Part of the [yandex-taxi-testsuite](https://github.com/yandex/yandex-taxi-testsuite/blob/develop/testsuite/plugins/asyncexc.py#L39)
48 """
49
50 def check():
51 _raise_if_any(_asyncexc)
52
53 return check
54
55
56def _raise_if_any(errors):
57 if not errors:
58 return
59 errors = _clear_and_copy(errors)
60 for exc in errors:
62 f'There were {len(errors)} background exceptions'
63 f', showing the first one',
64 ) from exc
65
66
67def _clear_and_copy(errors):
68 copy = errors[:]
69 errors.clear()
70 return copy