-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Breaking out clientserver fixture module
- Loading branch information
1 parent
502278f
commit 7f528e3
Showing
2 changed files
with
67 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |