From 7f528e3300eb6e48222ee68a8e049d5479dd453f Mon Sep 17 00:00:00 2001 From: Devon Stewart Date: Thu, 28 Nov 2024 12:04:23 -0800 Subject: [PATCH] Breaking out clientserver fixture module --- tests/conftest.py | 64 +-------------------------- tests/river_fixtures/clientserver.py | 65 ++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 62 deletions(-) create mode 100644 tests/river_fixtures/clientserver.py diff --git a/tests/conftest.py b/tests/conftest.py index e429135..16ee827 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,6 +1,4 @@ -import asyncio -import logging -from typing import Any, AsyncGenerator, Literal, Mapping +from typing import Any, Mapping import nanoid import pytest @@ -8,21 +6,15 @@ from opentelemetry.sdk.trace import TracerProvider from opentelemetry.sdk.trace.export import SimpleSpanProcessor from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter -from websockets.server import serve -from replit_river.client import Client -from replit_river.client_transport import UriAndMetadata from replit_river.error_schema import RiverError from replit_river.rpc import ( GenericRpcHandler, TransportMessage, ) -from replit_river.server import Server -from replit_river.transport_options import TransportOptions -from tests.river_fixtures.logging import NoErrors # Modular fixtures -pytest_plugins = ["tests.river_fixtures.logging"] +pytest_plugins = ["tests.river_fixtures.logging", "tests.river_fixtures.clientserver"] HandlerMapping = Mapping[tuple[str, str], tuple[str, GenericRpcHandler]] @@ -68,58 +60,6 @@ def deserialize_error(response: dict) -> RiverError: return RiverError.model_validate(response) -@pytest.fixture -def transport_options() -> TransportOptions: - return TransportOptions() - - -@pytest.fixture -def server_handlers(handlers: HandlerMapping) -> HandlerMapping: - return handlers - - -@pytest.fixture -def server( - transport_options: TransportOptions, server_handlers: HandlerMapping -) -> Server: - server = Server(server_id="test_server", transport_options=transport_options) - server.add_rpc_handlers(server_handlers) - return server - - -@pytest.fixture -async def client( - server: Server, - transport_options: TransportOptions, - no_logging_error: NoErrors, -) -> AsyncGenerator[Client, None]: - async def websocket_uri_factory() -> UriAndMetadata[None]: - return { - "uri": "ws://localhost:8765", - "metadata": None, - } - - try: - async with serve(server.serve, "localhost", 8765): - client: Client[Literal[None]] = Client[None]( - uri_and_metadata_factory=websocket_uri_factory, - client_id="test_client", - server_id="test_server", - transport_options=transport_options, - ) - try: - yield client - finally: - logging.debug("Start closing test client : %s", "test_client") - await client.close() - finally: - await asyncio.sleep(1) - logging.debug("Start closing test server") - await server.close() - # Server should close normally - no_logging_error() - - @pytest.fixture(scope="session") def span_exporter() -> InMemorySpanExporter: exporter = InMemorySpanExporter() diff --git a/tests/river_fixtures/clientserver.py b/tests/river_fixtures/clientserver.py new file mode 100644 index 0000000..b54489b --- /dev/null +++ b/tests/river_fixtures/clientserver.py @@ -0,0 +1,65 @@ +import asyncio +import logging +from typing import AsyncGenerator, Literal + +import pytest +from websockets.server import serve + +from replit_river.client import Client +from replit_river.client_transport import UriAndMetadata +from replit_river.server import Server +from replit_river.transport_options import TransportOptions +from tests.conftest import HandlerMapping +from tests.river_fixtures.logging import NoErrors # noqa: E402 + + +@pytest.fixture +def transport_options() -> TransportOptions: + return TransportOptions() + + +@pytest.fixture +def server_handlers(handlers: HandlerMapping) -> HandlerMapping: + return handlers + + +@pytest.fixture +def server( + transport_options: TransportOptions, server_handlers: HandlerMapping +) -> Server: + server = Server(server_id="test_server", transport_options=transport_options) + server.add_rpc_handlers(server_handlers) + return server + + +@pytest.fixture +async def client( + server: Server, + transport_options: TransportOptions, + no_logging_error: NoErrors, +) -> AsyncGenerator[Client, None]: + async def websocket_uri_factory() -> UriAndMetadata[None]: + return { + "uri": "ws://localhost:8765", + "metadata": None, + } + + try: + async with serve(server.serve, "localhost", 8765): + client: Client[Literal[None]] = Client[None]( + uri_and_metadata_factory=websocket_uri_factory, + client_id="test_client", + server_id="test_server", + transport_options=transport_options, + ) + try: + yield client + finally: + logging.debug("Start closing test client : %s", "test_client") + await client.close() + finally: + await asyncio.sleep(1) + logging.debug("Start closing test server") + await server.close() + # Server should close normally + no_logging_error()