From 91edb618c096edac17b5b4859b7233b17d4c41fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Francis=20Clairicia-Rose-Claire-Jos=C3=A9phine?= Date: Tue, 30 Jul 2024 12:14:06 +0200 Subject: [PATCH] Servers: Added `backend()` method to client objects (#335) --- .../lowlevel/api_async/servers/datagram.py | 4 ++++ src/easynetwork/servers/async_tcp.py | 3 +++ src/easynetwork/servers/async_udp.py | 3 +++ src/easynetwork/servers/handlers.py | 13 ++++++++++++- .../test_async/test_server/test_tcp.py | 7 +------ .../test_async/test_server/test_udp.py | 2 +- 6 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/easynetwork/lowlevel/api_async/servers/datagram.py b/src/easynetwork/lowlevel/api_async/servers/datagram.py index 3a07f130..6319d19d 100644 --- a/src/easynetwork/lowlevel/api_async/servers/datagram.py +++ b/src/easynetwork/lowlevel/api_async/servers/datagram.py @@ -62,6 +62,10 @@ class DatagramClientContext(Generic[_T_Response, _T_Address]): server: AsyncDatagramServer[Any, _T_Response, _T_Address] """The server which receives the datagram.""" + @_utils.inherit_doc(_transports.AsyncBaseTransport) + def backend(self) -> AsyncBackend: + return self.server.backend() + class AsyncDatagramServer(_transports.AsyncBaseTransport, Generic[_T_Request, _T_Response, _T_Address]): """ diff --git a/src/easynetwork/servers/async_tcp.py b/src/easynetwork/servers/async_tcp.py index b3435869..88f74063 100644 --- a/src/easynetwork/servers/async_tcp.py +++ b/src/easynetwork/servers/async_tcp.py @@ -405,6 +405,9 @@ async def send_packet(self, packet: _T_Response, /) -> None: raise ClientClosedError("Closed client") await self.__client.send_packet(packet) + def backend(self) -> AsyncBackend: + return self.__client.backend() + @property def extra_attributes(self) -> Mapping[Any, Callable[[], Any]]: if (extra_attributes_cache := self.__extra_attributes_cache) is not None: diff --git a/src/easynetwork/servers/async_udp.py b/src/easynetwork/servers/async_udp.py index 9283634b..0d508a02 100644 --- a/src/easynetwork/servers/async_udp.py +++ b/src/easynetwork/servers/async_udp.py @@ -218,6 +218,9 @@ async def send_packet(self, packet: _T_Response, /) -> None: raise ClientClosedError("Closed client") await server.send_packet_to(packet, address) + def backend(self) -> AsyncBackend: + return self.__context.backend() + @property def extra_attributes(self) -> Mapping[Any, Callable[[], Any]]: if (extra_attributes_cache := self.__extra_attributes_cache) is not None: diff --git a/src/easynetwork/servers/handlers.py b/src/easynetwork/servers/handlers.py index 4be053f4..6a49250a 100644 --- a/src/easynetwork/servers/handlers.py +++ b/src/easynetwork/servers/handlers.py @@ -28,11 +28,14 @@ import contextlib from abc import ABCMeta, abstractmethod from collections.abc import AsyncGenerator, Coroutine -from typing import Any, Generic +from typing import TYPE_CHECKING, Any, Generic from .._typevars import _T_Request, _T_Response from ..lowlevel import socket as socket_tools, typed_attr +if TYPE_CHECKING: + from ..lowlevel.api_async.backend.abc import AsyncBackend + class INETClientAttribute(typed_attr.TypedAttributeSet): socket: socket_tools.ISocket = socket_tools.SocketAttribute.socket @@ -84,6 +87,14 @@ def is_closing(self) -> bool: """ raise NotImplementedError + @abstractmethod + def backend(self) -> AsyncBackend: + """ + Returns: + The backend implementation linked to the server. + """ + raise NotImplementedError + class AsyncStreamClient(AsyncBaseClientInterface[_T_Response]): """ diff --git a/tests/functional_test/test_communication/test_async/test_server/test_tcp.py b/tests/functional_test/test_communication/test_async/test_server/test_tcp.py index 4b678459..38d06ad7 100644 --- a/tests/functional_test/test_communication/test_async/test_server/test_tcp.py +++ b/tests/functional_test/test_communication/test_async/test_server/test_tcp.py @@ -19,7 +19,6 @@ from easynetwork.lowlevel.api_async.backend._asyncio._asyncio_utils import create_connection from easynetwork.lowlevel.api_async.backend._asyncio.backend import AsyncIOBackend from easynetwork.lowlevel.api_async.backend._asyncio.stream.listener import ListenerSocketAdapter -from easynetwork.lowlevel.api_async.backend.abc import AsyncBackend from easynetwork.lowlevel.socket import SocketAddress, enable_socket_linger from easynetwork.protocol import AnyStreamProtocolType from easynetwork.servers.async_tcp import AsyncTCPNetworkServer @@ -186,13 +185,9 @@ async def handle(self, client: AsyncStreamClient[str]) -> AsyncGenerator[float | class TimeoutContextRequestHandler(AsyncStreamRequestHandler[str, str]): - backend: AsyncBackend request_timeout: float = 1.0 timeout_on_second_yield: bool = False - async def service_init(self, exit_stack: contextlib.AsyncExitStack, server: AsyncTCPNetworkServer[Any, Any]) -> None: - self.backend = server.backend() - async def on_connection(self, client: AsyncStreamClient[str]) -> None: await client.send_packet("milk") @@ -202,7 +197,7 @@ async def handle(self, client: AsyncStreamClient[str]) -> AsyncGenerator[None, s await client.send_packet(request) try: with pytest.raises(TimeoutError): - with self.backend.timeout(self.request_timeout): + with client.backend().timeout(self.request_timeout): yield await client.send_packet("successfully timed out") finally: diff --git a/tests/functional_test/test_communication/test_async/test_server/test_udp.py b/tests/functional_test/test_communication/test_async/test_server/test_udp.py index d1a0306b..ebfea429 100644 --- a/tests/functional_test/test_communication/test_async/test_server/test_udp.py +++ b/tests/functional_test/test_communication/test_async/test_server/test_udp.py @@ -155,7 +155,7 @@ async def handle(self, client: AsyncDatagramClient[str]) -> AsyncGenerator[None, await client.send_packet(request) try: with pytest.raises(TimeoutError): - async with asyncio.timeout(self.request_timeout): + with client.backend().timeout(self.request_timeout): yield await client.send_packet("successfully timed out") except BaseException: