Skip to content

Commit 6c5b1a1

Browse files
committed
Add tests for _compat.py
1 parent 100d1ad commit 6c5b1a1

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

tests/test_compat.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from __future__ import annotations
2+
3+
import asyncio
4+
from asyncio import AbstractEventLoop
5+
6+
import pytest
7+
8+
from tests.custom_loop_utils import CustomLoop, custom_loop_factory
9+
from tests.utils import get_asyncio_default_loop_per_os
10+
from uvicorn._compat import asyncio_run
11+
12+
13+
async def assert_event_loop(expected_loop_class: type[AbstractEventLoop]):
14+
assert isinstance(asyncio.get_event_loop(), expected_loop_class)
15+
16+
17+
def test_asyncio_run__default_loop_factory() -> None:
18+
asyncio_run(assert_event_loop(get_asyncio_default_loop_per_os()), loop_factory=None)
19+
20+
21+
def test_asyncio_run__custom_loop_factory() -> None:
22+
asyncio_run(assert_event_loop(CustomLoop), loop_factory=custom_loop_factory(use_subprocess=False))
23+
24+
25+
def test_asyncio_run__passing_a_non_awaitable_callback_should_throw_error() -> None:
26+
with pytest.raises(ValueError):
27+
asyncio_run(
28+
lambda: None, # type: ignore
29+
loop_factory=custom_loop_factory(use_subprocess=False),
30+
)

0 commit comments

Comments
 (0)