Skip to content

Commit 1f5ff65

Browse files
committed
Simplified the tests
1 parent 7578fe2 commit 1f5ff65

File tree

3 files changed

+39
-45
lines changed

3 files changed

+39
-45
lines changed

tests/conftest.py

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import asyncio
4+
import platform
45
import ssl
56
import sys
67
from collections.abc import Generator, Iterator
@@ -9,6 +10,7 @@
910
from unittest.mock import Mock
1011

1112
import pytest
13+
import sniffio
1214
import trustme
1315
from _pytest.fixtures import SubRequest
1416
from trustme import CA
@@ -18,7 +20,10 @@
1820

1921
uvloop_marks = []
2022
try:
21-
import uvloop
23+
if platform.system() == "Windows":
24+
import winloop as uvloop
25+
else:
26+
import uvloop
2227
except ImportError:
2328
uvloop_marks.append(pytest.mark.skip(reason="uvloop not available"))
2429
uvloop = Mock()
@@ -27,32 +32,22 @@
2732
uvloop.loop.Loop, "shutdown_default_executor"
2833
):
2934
uvloop_marks.append(
30-
pytest.mark.skip(reason="uvloop is missing shutdown_default_executor()")
35+
pytest.mark.skip(
36+
reason=f"{uvloop.__name__} is missing shutdown_default_executor()"
37+
)
3138
)
3239

33-
winloop_marks = []
34-
try:
35-
import winloop
36-
except ImportError:
37-
winloop_marks.append(pytest.mark.skip(reason="winloop not available"))
38-
winloop = Mock()
39-
4040
pytest_plugins = ["pytester"]
4141

4242
asyncio_params = [
4343
pytest.param(("asyncio", {"debug": True}), id="asyncio"),
4444
pytest.param(
4545
(
4646
"asyncio",
47-
{
48-
"debug": True,
49-
"loop_factory": uvloop.new_event_loop
50-
if sys.platform != "win32"
51-
else winloop.new_event_loop,
52-
},
47+
{"debug": True, "loop_factory": uvloop.new_event_loop},
5348
),
54-
marks=uvloop_marks if sys.platform != "win32" else winloop_marks,
55-
id="asyncio+uvloop",
49+
marks=uvloop_marks,
50+
id=f"asyncio+{uvloop.__name__}",
5651
),
5752
]
5853
if sys.version_info >= (3, 12):
@@ -159,3 +154,16 @@ def no_other_refs() -> list[object]:
159154

160155
def no_other_refs() -> list[object]:
161156
return [sys._getframe(1)]
157+
158+
159+
@pytest.fixture
160+
def event_loop_implementation_name() -> str | None:
161+
try:
162+
name = sniffio.current_async_library()
163+
except sniffio.AsyncLibraryNotFoundError:
164+
return None
165+
166+
if name == "asyncio":
167+
return asyncio.get_running_loop().__module__
168+
169+
return name

tests/test_sockets.py

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2197,28 +2197,19 @@ async def test_getaddrinfo() -> None:
21972197
@pytest.mark.parametrize("sock_type", [socket.SOCK_STREAM, socket.SOCK_STREAM])
21982198
async def test_getaddrinfo_ipv6addr(
21992199
sock_type: Literal[socket.SocketKind.SOCK_STREAM],
2200+
event_loop_implementation_name: str | None,
22002201
) -> None:
22012202
# IDNA trips up over raw IPv6 addresses
2202-
if platform.system() == "Windows":
2203-
import sniffio
2204-
2205-
if sniffio.current_async_library() == "asyncio":
2206-
import asyncio
2207-
2208-
if asyncio.get_running_loop().__module__ == "winloop":
2209-
proto = 6
2210-
else:
2211-
proto = 0
2212-
else:
2213-
proto = 0
2203+
if platform.system() == "Windows" and event_loop_implementation_name != "winloop":
2204+
expected_proto = 0
22142205
else:
2215-
proto = 6
2206+
expected_proto = 6
22162207

22172208
assert await getaddrinfo("::1", 0, type=sock_type) == [
22182209
(
22192210
socket.AF_INET6,
22202211
socket.SOCK_STREAM,
2221-
proto,
2212+
expected_proto,
22222213
"",
22232214
("::1", 0),
22242215
)

tests/test_subprocesses.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,11 @@
4040
],
4141
)
4242
async def test_run_process(
43-
shell: bool, command: str | list[str], anyio_backend_name: str
43+
shell: bool, command: str | list[str], event_loop_implementation_name: str | None
4444
) -> None:
45-
if anyio_backend_name == "asyncio" and sys.platform == "win32":
46-
import asyncio
45+
if event_loop_implementation_name == "winloop":
46+
pytest.skip(reason="winloop produces a non-zero exit with status 1")
4747

48-
if asyncio.get_event_loop().__module__ == "winloop":
49-
pytest.skip(reason="winloop produces a non-zero exit with status 1")
5048
process = await run_process(command, input=b"abc")
5149
assert process.returncode == 0
5250
assert process.stdout.rstrip() == b"cba"
@@ -312,24 +310,21 @@ async def test_process_aexit_cancellation_closes_standard_streams(
312310
async def test_py39_arguments(
313311
argname: str,
314312
argvalue_factory: Callable[[], Any],
315-
anyio_backend_name: str,
316-
anyio_backend_options: dict[str, Any],
313+
event_loop_implementation_name: str | None,
317314
) -> None:
318315
try:
319316
await run_process(
320317
[sys.executable, "-c", "print('hello')"],
321318
**{argname: argvalue_factory()},
322319
)
323320
except ValueError as exc:
324-
if (
325-
"unexpected kwargs" in str(exc)
326-
and anyio_backend_name == "asyncio"
327-
and anyio_backend_options["loop_factory"]
328-
and anyio_backend_options["loop_factory"].__module__
329-
in ("uvloop", "winloop")
321+
if "unexpected kwargs" in str(exc) and event_loop_implementation_name in (
322+
"uvloop",
323+
"winloop",
330324
):
331325
pytest.skip(
332-
f"the {argname!r} argument is not supported by uvloop or winloop yet"
326+
f"the {argname!r} argument is not supported by "
327+
f"{event_loop_implementation_name} yet"
333328
)
334329

335330
raise

0 commit comments

Comments
 (0)