avocado.utils: Add wait library

Add a library for functions with the same pattern: wait for
another function to evaluate to True, for a certain amount
of time.
Signed-off-by: NLucas Meneghel Rodrigues <lmr@redhat.com>
上级 ac387a0f
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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册