"""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
