Skip to content

Commit 99ea8d5

Browse files
committed
[testing] Add debug_log_delta
1 parent 0d38d7d commit 99ea8d5

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

test/functional/test_framework/test_node.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import errno
1010
from enum import Enum
1111
import http.client
12+
import datetime
1213
import json
1314
import logging
1415
import os
@@ -382,6 +383,45 @@ def assert_debug_log(self, expected_msgs, unexpected_msgs=None, timeout=2):
382383
time.sleep(0.05)
383384
self._raise_assertion_error('Expected messages "{}" does not partially match log:\n\n{}\n\n'.format(str(expected_msgs), print_log))
384385

386+
@contextlib.contextmanager
387+
def debug_log_delta(self, first_msg, second_msg, timeout=30):
388+
time_end = time.time() + timeout
389+
if self.chain == 'mainnet':
390+
chain_dir = ''
391+
else:
392+
chain_dir = self.chain
393+
394+
debug_log = os.path.join(self.datadir, chain_dir, 'debug.log')
395+
with open(debug_log, encoding='utf-8') as dl:
396+
dl.seek(0, 2)
397+
prev_size = dl.tell()
398+
399+
yield
400+
401+
DEBUG_LOG_TIMESTAMP = "^\d{4}-\d{2}-\d{2}T(\d{2}:\d{2}:\d{2}.\d{6})Z"
402+
found_first = False
403+
404+
while True:
405+
with open(debug_log, encoding='utf-8') as dl:
406+
dl.seek(prev_size)
407+
log = dl.read()
408+
print_log = " - " + "\n - ".join(log.splitlines())
409+
if not found_first:
410+
m = re.search(DEBUG_LOG_TIMESTAMP + ".*" + first_msg + ".*$", log, re.MULTILINE)
411+
if m:
412+
time_first = datetime.datetime.strptime(m.groups()[0], "%H:%M:%S.%f")
413+
found_first = True
414+
if found_first:
415+
m2 = re.search(DEBUG_LOG_TIMESTAMP + ".*" + second_msg + ".*$", log, re.MULTILINE)
416+
if m2:
417+
time_second = datetime.datetime.strptime(m2.groups()[0], "%H:%M:%S.%f")
418+
print("{} to {}: {}ms".format(first_msg, second_msg, (time_second - time_first).microseconds / 1000))
419+
return
420+
if time.time() >= time_end:
421+
break
422+
time.sleep(0.05)
423+
self._raise_assertion_error('Expected messages "{}" does not partially match log:\n\n{}\n\n'.format(first_msg, print_log))
424+
385425
@contextlib.contextmanager
386426
def profile_with_perf(self, profile_name):
387427
"""

0 commit comments

Comments
 (0)