|
1 | | -import asyncio |
2 | | - |
3 | | -import pytest |
4 | | - |
5 | | -from quarkchain.protocol import AbstractConnection |
6 | | -from quarkchain.utils import _get_or_create_event_loop |
7 | | - |
8 | | - |
9 | | -@pytest.fixture(autouse=True) |
10 | | -def cleanup_event_loop(): |
11 | | - """Cancel all pending asyncio tasks after each test to prevent inter-test contamination.""" |
12 | | - yield |
13 | | - loop = _get_or_create_event_loop() |
14 | | - # Multiple rounds of cleanup: cancelling tasks can spawn new tasks in finally blocks |
15 | | - for _ in range(3): |
16 | | - pending = [t for t in asyncio.all_tasks(loop) if not t.done()] |
17 | | - if not pending: |
18 | | - break |
19 | | - for task in pending: |
20 | | - task.cancel() |
21 | | - loop.run_until_complete(asyncio.gather(*pending, return_exceptions=True)) |
22 | | - # Let the loop process any callbacks triggered by cancellation |
23 | | - loop.run_until_complete(asyncio.sleep(0)) |
24 | | - AbstractConnection.aborted_rpc_count = 0 |
| 1 | +import asyncio |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from quarkchain.protocol import AbstractConnection |
| 6 | +from quarkchain.utils import _get_or_create_event_loop |
| 7 | + |
| 8 | + |
| 9 | +@pytest.fixture(autouse=True) |
| 10 | +def cleanup_event_loop(): |
| 11 | + """Cancel all pending asyncio tasks after each test to prevent inter-test contamination.""" |
| 12 | + yield |
| 13 | + loop = _get_or_create_event_loop() |
| 14 | + # Multiple rounds of cleanup: cancelling tasks can spawn new tasks in finally blocks |
| 15 | + for _ in range(3): |
| 16 | + pending = [t for t in asyncio.all_tasks(loop) if not t.done()] |
| 17 | + if not pending: |
| 18 | + break |
| 19 | + for task in pending: |
| 20 | + task.cancel() |
| 21 | + loop.run_until_complete(asyncio.gather(*pending, return_exceptions=True)) |
| 22 | + # Let the loop process any callbacks triggered by cancellation |
| 23 | + loop.run_until_complete(asyncio.sleep(0)) |
| 24 | + AbstractConnection.aborted_rpc_count = 0 |
0 commit comments