diff options
author | John Snow <jsnow@redhat.com> | 2021-09-15 12:29:54 -0400 |
---|---|---|
committer | John Snow <jsnow@redhat.com> | 2021-09-27 12:10:29 -0400 |
commit | 8193b9d148e0d42794b78ab040c804db15b5f524 (patch) | |
tree | 1936bafdbbc75a38abf9f98f4e237d8bc6b09a54 /python | |
parent | a1f71b61ea763b5ac7429940018c1bb697889e56 (diff) |
python/aqmp: add LineProtocol tests
Tests a real connect, a real accept, and really sending and receiving a
message over a UNIX socket.
Brings coverage of protocol.py up to ~93%.
Signed-off-by: John Snow <jsnow@redhat.com>
Message-id: 20210915162955.333025-27-jsnow@redhat.com
Signed-off-by: John Snow <jsnow@redhat.com>
Diffstat (limited to 'python')
-rw-r--r-- | python/tests/protocol.py | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/python/tests/protocol.py b/python/tests/protocol.py index f0682d29ce..5cd7938be3 100644 --- a/python/tests/protocol.py +++ b/python/tests/protocol.py @@ -78,6 +78,25 @@ class NullProtocol(AsyncProtocol[None]): self._schedule_disconnect() +class LineProtocol(AsyncProtocol[str]): + def __init__(self, name=None): + super().__init__(name) + self.rx_history = [] + + async def _do_recv(self) -> str: + raw = await self._readline() + msg = raw.decode() + self.rx_history.append(msg) + return msg + + def _do_send(self, msg: str) -> None: + assert self._writer is not None + self._writer.write(msg.encode() + b'\n') + + async def send_msg(self, msg: str) -> None: + await self._outgoing.put(msg) + + def run_as_task(coro, allow_cancellation=False): """ Run a given coroutine as a task. @@ -533,3 +552,32 @@ class FakeSession(TestBase): " Call disconnect() to return to IDLE state."), accept=False, ) + + +class SimpleSession(TestBase): + + def setUp(self): + super().setUp() + self.server = LineProtocol(type(self).__name__ + '-server') + + async def _asyncSetUp(self): + await super()._asyncSetUp() + await self._watch_runstates(*self.GOOD_CONNECTION_STATES) + + async def _asyncTearDown(self): + await self.proto.disconnect() + try: + await self.server.disconnect() + except EOFError: + pass + await super()._asyncTearDown() + + @TestBase.async_test + async def testSmoke(self): + with TemporaryDirectory(suffix='.aqmp') as tmpdir: + sock = os.path.join(tmpdir, type(self.proto).__name__ + ".sock") + server_task = create_task(self.server.accept(sock)) + + # give the server a chance to start listening [...] + await asyncio.sleep(0) + await self.proto.connect(sock) |