Skip to content

Commit

Permalink
Improve asynchronous unpacking test for 'Connection'
Browse files Browse the repository at this point in the history
  • Loading branch information
friedkeenan committed Nov 2, 2024
1 parent 4a06c90 commit 4f58d2b
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions tests/test_io/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ def create_packet(self, packet_cls, **fields):

class UnsizedPacket(pak.Packet):
class Header(pak.Packet.Header):
id: pak.UInt8
id: pak.ULEB128

class UnsizedStringPacket(UnsizedPacket):
id = 1
Expand Down Expand Up @@ -386,16 +386,26 @@ def __init__(self, *, data=None, ctx=UnsizedPacket.Context()):

super().__init__(reader=reader, writer=writer, ctx=ctx)

async def _read_next_packet(self):
header_data = await self.read_data(UnsizedPacket.Header.size(ctx=self.ctx))
if header_data is None:
async def _try_read_packet(self, packet_cls):
# Handle when we reach EOF.

try:
return await packet_cls.unpack_async(self.reader, ctx=self.ctx)

except pak.Type.UnsuppressedError:
pass

except Exception:
return None

header = UnsizedPacket.Header.unpack(header_data, ctx=self.ctx)
async def _read_next_packet(self):
header = await self._try_read_packet(UnsizedPacket.Header)
if header is None:
return None

packet_cls = UnsizedPacket.subclass_with_id(header.id, ctx=self.ctx)

return await packet_cls.unpack_async(self.reader, ctx=self.ctx)
return await self._try_read_packet(packet_cls)

async def write_packet_instance(self, packet):
await self.write_data(packet.pack(ctx=self.ctx))
Expand Down

0 comments on commit 4f58d2b

Please sign in to comment.