Skip to content

Commit

Permalink
Resolve pytest warnings (#127)
Browse files Browse the repository at this point in the history
Why
===

Running `pytest` reports two warnings, it should not.

What changed
============

- `client` depends on `NoError`, but since by the time pytest is
initialized the package has already been imported, pytest raises a
warning. Move `client` (and `server`, for completeness,) out into a
plugin.
- `tool.pytest.ini_options.env` isn't recognized without `pytest-env`

Test plan
=========

CI
  • Loading branch information
blast-hardcheese authored Nov 29, 2024
1 parent 5c96f74 commit 7ce86bd
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 62 deletions.
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.

0 comments on commit 7ce86bd

Please sign in to comment.