refactor(qa): Avoid unnecessary string operations

print_log was recalculated every 0.05s in assert_debug_log(), even during successful circumstances - changed to only be computed upon failure.

Simplified terminology from "(does not) partially match(es)" to "(not) found in" since it seems to reference the first function having used regular expression matching, while it always escaped the search strings (see parent commit). (Simplified grammar also avoids issues with singular/plural "was/were not found").
This commit is contained in:
Hodlinator committed 2025-09-26 22:27:42 +02:00
1 parent a9021101dc
commit 1e54125e2e
1 file changed
+9 -6
+9 -6
View File
@@ -550,6 +550,9 @@ class TestNode():
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
def join_log(log):
return " - " + "\n - ".join(log.splitlines())
yield yield
while True: while True:
@@ -557,10 +560,10 @@ class TestNode():
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()
print_log = " - " + "\n - ".join(log.splitlines())
for unexpected_msg in unexpected_msgs: for unexpected_msg in unexpected_msgs:
if unexpected_msg in log: if unexpected_msg in log:
self._raise_assertion_error('Unexpected message "{}" partially matches log:\n\n{}\n\n'.format(unexpected_msg, print_log)) self._raise_assertion_error(f'Unexpected message "{unexpected_msg}" '
f'found in log:\n\n{join_log(log)}\n\n')
for expected_msg in expected_msgs: for expected_msg in expected_msgs:
if expected_msg not in log: if expected_msg not in log:
found = False found = False
@@ -569,7 +572,8 @@ class TestNode():
if time.time() >= time_end: if time.time() >= time_end:
break break
time.sleep(0.05) time.sleep(0.05)
self._raise_assertion_error('Expected messages "{}" does not partially match log:\n\n{}\n\n'.format(str(expected_msgs), print_log)) self._raise_assertion_error(f'Expected message(s) {expected_msgs!s} '
f'not found in log:\n\n{join_log(log)}\n\n')
@contextlib.contextmanager @contextlib.contextmanager
def busy_wait_for_debug_log(self, expected_msgs, timeout=60): def busy_wait_for_debug_log(self, expected_msgs, timeout=60):
@@ -601,9 +605,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( self._raise_assertion_error(f'Expected message(s) {expected_msgs!s} '
'Expected messages "{}" does not partially match log:\n\n{}\n\n'.format( f'not found in log:\n\n{print_log}\n\n')
str(expected_msgs), print_log))
@contextlib.contextmanager @contextlib.contextmanager
def wait_for_new_peer(self, timeout=5): def wait_for_new_peer(self, timeout=5):