Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve pytest warnings #127

Merged
merged 2 commits into from
Nov 29, 2024
Merged
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
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ dev-dependencies = [
"pytest>=7.4.0",
"pytest-asyncio>=0.21.1",
"pytest-cov>=4.1.0",
"pytest-env>=1.1.5",
"pytest-mock>=3.11.1",
"ruff>=0.0.278",
"types-protobuf>=4.24.0.20240311",
Expand Down
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()
14 changes: 14 additions & 0 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading