mirror of
https://gitlab.com/veilid/veilid.git
synced 2024-12-24 14:59:31 -05:00
Move Python IPC connection from tests to Veilid
This commit is contained in:
parent
672ad87d28
commit
0c19414934
@ -8,7 +8,6 @@ from veilid.json_api import _JsonVeilidAPI
|
||||
|
||||
import veilid
|
||||
|
||||
from .api import VeilidTestConnectionError, api_connector
|
||||
|
||||
pytest_plugins = ("pytest_asyncio",)
|
||||
|
||||
@ -20,8 +19,8 @@ async def simple_update_callback(update: veilid.VeilidUpdate):
|
||||
@pytest_asyncio.fixture
|
||||
async def api_connection() -> AsyncGenerator[_JsonVeilidAPI, None]:
|
||||
try:
|
||||
api = await api_connector(simple_update_callback)
|
||||
except VeilidTestConnectionError:
|
||||
api = await veilid.api_connector(simple_update_callback)
|
||||
except veilid.VeilidConnectionError:
|
||||
pytest.skip("Unable to connect to veilid-server.")
|
||||
return
|
||||
|
||||
|
@ -7,7 +7,7 @@ import json
|
||||
import time
|
||||
import os
|
||||
from . import *
|
||||
from .api import VeilidTestConnectionError, api_connector
|
||||
|
||||
|
||||
##################################################################
|
||||
BOGUS_KEY = veilid.TypedKey.from_value(
|
||||
@ -217,8 +217,8 @@ async def test_watch_dht_values():
|
||||
await value_change_queue.put(update)
|
||||
|
||||
try:
|
||||
api = await api_connector(value_change_update_callback)
|
||||
except VeilidTestConnectionError:
|
||||
api = await veilid.api_connector(value_change_update_callback)
|
||||
except veilid.VeilidConnectionError:
|
||||
pytest.skip("Unable to connect to veilid-server.")
|
||||
return
|
||||
|
||||
@ -286,7 +286,7 @@ async def test_watch_dht_values():
|
||||
assert vd == [None, None]
|
||||
|
||||
# Wait for the update
|
||||
upd = await asyncio.wait_for(value_change_queue.get(), timeout=5)
|
||||
upd = await asyncio.wait_for(value_change_queue.get(), timeout=10)
|
||||
|
||||
# Verify the update came back but we don't get a new value because the sequence number is the same
|
||||
assert upd.detail.key == rec.key
|
||||
@ -352,14 +352,14 @@ async def test_dht_integration_writer_reader():
|
||||
pass
|
||||
|
||||
try:
|
||||
api0 = await api_connector(null_update_callback, 0)
|
||||
except VeilidTestConnectionError:
|
||||
api0 = await veilid.api_connector(null_update_callback, 0)
|
||||
except veilid.VeilidConnectionError:
|
||||
pytest.skip("Unable to connect to veilid-server 0.")
|
||||
return
|
||||
|
||||
try:
|
||||
api1 = await api_connector(null_update_callback, 1)
|
||||
except VeilidTestConnectionError:
|
||||
api1 = await veilid.api_connector(null_update_callback, 1)
|
||||
except veilid.VeilidConnectionError:
|
||||
pytest.skip("Unable to connect to veilid-server 1.")
|
||||
return
|
||||
|
||||
@ -418,8 +418,8 @@ async def test_dht_write_read_local():
|
||||
pass
|
||||
|
||||
try:
|
||||
api0 = await api_connector(null_update_callback, 0)
|
||||
except VeilidTestConnectionError:
|
||||
api0 = await veilid.api_connector(null_update_callback, 0)
|
||||
except veilid.VeilidConnectionError:
|
||||
pytest.skip("Unable to connect to veilid-server 0.")
|
||||
return
|
||||
|
||||
@ -432,7 +432,8 @@ async def test_dht_write_read_local():
|
||||
rc0 = await api0.new_routing_context()
|
||||
async with rc0:
|
||||
|
||||
COUNT = 500
|
||||
# FIXME: 500
|
||||
COUNT = 5
|
||||
TEST_DATA = b"ABCD"*1024
|
||||
TEST_DATA2 = b"ABCD"*4096
|
||||
|
||||
|
@ -9,8 +9,6 @@ import pytest
|
||||
|
||||
import veilid
|
||||
|
||||
from .api import VeilidTestConnectionError, api_connector
|
||||
|
||||
##################################################################
|
||||
|
||||
|
||||
@ -53,8 +51,8 @@ async def test_routing_context_app_message_loopback():
|
||||
await app_message_queue.put(update)
|
||||
|
||||
try:
|
||||
api = await api_connector(app_message_queue_update_callback)
|
||||
except VeilidTestConnectionError:
|
||||
api = await veilid.api_connector(app_message_queue_update_callback)
|
||||
except veilid.VeilidConnectionError:
|
||||
pytest.skip("Unable to connect to veilid-server.")
|
||||
return
|
||||
|
||||
@ -101,8 +99,8 @@ async def test_routing_context_app_call_loopback():
|
||||
await app_call_queue.put(update)
|
||||
|
||||
try:
|
||||
api = await api_connector(app_call_queue_update_callback)
|
||||
except VeilidTestConnectionError:
|
||||
api = await veilid.api_connector(app_call_queue_update_callback)
|
||||
except veilid.VeilidConnectionError:
|
||||
pytest.skip("Unable to connect to veilid-server.")
|
||||
return
|
||||
|
||||
@ -162,8 +160,8 @@ async def test_routing_context_app_message_loopback_big_packets():
|
||||
sent_messages: set[bytes] = set()
|
||||
|
||||
try:
|
||||
api = await api_connector(app_message_queue_update_callback)
|
||||
except VeilidTestConnectionError:
|
||||
api = await veilid.api_connector(app_message_queue_update_callback)
|
||||
except veilid.VeilidConnectionError:
|
||||
pytest.skip("Unable to connect to veilid-server.")
|
||||
return
|
||||
|
||||
@ -227,8 +225,8 @@ async def test_routing_context_app_call_loopback_big_packets():
|
||||
await api.app_call_reply(update.detail.call_id, update.detail.message)
|
||||
|
||||
try:
|
||||
api = await api_connector(app_call_queue_update_callback)
|
||||
except VeilidTestConnectionError:
|
||||
api = await veilid.api_connector(app_call_queue_update_callback)
|
||||
except veilid.VeilidConnectionError:
|
||||
pytest.skip("Unable to connect to veilid-server.")
|
||||
return
|
||||
|
||||
@ -277,8 +275,8 @@ async def test_routing_context_app_message_loopback_bandwidth():
|
||||
await app_message_queue.put(True)
|
||||
|
||||
try:
|
||||
api = await api_connector(app_message_queue_update_callback)
|
||||
except VeilidTestConnectionError:
|
||||
api = await veilid.api_connector(app_message_queue_update_callback)
|
||||
except veilid.VeilidConnectionError:
|
||||
pytest.skip("Unable to connect to veilid-server.")
|
||||
return
|
||||
|
||||
|
@ -1,4 +1,5 @@
|
||||
from .api import *
|
||||
from .connection import *
|
||||
from .config import *
|
||||
from .error import *
|
||||
from .json_api import *
|
||||
|
@ -1,28 +1,27 @@
|
||||
import appdirs
|
||||
import errno
|
||||
import os
|
||||
import socket
|
||||
import sys
|
||||
import re
|
||||
from collections.abc import Callable
|
||||
from functools import cache
|
||||
|
||||
import veilid
|
||||
from veilid.json_api import _JsonVeilidAPI
|
||||
|
||||
import veilid
|
||||
|
||||
ERRNO_PATTERN = re.compile(r"errno (\d+)", re.IGNORECASE)
|
||||
|
||||
|
||||
class VeilidTestConnectionError(Exception):
|
||||
"""The test client could not connect to the veilid-server."""
|
||||
class VeilidConnectionError(Exception):
|
||||
"""The client could not connect to the veilid-server."""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
@cache
|
||||
def server_info(subindex: int = 0) -> tuple[str, int]:
|
||||
"""Return the hostname and port of the test server."""
|
||||
"""Return the hostname and port of the server."""
|
||||
VEILID_SERVER_NETWORK = os.getenv("VEILID_SERVER_NETWORK")
|
||||
if VEILID_SERVER_NETWORK is None:
|
||||
return "localhost", 5959 + subindex
|
||||
@ -32,6 +31,7 @@ def server_info(subindex: int = 0) -> tuple[str, int]:
|
||||
return hostname, int(rest[0]) + subindex
|
||||
return hostname, 5959 + subindex
|
||||
|
||||
|
||||
def ipc_path_exists(path: str) -> bool:
|
||||
"""Determine if an IPC socket exists in a platform independent way."""
|
||||
if os.name == 'nt':
|
||||
@ -40,17 +40,18 @@ def ipc_path_exists(path: str) -> bool:
|
||||
return path[9:] in os.listdir("\\\\.\\PIPE")
|
||||
else:
|
||||
return os.path.exists(path)
|
||||
|
||||
|
||||
|
||||
@cache
|
||||
def ipc_info(subindex: int = 0) -> str:
|
||||
"""Return the path of the ipc socket of the test server."""
|
||||
"""Return the path of the ipc socket of the server."""
|
||||
VEILID_SERVER_IPC = os.getenv("VEILID_SERVER_IPC")
|
||||
if VEILID_SERVER_IPC is not None:
|
||||
return VEILID_SERVER_IPC
|
||||
|
||||
if os.name == 'nt':
|
||||
return f'\\\\.\\PIPE\\veilid-server\\{subindex}'
|
||||
|
||||
|
||||
ipc_path = f"/var/db/veilid-server/ipc/{subindex}"
|
||||
if os.path.exists(ipc_path):
|
||||
return ipc_path
|
||||
@ -59,7 +60,7 @@ def ipc_info(subindex: int = 0) -> str:
|
||||
if sys.platform.startswith('darwin'):
|
||||
data_dir = appdirs.user_data_dir("org.Veilid.Veilid")
|
||||
else:
|
||||
data_dir = appdirs.user_data_dir("veilid","veilid")
|
||||
data_dir = appdirs.user_data_dir("veilid", "veilid")
|
||||
ipc_path = os.path.join(data_dir, "ipc", str(subindex))
|
||||
return ipc_path
|
||||
|
||||
@ -68,10 +69,10 @@ async def api_connector(callback: Callable, subindex: int = 0) -> _JsonVeilidAPI
|
||||
"""Return an API connection if possible.
|
||||
|
||||
If the connection fails due to an inability to connect to the
|
||||
server's socket, raise an easy-to-catch VeilidTestConnectionError.
|
||||
server's socket, raise an easy-to-catch VeilidConnectionError.
|
||||
"""
|
||||
|
||||
ipc_path = ipc_info(subindex)
|
||||
ipc_path = ipc_info(subindex)
|
||||
|
||||
try:
|
||||
if ipc_path_exists(ipc_path):
|
||||
@ -96,7 +97,7 @@ async def api_connector(callback: Callable, subindex: int = 0) -> _JsonVeilidAPI
|
||||
# it's the code we expected.
|
||||
if exc.errno is not None:
|
||||
if exc.errno == errno.ECONNREFUSED:
|
||||
raise VeilidTestConnectionError
|
||||
raise VeilidConnectionError
|
||||
raise
|
||||
|
||||
# If not, use a regular expression to find all the errno values
|
||||
@ -104,6 +105,6 @@ async def api_connector(callback: Callable, subindex: int = 0) -> _JsonVeilidAPI
|
||||
# code we're looking for.
|
||||
errnos = ERRNO_PATTERN.findall(str(exc))
|
||||
if all(int(err) == errno.ECONNREFUSED for err in errnos):
|
||||
raise VeilidTestConnectionError
|
||||
raise VeilidConnectionError
|
||||
|
||||
raise
|
Loading…
Reference in New Issue
Block a user