tests/utils.py: wait_for_log automatic offset.

I have a test which waits for multiple occurrences of the same string,
but doesn't want them to overlap.  Make wait_for_log() do the right thing,
so that it only looks for log entries since the last wait_for_log.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2017-06-20 15:09:03 +09:30
parent e161c11e40
commit 733e0aeccf

View File

@@ -47,6 +47,7 @@ class TailableProc(object):
self.running = False self.running = False
self.proc = None self.proc = None
self.outputDir = outputDir self.outputDir = outputDir
self.logsearch_start = 0
def start(self): def start(self):
"""Start the underlying process and start monitoring it. """Start the underlying process and start monitoring it.
@@ -93,21 +94,21 @@ class TailableProc(object):
logging.debug("Did not find '%s' in logs", regex) logging.debug("Did not find '%s' in logs", regex)
return False return False
def wait_for_log(self, regex, offset=1000, timeout=60): def wait_for_log(self, regex, timeout=60):
"""Look for `regex` in the logs. """Look for `regex` in the logs.
We tail the stdout of the process and look for `regex`, We tail the stdout of the process and look for `regex`,
starting from `offset` lines in the past. We fail if the starting from the previous waited-for log entry (if any). We
timeout is exceeded or if the underlying process exits before fail if the timeout is exceeded or if the underlying process
the `regex` was found. The reason we start `offset` lines in exits before the `regex` was found. The reason we start
the past is so that we can issue a command and not miss its `offset` lines in the past is so that we can issue a command
effects. and not miss its effects.
""" """
logging.debug("Waiting for '%s' in the logs", regex) logging.debug("Waiting for '%s' in the logs", regex)
ex = re.compile(regex) ex = re.compile(regex)
start_time = time.time() start_time = time.time()
pos = max(len(self.logs) - offset, 0) pos = self.logsearch_start
initial_pos = len(self.logs) initial_pos = len(self.logs)
while True: while True:
if time.time() > start_time + timeout: if time.time() > start_time + timeout:
@@ -128,6 +129,7 @@ class TailableProc(object):
if ex.search(self.logs[pos]): if ex.search(self.logs[pos]):
logging.debug("Found '%s' in logs", regex) logging.debug("Found '%s' in logs", regex)
self.logsearch_start = pos+1
return self.logs[pos] return self.logs[pos]
pos += 1 pos += 1