Add default timeout to wait method to avoid

infinite loop

Change-Id: Iefd3db625c6b68d1ee95f121c628fe87ab64bbd2
Closes-Bug: #1349886
This commit is contained in:
asledzinskiy 2014-07-30 18:13:36 +03:00
parent 1def596d99
commit c85659bb4b
1 changed files with 10 additions and 9 deletions

View File

@ -76,8 +76,8 @@ def tcp_ping(host, port):
return True
def wait(predicate, interval=5, timeout=None):
"""Wait until predicate will become True.
def wait(predicate, interval=5, timeout=60):
"""wait until predicate will become True.
returns number of seconds that is left or 0 if timeout is None.
@ -87,20 +87,21 @@ def wait(predicate, interval=5, timeout=None):
timeout - raise TimeoutError if predicate won't become True after
this amount of seconds. 'None' disables timeout.
"""
start_time = time.time()
if not timeout:
return predicate()
while not predicate():
if timeout and start_time + timeout < time.time():
if start_time + timeout < time.time():
raise TimeoutError("Waiting timed out")
seconds_to_sleep = interval
if timeout:
seconds_to_sleep = max(
0,
min(seconds_to_sleep, start_time + timeout - time.time()))
seconds_to_sleep = max(
0,
min(interval, start_time + timeout - time.time()))
time.sleep(seconds_to_sleep)
return timeout + start_time - time.time() if timeout else 0
return timeout + start_time - time.time()
def _wait(raising_predicate, expected=Exception, interval=5, timeout=None):