From 46078a9e2a1a63fdf19dbbdd6b9d07fccd1086df Mon Sep 17 00:00:00 2001 From: Nir Geller Date: Mon, 19 Aug 2024 00:38:32 +0300 Subject: [PATCH] Add tests for `_compat.py` --- tests/test_compat.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 tests/test_compat.py diff --git a/tests/test_compat.py b/tests/test_compat.py new file mode 100644 index 000000000..15af6a4eb --- /dev/null +++ b/tests/test_compat.py @@ -0,0 +1,30 @@ +from __future__ import annotations + +import asyncio +from asyncio import AbstractEventLoop + +import pytest + +from tests.custom_loop_utils import CustomLoop, custom_loop_factory +from tests.utils import get_asyncio_default_loop_per_os +from uvicorn._compat import asyncio_run + + +async def assert_event_loop(expected_loop_class: type[AbstractEventLoop]): + assert isinstance(asyncio.get_event_loop(), expected_loop_class) + + +def test_asyncio_run__default_loop_factory() -> None: + asyncio_run(assert_event_loop(get_asyncio_default_loop_per_os()), loop_factory=None) + + +def test_asyncio_run__custom_loop_factory() -> None: + asyncio_run(assert_event_loop(CustomLoop), loop_factory=custom_loop_factory(use_subprocess=False)) + + +def test_asyncio_run__passing_a_non_awaitable_callback_should_throw_error() -> None: + with pytest.raises(ValueError): + asyncio_run( + lambda: None, # type: ignore + loop_factory=custom_loop_factory(use_subprocess=False), + )