tests: generalize wait_for_log into wait_for_logs.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2017-07-01 16:40:18 +09:30
parent c05f63a4db
commit 9fae82c175

View file

@ -94,31 +94,30 @@ 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, timeout=60): def wait_for_logs(self, regexs, timeout=60):
"""Look for `regex` in the logs. """Look for `regexs` in the logs.
We tail the stdout of the process and look for `regex`, We tail the stdout of the process and look for each regex in `regexs`,
starting from the previous waited-for log entry (if any). We starting from last of the previous waited-for log entries (if any). We
fail if the timeout is exceeded or if the underlying process fail if the timeout is exceeded or if the underlying process
exits before the `regex` was found. The reason we start exits before all the `regexs` were found.
`offset` lines in the past is so that we can issue a command
and not miss its effects.
""" """
logging.debug("Waiting for '%s' in the logs", regex) logging.debug("Waiting for {} in the logs".format(regexs))
ex = re.compile(regex) exs = {re.compile(r) for r in regexs}
start_time = time.time() start_time = time.time()
pos = self.logsearch_start 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:
print("Can't find {} in logs".format(regex)) print("Can't find {} in logs".format(exs))
with self.logs_cond: with self.logs_cond:
for i in range(initial_pos, len(self.logs)): for i in range(initial_pos, len(self.logs)):
print(" " + self.logs[i]) print(" " + self.logs[i])
if self.is_in_log(regex): for r in exs:
print("(Was previously in logs!") if self.is_in_log(r):
raise TimeoutError('Unable to find "{}" in logs.'.format(regex)) print("({} was previously in logs!)".format(r))
raise TimeoutError('Unable to find "{}" in logs.'.format(exs))
elif not self.running: elif not self.running:
raise ValueError('Process died while waiting for logs') raise ValueError('Process died while waiting for logs')
@ -127,12 +126,21 @@ class TailableProc(object):
self.logs_cond.wait(1) self.logs_cond.wait(1)
continue continue
if ex.search(self.logs[pos]): for r in exs.copy():
logging.debug("Found '%s' in logs", regex) if r.search(self.logs[pos]):
logging.debug("Found '%s' in logs", r)
exs.remove(r)
self.logsearch_start = pos+1 self.logsearch_start = pos+1
if len(exs) == 0:
return self.logs[pos] return self.logs[pos]
pos += 1 pos += 1
def wait_for_log(self, regex, timeout=60):
"""Look for `regex` in the logs.
Convenience wrapper for the common case of only seeking a single entry.
"""
return self.wait_for_logs([regex], timeout)
class SimpleBitcoinProxy: class SimpleBitcoinProxy:
"""Wrapper for BitcoinProxy to reconnect. """Wrapper for BitcoinProxy to reconnect.