Skip to content

Commit

Permalink
Fiix flaky test
Browse files Browse the repository at this point in the history
Was flaky due to communication log being reset shortly after starting readerthread sometimes clearing first command.
  • Loading branch information
mvdwetering committed Mar 1, 2024
1 parent 2a89985 commit d9a2079
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 13 deletions.
1 change: 0 additions & 1 deletion requirements_test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@
pytest>=7.0.0
pytest-mock
pytest-cov
flaky==3.7.0
mock-serial==0.0.1
9 changes: 1 addition & 8 deletions tests/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from unittest import mock

import pytest
from flaky import flaky
from mock_serial import MockSerial

from ynca.connection import YncaConnection, YncaProtocolStatus
Expand Down Expand Up @@ -224,10 +223,6 @@ def test_protocol_status(mock_serial):
def test_get_communication_log_items(mock_serial):

with active_connection(mock_serial, communication_log_size=5) as connection:
raw_data = mock_serial.stub(
receive_bytes=b"@Subunit:Function=?\r\n", send_bytes=b""
)

time.sleep(SHORT_DELAY)
logitems = connection.get_communication_log_items()
assert len(logitems) == 4 # Send en received keep-alive
Expand All @@ -238,8 +233,6 @@ def test_get_communication_log_items(mock_serial):
assert len(logitems) == 5 # 1 dropped out due to size limit


# Flaky due to multiple threads needing to communicate.
@flaky(max_runs=5)
def test_keep_alive(mock_serial):

# Tweak the internal keep-alive interval to keep test short
Expand All @@ -252,7 +245,7 @@ def test_keep_alive(mock_serial):
message_callback = mock.MagicMock()
connection.register_message_callback(message_callback)

time.sleep(1)
time.sleep(SHORT_DELAY)
logitems = connection.get_communication_log_items()
assert len(logitems) == 4 # Send en received keep-alive are logged

Expand Down
7 changes: 3 additions & 4 deletions ynca/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class YncaProtocol(serial.threaded.LineReader):
# YNCA spec says standby timeout is 40 seconds, so use a shorter period to be on the safe side
KEEP_ALIVE_INTERVAL = 30

def __init__(self):
def __init__(self, communication_log_size=0):
super().__init__()
self.message_callback:Callable[[YncaProtocolStatus, str|None, str|None, str|None], None]
self.disconnect_callback:Callable[[], None] | None
Expand All @@ -44,7 +44,7 @@ def __init__(self):
self._last_sent_command = None
self.connected = False
self._keep_alive_pending = False
self._communication_log_buffer: LogBuffer = LogBuffer(0)
self._communication_log_buffer: LogBuffer = LogBuffer(communication_log_size)
self.num_commands_sent = 0

def connection_made(self, transport):
Expand Down Expand Up @@ -222,7 +222,7 @@ def connect(
try:
self._serial = serial.serial_for_url(self._port)
self._readerthread = serial.threaded.ReaderThread(
self._serial, YncaProtocol
self._serial, lambda: YncaProtocol(communication_log_size)
)
self._readerthread.start()
_, protocol = self._readerthread.connect()
Expand All @@ -235,7 +235,6 @@ def connect(
if self._protocol:
self._protocol.message_callback = self._call_registered_message_callbacks
self._protocol.disconnect_callback = disconnect_callback
self._protocol.set_communication_log_size(communication_log_size)

def close(self):
# Disconnect callback is for unexpected disconnects
Expand Down

0 comments on commit d9a2079

Please sign in to comment.