mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2026-02-12 08:00:54 +00:00
Raise ParseError in read_delim
This commit is contained in:
@ -20,10 +20,10 @@ class MultiselectCommunicator(IMultiselectCommunicator):
|
|||||||
msg_bytes = encode_delim(msg_str.encode())
|
msg_bytes = encode_delim(msg_str.encode())
|
||||||
try:
|
try:
|
||||||
await self.read_writer.write(msg_bytes)
|
await self.read_writer.write(msg_bytes)
|
||||||
except IOException:
|
except IOException as error:
|
||||||
raise MultiselectCommunicatorError(
|
raise MultiselectCommunicatorError(
|
||||||
"fail to write to multiselect communicator"
|
"fail to write to multiselect communicator"
|
||||||
)
|
) from error
|
||||||
|
|
||||||
async def read(self) -> str:
|
async def read(self) -> str:
|
||||||
"""
|
"""
|
||||||
@ -32,8 +32,8 @@ class MultiselectCommunicator(IMultiselectCommunicator):
|
|||||||
try:
|
try:
|
||||||
data = await read_delim(self.read_writer)
|
data = await read_delim(self.read_writer)
|
||||||
# `IOException` includes `IncompleteReadError` and `StreamError`
|
# `IOException` includes `IncompleteReadError` and `StreamError`
|
||||||
except (ParseError, IOException, ValueError):
|
except (ParseError, IOException) as error:
|
||||||
raise MultiselectCommunicatorError(
|
raise MultiselectCommunicatorError(
|
||||||
"fail to read from multiselect communicator"
|
"fail to read from multiselect communicator"
|
||||||
)
|
) from error
|
||||||
return data.decode()
|
return data.decode()
|
||||||
|
|||||||
@ -73,9 +73,8 @@ def encode_delim(msg: bytes) -> bytes:
|
|||||||
|
|
||||||
async def read_delim(reader: Reader) -> bytes:
|
async def read_delim(reader: Reader) -> bytes:
|
||||||
msg_bytes = await read_varint_prefixed_bytes(reader)
|
msg_bytes = await read_varint_prefixed_bytes(reader)
|
||||||
# TODO: Investigate if it is possible to have empty `msg_bytes`
|
if len(msg_bytes) == 0 or msg_bytes[-1:] != b"\n":
|
||||||
if len(msg_bytes) != 0 and msg_bytes[-1:] != b"\n":
|
raise ParseError(f'msg_bytes is not delimited by b"\\n": msg_bytes={msg_bytes}')
|
||||||
raise ValueError(f'msg_bytes is not delimited by b"\\n": msg_bytes={msg_bytes}')
|
|
||||||
return msg_bytes[:-1]
|
return msg_bytes[:-1]
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -22,6 +22,5 @@ async def test_connect(hosts, p2pds):
|
|||||||
assert len(host.get_network().connections) == 1
|
assert len(host.get_network().connections) == 1
|
||||||
# Test: `disconnect` from Go
|
# Test: `disconnect` from Go
|
||||||
await p2pd.control.disconnect(host.get_id())
|
await p2pd.control.disconnect(host.get_id())
|
||||||
# FIXME: Failed to handle disconnect
|
|
||||||
await asyncio.sleep(0.01)
|
await asyncio.sleep(0.01)
|
||||||
assert len(host.get_network().connections) == 0
|
assert len(host.get_network().connections) == 0
|
||||||
|
|||||||
Reference in New Issue
Block a user