ci(tst-03): ensure a current event loop for sync python tests (py3.12)
ci / live-mxaccess (push) Waiting to run
ci / java (push) Successful in 1m50s
ci / portable (push) Successful in 7m9s
ci / windows (push) Failing after 8s

The last portable failure: pytest-asyncio 1.x does not install a current event
loop for synchronous tests and clears it after each async test, so a later sync
test that builds a grpc.aio.Channel (create_channel) hit
"RuntimeError: There is no current event loop" on Python 3.12 depending on test
order (CI hit it; local order did not). Add an autouse conftest fixture that
ensures a usable current loop per test. Local: 127 passed, 1 skipped.

Claude-Session: https://claude.ai/code/session_01DMXXvNuPekkkrTEyPNxEkW
This commit is contained in:
Joseph Doherty
2026-07-10 04:11:45 -04:00
parent 576179bd3f
commit 70ef87aea2
+27
View File
@@ -0,0 +1,27 @@
"""Shared pytest fixtures for the client test suite.
pytest-asyncio 1.x no longer installs a current event loop for *synchronous*
tests, and it clears the loop after each ``async`` test. Some synchronous tests
build a ``grpc.aio.Channel`` (via ``create_channel``), which calls
``asyncio.get_event_loop()`` and therefore raises
``RuntimeError: There is no current event loop`` on Python 3.12 when a prior
async test has left the policy with no current loop. Ensure every test starts
with a usable current loop so channel construction in a sync test is
order-independent.
"""
from __future__ import annotations
import asyncio
from collections.abc import Iterator
import pytest
@pytest.fixture(autouse=True)
def _ensure_current_event_loop() -> Iterator[None]:
try:
asyncio.get_event_loop()
except RuntimeError:
asyncio.set_event_loop(asyncio.new_event_loop())
yield