Skip to content

Commit

Permalink
Modify unix socket tests to use stdlib tempdirs
Browse files Browse the repository at this point in the history
  • Loading branch information
cjntaylor committed Dec 28, 2023
1 parent 2e9f51a commit 1ac5988
Showing 1 changed file with 32 additions and 13 deletions.
45 changes: 32 additions & 13 deletions tests/test_sockets.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
import platform
import socket
import sys
import tempfile
import threading
import time
from contextlib import suppress
from pathlib import Path
from socket import AddressFamily
from ssl import SSLContext, SSLError
from threading import Thread
from typing import Any, Iterable, Iterator, NoReturn, TypeVar, cast
from typing import Any, Generator, Iterable, Iterator, NoReturn, TypeVar, cast

import psutil
import pytest
Expand Down Expand Up @@ -707,8 +708,11 @@ async def test_bind_link_local(self) -> None:
)
class TestUNIXStream:
@pytest.fixture
def socket_path(self, tmp_path_factory: TempPathFactory) -> Path:
return tmp_path_factory.mktemp("unix").joinpath("socket")
def socket_path(self) -> Generator[Path, None, None]:
# Use stdlib tempdir generation
# Fixes `OSError: AF_UNIX path too long` from pytest generated temp_path
with tempfile.TemporaryDirectory() as path:
yield Path(path) / "socket"

@pytest.fixture(params=[False, True], ids=["str", "path"])
def socket_path_or_str(self, request: SubRequest, socket_path: Path) -> Path | str:
Expand Down Expand Up @@ -1026,8 +1030,11 @@ async def test_connecting_with_non_utf8(self, socket_path: Path) -> None:
)
class TestUNIXListener:
@pytest.fixture
def socket_path(self, tmp_path_factory: TempPathFactory) -> Path:
return tmp_path_factory.mktemp("unix").joinpath("socket")
def socket_path(self) -> Generator[Path, None, None]:
# Use stdlib tempdir generation
# Fixes `OSError: AF_UNIX path too long` from pytest generated temp_path
with tempfile.TemporaryDirectory() as path:
yield Path(path) / "socket"

@pytest.fixture(params=[False, True], ids=["str", "path"])
def socket_path_or_str(self, request: SubRequest, socket_path: Path) -> Path | str:
Expand Down Expand Up @@ -1423,16 +1430,22 @@ async def test_send_after_close(self, family: AnyIPAddressFamily) -> None:
)
class TestUNIXDatagramSocket:
@pytest.fixture
def socket_path(self, tmp_path_factory: TempPathFactory) -> Path:
return tmp_path_factory.mktemp("unix").joinpath("socket")
def socket_path(self) -> Generator[Path, None, None]:
# Use stdlib tempdir generation
# Fixes `OSError: AF_UNIX path too long` from pytest generated temp_path
with tempfile.TemporaryDirectory() as path:
yield Path(path) / "socket"

@pytest.fixture(params=[False, True], ids=["str", "path"])
def socket_path_or_str(self, request: SubRequest, socket_path: Path) -> Path | str:
return socket_path if request.param else str(socket_path)

@pytest.fixture
def peer_socket_path(self, tmp_path_factory: TempPathFactory) -> Path:
return tmp_path_factory.mktemp("unix").joinpath("peer_socket")
def peer_socket_path(self) -> Generator[Path, None, None]:
# Use stdlib tempdir generation
# Fixes `OSError: AF_UNIX path too long` from pytest generated temp_path
with tempfile.TemporaryDirectory() as path:
yield Path(path) / "peer_socket"

async def test_extra_attributes(self, socket_path: Path) -> None:
async with await create_unix_datagram_socket(local_path=socket_path) as unix_dg:
Expand Down Expand Up @@ -1545,16 +1558,22 @@ async def test_local_path_invalid_ascii(self, socket_path: Path) -> None:
)
class TestConnectedUNIXDatagramSocket:
@pytest.fixture
def socket_path(self, tmp_path_factory: TempPathFactory) -> Path:
return tmp_path_factory.mktemp("unix").joinpath("socket")
def socket_path(self) -> Generator[Path, None, None]:
# Use stdlib tempdir generation
# Fixes `OSError: AF_UNIX path too long` from pytest generated temp_path
with tempfile.TemporaryDirectory() as path:
yield Path(path) / "socket"

@pytest.fixture(params=[False, True], ids=["str", "path"])
def socket_path_or_str(self, request: SubRequest, socket_path: Path) -> Path | str:
return socket_path if request.param else str(socket_path)

@pytest.fixture
def peer_socket_path(self, tmp_path_factory: TempPathFactory) -> Path:
return tmp_path_factory.mktemp("unix").joinpath("peer_socket")
def peer_socket_path(self) -> Generator[Path, None, None]:
# Use stdlib tempdir generation
# Fixes `OSError: AF_UNIX path too long` from pytest generated temp_path
with tempfile.TemporaryDirectory() as path:
yield Path(path) / "peer_socket"

@pytest.fixture(params=[False, True], ids=["peer_str", "peer_path"])
def peer_socket_path_or_str(
Expand Down

0 comments on commit 1ac5988

Please sign in to comment.