Skip to content

Commit

Permalink
tests: Fix uart_ordered() check
Browse files Browse the repository at this point in the history
This implementation had issues.

Signed-off-by: Jorgen Kvalvaag <[email protected]>
  • Loading branch information
jorgenmk committed Nov 13, 2024
1 parent 346f7a6 commit 534c8cb
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
38 changes: 36 additions & 2 deletions tests/on_target/utils/test_uart.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,47 @@ def test_wait_ordered_2_missing(time_sleep, time_time):
u.log = "foo123\nbar123\nbaz123\n"
with pytest.raises(AssertionError) as ex_info:
u.wait_for_str_ordered(["foo", "bar", "baz", "1234"], timeout=3)
assert "1234" in str(ex_info.value)
assert "1234 missing" in str(ex_info.value)

@patch("time.time", side_effect=counter())
@patch("time.sleep")
def test_wait_ordered_2_out_of_order(time_sleep, time_time):
def test_wait_ordered_3_out_of_order(time_sleep, time_time):
u = mocked_uart()
u.log = "foo123\nbar123\nbaz123\n"
with pytest.raises(AssertionError) as ex_info:
u.wait_for_str_ordered(["foo", "baz", "bar"], timeout=3)
assert "bar missing" in str(ex_info.value)

@patch("time.time", side_effect=counter())
@patch("time.sleep")
def test_wait_ordered_4_multiple(time_sleep, time_time):
u = mocked_uart()
u.log = "foo123\nbar123\nbaz123\nfoo123\nfoo123\nbar123\n"
u.wait_for_str_ordered(["foo", "foo", "foo"], timeout=3)

@patch("time.time", side_effect=counter())
@patch("time.sleep")
def test_wait_ordered_5_overflow(time_sleep, time_time):
u = mocked_uart()
u.log = "foo123\nbar123\nbaz123\nfoo123\nfoo123\nbar123\n"
with pytest.raises(AssertionError) as ex_info:
u.wait_for_str_ordered(["foo", "foo", "foo", "foo"], timeout=3)
assert "foo missing" in str(ex_info.value)

@patch("time.time", side_effect=counter())
@patch("time.sleep")
def test_wait_ordered_6_out_of_order(time_sleep, time_time):
u = mocked_uart()
u.log = "foo123\nbar123\nbaz123\nfoo123\nfoo123\nbar123\n"
with pytest.raises(AssertionError) as ex_info:
u.wait_for_str_ordered(["foo", "bar", "foo", "baz"], timeout=3)
assert "baz missing" in str(ex_info.value)

@patch("time.time", side_effect=counter())
@patch("time.sleep")
def test_wait_ordered_7_none(time_sleep, time_time):
u = mocked_uart()
u.log = "foo123\nbar123\nbaz123\nfoo123\nfoo123\nbar123\n"
with pytest.raises(AssertionError) as ex_info:
u.wait_for_str_ordered(["abc", "def", "ghi", "jkl"], timeout=2)
assert "abc missing" in str(ex_info.value)
15 changes: 8 additions & 7 deletions tests/on_target/utils/uart.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,21 +159,22 @@ def wait_for_str_ordered(
self, msgs: list, error_msg: str = "", timeout: int = DEFAULT_WAIT_FOR_STR_TIMEOUT
) -> None:
start = time.time()

while True:
time.sleep(1)
missing = None
pos = 0
missing_msg = None
for msg in msgs:
pos = self.log.find(msg, pos)
if pos == -1:
missing_msg = msg
try:
pos = self.log.index(msg, pos)
except ValueError:
missing = msg
break
if not missing_msg:
pos += 1
else:
break
if start + timeout < time.time():
raise AssertionError(
f"{missing_msg} missing in UART log in the expected order. {error_msg}\nUart log:\n{self.log}"
f"{missing if missing else msgs} missing in UART log in the expected order. {error_msg}\nUart log:\n{self.log}"
)
if self._evt.is_set():
raise RuntimeError(f"Uart thread stopped, log:\n{self.log}")
Expand Down

0 comments on commit 534c8cb

Please sign in to comment.