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

Servers: Added backend() method to client objects #335

Merged
merged 1 commit into from
Jul 30, 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
4 changes: 4 additions & 0 deletions src/easynetwork/lowlevel/api_async/servers/datagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]):
"""
Expand Down
3 changes: 3 additions & 0 deletions src/easynetwork/servers/async_tcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 3 additions & 0 deletions src/easynetwork/servers/async_udp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
13 changes: 12 additions & 1 deletion src/easynetwork/servers/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]):
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")

Expand All @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down