From d76ca6ed35e2e586dca2dd8de67372aa5cb540f5 Mon Sep 17 00:00:00 2001 From: Michael Schmoock Date: Wed, 3 Feb 2021 12:14:38 +0100 Subject: [PATCH] pyln-testing: improve `wait_for` a bit This 'fixes' the `wait_for` helper by removing a pointless final `time.sleep()`, thus potentially making the method return quicker. The old code could have had three final states: - success() := True - Timeout and success() := True - Timeout and success() := False The new code has just two final state: - success() := True - Timeout and success() := False It ensures the final `time.sleep()` is just the right amount before timeout. And more importantly making it more readable :-) Changelog-None --- contrib/pyln-testing/pyln/testing/utils.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/contrib/pyln-testing/pyln/testing/utils.py b/contrib/pyln-testing/pyln/testing/utils.py index 905cd1262..40fa38cf7 100644 --- a/contrib/pyln-testing/pyln/testing/utils.py +++ b/contrib/pyln-testing/pyln/testing/utils.py @@ -84,13 +84,14 @@ TIMEOUT = int(env("TIMEOUT", 180 if SLOW_MACHINE else 60)) def wait_for(success, timeout=TIMEOUT): start_time = time.time() interval = 0.25 - while not success() and time.time() < start_time + timeout: - time.sleep(interval) + while not success(): + time_left = start_time + timeout - time.time() + if time_left <= 0: + raise ValueError("Timeout while waiting for {}", success) + time.sleep(min(interval, time_left)) interval *= 2 if interval > 5: interval = 5 - if time.time() > start_time + timeout: - raise ValueError("Error waiting for {}", success) def write_config(filename, opts, regtest_opts=None, section_name='regtest'):