Skip to content

Commit

Permalink
[QMI-0xx] Adding some discard_read tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
heevasti committed Dec 18, 2023
1 parent ba818f4 commit 7535d57
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
5 changes: 1 addition & 4 deletions qmi/core/transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -739,10 +739,7 @@ def discard_read(self) -> None:
while True:
try:
b, addr = self._safe_socket.recvfrom(4096)
except socket.timeout:
# no more bytes available
break
except BlockingIOError:
except (BlockingIOError, socket.timeout):
# no more bytes available
break
except OSError:
Expand Down
27 changes: 26 additions & 1 deletion tests/core/test_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,8 @@ async def the_call():
if len(read) != 100:
raise AssertionError from ass

trans.discard_read()

# Send some bytes from server to transport.
loop = asyncio.get_event_loop()
if loop.is_closed():
Expand All @@ -600,9 +602,11 @@ async def the_call():

except qmi.core.exceptions.QMI_TimeoutException as tim:
# Catch this as some servers apparently fragment the message to be max of 4096 bytes, so it does not crash
if len(trans._read_buffer) != 8092:
if len(trans._read_buffer) != 4096:
raise AssertionError from tim

trans.discard_read()

if loop.is_running():
loop.close()

Expand Down Expand Up @@ -710,6 +714,27 @@ def test_read_until_timeout(self):
# Close transport.
trans.close()

def test_discard_read(self):
"""Test discarding read data."""
# Create UDP transport connected to local server.
trans = QMI_UdpTransport("localhost", self.server_port)
trans.open()

# Accept the connection on server side.
self.server_sock.connect((self.server_host, self.server_port + 1))
server_conn = self.server_sock
server_conn.settimeout(1.0)

# Send some bytes from server to transport.
testmsg = b"hello there"
server_conn.sendall(testmsg)

# Discard bytes through the transport.
trans.discard_read()
# Try to read, which should now except as the input buffer was discarded
with self.assertRaises(qmi.core.exceptions.QMI_TimeoutException):
trans.read(1, 0.0)

def test_client_non_existing(self):

# Try to create a server for non-existing client host name.
Expand Down

0 comments on commit 7535d57

Please sign in to comment.