fix(tests): avoid asyncio DeprecationWarning in event loop fixture on 3.12+

This commit is contained in:
xxxigm 2026-05-07 20:55:59 +07:00 committed by Teknium
parent 12a0f5901c
commit d5fcc83922

View file

@ -483,15 +483,26 @@ def _ensure_current_event_loop(request):
A number of gateway tests still use asyncio.get_event_loop().run_until_complete(...).
Ensure they always have a usable loop without interfering with pytest-asyncio's
own loop management for @pytest.mark.asyncio tests.
On Python 3.12+, ``asyncio.get_event_loop_policy().get_event_loop()`` with no
*running* loop emits DeprecationWarning; skip that path and install a fresh
loop via ``new_event_loop()`` instead.
"""
if request.node.get_closest_marker("asyncio") is not None:
yield
return
loop = None
try:
loop = asyncio.get_event_loop_policy().get_event_loop()
loop = asyncio.get_running_loop()
except RuntimeError:
loop = None
pass
if loop is None and sys.version_info < (3, 12):
try:
loop = asyncio.get_event_loop_policy().get_event_loop()
except RuntimeError:
loop = None
created = loop is None or loop.is_closed()
if created: