-
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.
tests: simplify and document the emulator server.
Signed-off-by: Romain Bezut <morian@xdec.net>
- Loading branch information
Showing
8 changed files
with
113 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
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
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,63 @@ | ||
from __future__ import annotations | ||
|
||
from asyncio import start_unix_server | ||
from typing import TYPE_CHECKING | ||
|
||
from anyio import TASK_STATUS_IGNORED | ||
|
||
from .device import EmulatedDevice | ||
|
||
if TYPE_CHECKING: | ||
from asyncio import StreamReader, StreamWriter | ||
|
||
from anyio.abc import TaskStatus | ||
|
||
|
||
class EmulatorServer: | ||
"""Server used to emulate LD2410 devices on top of an unix socket.""" | ||
|
||
def __init__(self, socket_path: str) -> None: | ||
""" | ||
Create an emulation server for LD2410 devices on top of an unix socket. | ||
Args: | ||
socket_path: path to the unix socket | ||
""" | ||
self._socket_path = socket_path | ||
|
||
@property | ||
def socket_path(self) -> str: | ||
"""Get the server's unix socket path.""" | ||
return self._socket_path | ||
|
||
async def _handle_connection(self, reader: StreamReader, writer: StreamWriter) -> None: | ||
""" | ||
Handle a new connection for a new emulated device. | ||
This coroutine runs in a new :class:`asyncio.Task` as stated in the documentation | ||
from :meth:`start_unix_server`, which means that we don't have to handle anything | ||
else than the emulated device here. | ||
Args: | ||
reader: the read part of the connection stream | ||
writer: the write part of the connection stream | ||
""" | ||
async with EmulatedDevice(reader, writer) as device: | ||
await device.wait_for_closing() | ||
|
||
async def run(self, *, task_status: TaskStatus[None] = TASK_STATUS_IGNORED) -> None: | ||
""" | ||
Serve incoming connection requests forever. | ||
Connection server is closed when the underlying task is cancelled. | ||
Keyword Args: | ||
task_status: anyio specific used to tell when we are ready to serve | ||
""" | ||
server = await start_unix_server(self._handle_connection, self._socket_path) | ||
async with server: | ||
task_status.started() | ||
await server.serve_forever() |
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
anyio==4.6.0 | ||
coverage==7.6.1 | ||
coverage==7.6.2 | ||
pytest-cov==5.0.0 | ||
pytest-timeout==2.3.1 | ||
pytest==8.3.3 |
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