Skip to content

Commit 9cf622c

Browse files
Wescoeurgduperrey
andauthored
Fix wait_for behavior (#224)
* Make `wait_for` more accurate - The old version does not take into account the execution time of `fn`. So instead of waiting 2 minutes (using the default timeout_secs delay), we can end up with much longer delays like 4 minutes. - And for basic functions, the timeout is not respected. For example if I patch common.py like that: ``` def toto(): return False wait_for(toto, timeout_secs=10) ``` Run wait_for call: ``` > time ./common.py Traceback (most recent call last): File "/home/ronan/Projets/sm/./test.py", line 27, in <module> wait_for(toto, timeout_secs=10) File "/home/ronan/Projets/sm/./test.py", line 18, in wait_for raise TimeoutError( Exception: Timeout reached while waiting for fn call to yield True (10). real 0m8,040s user 0m0,021s sys 0m0,016s ``` Signed-off-by: Ronan Abhamon <[email protected]> Co-authored-by: Gael Duperrey <[email protected]>
1 parent 112cf43 commit 9cf622c

File tree

2 files changed

+4
-5
lines changed

2 files changed

+4
-5
lines changed

lib/common.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,14 @@ def prefix_object_name(label):
3232
def wait_for(fn, msg=None, timeout_secs=2 * 60, retry_delay_secs=2, invert=False):
3333
if msg is not None:
3434
logging.info(msg)
35-
time_left = timeout_secs
35+
start_time = time.perf_counter()
3636
while True:
3737
ret = fn()
3838
if not invert and ret:
3939
return
4040
if invert and not ret:
4141
return
42-
time_left -= retry_delay_secs
43-
if time_left <= 0:
42+
if time.perf_counter() - start_time >= timeout_secs:
4443
expected = 'True' if not invert else 'False'
4544
raise TimeoutError(
4645
"Timeout reached while waiting for fn call to yield %s (%s)." % (expected, timeout_secs)

lib/host.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,9 +384,9 @@ def reboot(self, verify=False):
384384
if verify:
385385
wait_for_not(self.is_enabled, "Wait for host down")
386386
wait_for(lambda: not os.system(f"ping -c1 {self.hostname_or_ip} > /dev/null 2>&1"),
387-
"Wait for host up", retry_delay_secs=10)
387+
"Wait for host up", timeout_secs=10 * 60, retry_delay_secs=10)
388388
wait_for(lambda: not os.system(f"nc -zw5 {self.hostname_or_ip} 22"),
389-
"Wait for ssh up on host", retry_delay_secs=5)
389+
"Wait for ssh up on host", timeout_secs=10 * 60, retry_delay_secs=5)
390390
wait_for(self.is_enabled, "Wait for XAPI to be ready", timeout_secs=30 * 60)
391391

392392
def management_network(self):

0 commit comments

Comments
 (0)