Skip to content

Commit

Permalink
Modify unix socket tests to short paths
Browse files Browse the repository at this point in the history
  • Loading branch information
cjntaylor committed Dec 23, 2023
1 parent befba29 commit f42ac6d
Showing 1 changed file with 50 additions and 13 deletions.
63 changes: 50 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,14 @@ 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 /tmp if exists, falling back to the default location
# Fixes `OSError: AF_UNIX path too long` on MacOS
# Discussion: https://github.com/python/cpython/issues/93852
tmp = Path("/tmp")
dir = tmp if tmp.exists() else None
with tempfile.TemporaryDirectory(dir=dir) 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 +1033,14 @@ 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 /tmp if exists, falling back to the default location
# Fixes `OSError: AF_UNIX path too long` on MacOS
# Discussion: https://github.com/python/cpython/issues/93852
tmp = Path("/tmp")
dir = tmp if tmp.exists() else None
with tempfile.TemporaryDirectory(dir=dir) 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 +1436,28 @@ 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 /tmp if exists, falling back to the default location
# Fixes `OSError: AF_UNIX path too long` on MacOS
# Discussion: https://github.com/python/cpython/issues/93852
tmp = Path("/tmp")
dir = tmp if tmp.exists() else None
with tempfile.TemporaryDirectory(dir=dir) 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 /tmp if exists, falling back to the default location
# Fixes `OSError: AF_UNIX path too long` on MacOS
# Discussion: https://github.com/python/cpython/issues/93852
tmp = Path("/tmp")
dir = tmp if tmp.exists() else None
with tempfile.TemporaryDirectory(dir=dir) 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 +1570,28 @@ 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 /tmp if exists, falling back to the default location
# Fixes `OSError: AF_UNIX path too long` on MacOS
# Discussion: https://github.com/python/cpython/issues/93852
tmp = Path("/tmp")
dir = tmp if tmp.exists() else None
with tempfile.TemporaryDirectory(dir=dir) 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 /tmp if exists, falling back to the default location
# Fixes `OSError: AF_UNIX path too long` on MacOS
# Discussion: https://github.com/python/cpython/issues/93852
tmp = Path("/tmp")
dir = tmp if tmp.exists() else None
with tempfile.TemporaryDirectory(dir=dir) 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 f42ac6d

Please sign in to comment.