Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions spectator/writer/socket_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import time
from ipaddress import ip_address, IPv6Address
from typing import Optional, Tuple, TypeAlias, Union
from urllib.parse import urlparse
from urllib.parse import unquote, urlparse

from spectator.config import Config
from spectator.writer import Writer
Expand Down Expand Up @@ -57,8 +57,8 @@ def _init_udp(self, location: str):
self._family = socket.AF_INET

def _init_unix(self, location: str):
self._address = urlparse(location).path
self._family = socket.AF_UNIX
self._address = unquote(urlparse(location).path)
self._family = getattr(socket, "AF_UNIX", socket.AF_INET)

def _background_flush(self) -> None:
while not self._closed:
Expand All @@ -81,6 +81,9 @@ def _acquire_socket(self) -> None:
try:
with self._lock:
if self._sock is None and not self._closed:
if self._family is None:
self._logger.error("AF_UNIX socket family is not supported on this platform")
return
self._sock = socket.socket(family=self._family, type=socket.SOCK_DGRAM)
except Exception as e:
self._logger.error("exception during socket acquire: %s", e)
Expand Down
17 changes: 17 additions & 0 deletions tests/writer/test_socket_writer_unix.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import random
import socket
import unittest
from contextlib import closing
from urllib.parse import quote

from spectator import Config, SocketWriter
from ..unix_server import UnixServer


class SocketWriterUnixTest(unittest.TestCase):

def test_socket_writer_unix_escaped_address(self) -> None:
w = SocketWriter(Config("unix:///run/spectatord/my%20service/spectatord.unix"))
self.assertEqual("/run/spectatord/my service/spectatord.unix", w._address) # pylint: disable=protected-access

@unittest.skipUnless(hasattr(socket, "AF_UNIX"), "requires AF_UNIX support")
def test_socket_writer_unix(self) -> None:
with closing(UnixServer()) as server:
with closing(SocketWriter(Config(server.address()))) as w:
Expand All @@ -15,8 +23,17 @@ def test_socket_writer_unix(self) -> None:
w.write("bar")
self.assertEqual("bar", server.read())

@unittest.skipUnless(hasattr(socket, "AF_UNIX"), "requires AF_UNIX support")
def test_socket_writer_unix_path_containing_udp(self) -> None:
with closing(UnixServer("/tmp/udp-spectatord-test")) as server:
with closing(SocketWriter(Config(server.address()))) as w:
w.write("foo")
self.assertEqual("foo", server.read())

@unittest.skipUnless(hasattr(socket, "AF_UNIX"), "requires AF_UNIX support")
def test_socket_writer_unix_escaped_path(self) -> None:
path = f"/tmp/spectatord test {random.randint(1, 10000)}.unix"
with closing(UnixServer(path)) as server:
with closing(SocketWriter(Config(f"unix://{quote(path)}"))) as w:
w.write("foo")
self.assertEqual("foo", server.read())