diff --git a/avocado/utils/wait.py b/avocado/utils/wait.py new file mode 100644 index 0000000000000000000000000000000000000000..91f184d37fc41299732c88e8f80115ab7210d146 --- /dev/null +++ b/avocado/utils/wait.py @@ -0,0 +1,34 @@ +import logging +import time + +log = logging.getLogger('avocado.test') + + +def wait_for(func, timeout, first=0.0, step=1.0, text=None): + """ + Wait until func() evaluates to True. + + If func() evaluates to True before timeout expires, return the + value of func(). Otherwise return None. + + :param timeout: Timeout in seconds + :param first: Time to sleep before first attempt + :param steps: Time to sleep between attempts in seconds + :param text: Text to print while waiting, for debug purposes + """ + start_time = time.time() + end_time = time.time() + timeout + + time.sleep(first) + + while time.time() < end_time: + if text: + log.debug("%s (%f secs)", text, (time.time() - start_time)) + + output = func() + if output: + return output + + time.sleep(step) + + return None