qa: Only complain about expected messages that were not found

Only leaves messages we never found in the final assert message of the functions, which is more helpful (remaining_expected).

Avoids repeatedly searching for messages we have already found (pop()).

Stops searching for other expected messages if we already failed finding one. Still need to clean remaining_expected at the end, but *only if we fail*.

Co-authored-by: Lőrinc <pap.lorinc@gmail.com>
This commit is contained in:
HodlinatorandLőrinc committed 2025-09-26 23:15:15 +02:00
1 parent 1e54125e2e
commit a1f7623020
1 file changed
+12 -13
+12 -13
View File
@@ -546,6 +546,7 @@ class TestNode():
unexpected_msgs = [] unexpected_msgs = []
assert_equal(type(expected_msgs), list) assert_equal(type(expected_msgs), list)
assert_equal(type(unexpected_msgs), list) assert_equal(type(unexpected_msgs), list)
remaining_expected = list(expected_msgs)
time_end = time.time() + timeout * self.timeout_factor time_end = time.time() + timeout * self.timeout_factor
prev_size = self.debug_log_size(encoding="utf-8") # Must use same encoding that is used to read() below prev_size = self.debug_log_size(encoding="utf-8") # Must use same encoding that is used to read() below
@@ -556,7 +557,6 @@ class TestNode():
yield yield
while True: while True:
found = True
with open(self.debug_log_path, encoding="utf-8", errors="replace") as dl: with open(self.debug_log_path, encoding="utf-8", errors="replace") as dl:
dl.seek(prev_size) dl.seek(prev_size)
log = dl.read() log = dl.read()
@@ -564,15 +564,15 @@ class TestNode():
if unexpected_msg in log: if unexpected_msg in log:
self._raise_assertion_error(f'Unexpected message "{unexpected_msg}" ' self._raise_assertion_error(f'Unexpected message "{unexpected_msg}" '
f'found in log:\n\n{join_log(log)}\n\n') f'found in log:\n\n{join_log(log)}\n\n')
for expected_msg in expected_msgs: while remaining_expected and remaining_expected[-1] in log:
if expected_msg not in log: remaining_expected.pop()
found = False if not remaining_expected:
if found:
return return
if time.time() >= time_end: if time.time() >= time_end:
break break
time.sleep(0.05) time.sleep(0.05)
self._raise_assertion_error(f'Expected message(s) {expected_msgs!s} ' remaining_expected = [e for e in remaining_expected if e not in log]
self._raise_assertion_error(f'Expected message(s) {remaining_expected!s} '
f'not found in log:\n\n{join_log(log)}\n\n') f'not found in log:\n\n{join_log(log)}\n\n')
@contextlib.contextmanager @contextlib.contextmanager
@@ -582,20 +582,18 @@ class TestNode():
""" """
time_end = time.time() + timeout * self.timeout_factor time_end = time.time() + timeout * self.timeout_factor
prev_size = self.debug_log_size(mode="rb") # Must use same mode that is used to read() below prev_size = self.debug_log_size(mode="rb") # Must use same mode that is used to read() below
remaining_expected = list(expected_msgs)
yield yield
while True: while True:
found = True
with open(self.debug_log_path, "rb") as dl: with open(self.debug_log_path, "rb") as dl:
dl.seek(prev_size) dl.seek(prev_size)
log = dl.read() log = dl.read()
for expected_msg in expected_msgs: while remaining_expected and remaining_expected[-1] in log:
if expected_msg not in log: remaining_expected.pop()
found = False if not remaining_expected:
if found:
return return
if time.time() >= time_end: if time.time() >= time_end:
@@ -605,7 +603,8 @@ class TestNode():
# No sleep here because we want to detect the message fragment as fast as # No sleep here because we want to detect the message fragment as fast as
# possible. # possible.
self._raise_assertion_error(f'Expected message(s) {expected_msgs!s} ' remaining_expected = [e for e in remaining_expected if e not in log]
self._raise_assertion_error(f'Expected message(s) {remaining_expected!s} '
f'not found in log:\n\n{print_log}\n\n') f'not found in log:\n\n{print_log}\n\n')
@contextlib.contextmanager @contextlib.contextmanager