Skip to content

Commit

Permalink
Breaking out clientserver fixture module
Browse files Browse the repository at this point in the history
  • Loading branch information
blast-hardcheese committed Nov 28, 2024
1 parent 502278f commit 7f528e3
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 62 deletions.
64 changes: 2 additions & 62 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,20 @@
import asyncio
import logging
from typing import Any, AsyncGenerator, Literal, Mapping
from typing import Any, Mapping

import nanoid
import pytest
from opentelemetry import trace
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]]

Expand Down Expand Up @@ -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()
Expand Down
65 changes: 65 additions & 0 deletions tests/river_fixtures/clientserver.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 7f528e3

Please sign in to comment.