Skip to content

Commit

Permalink
pyln-testing: improve wait_for a bit
Browse files Browse the repository at this point in the history
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
  • Loading branch information
m-schmoock authored and rustyrussell committed Feb 4, 2021
1 parent 9da191e commit d76ca6e
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions contrib/pyln-testing/pyln/testing/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,14 @@ def env(name, default=None):
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'):
Expand Down

0 comments on commit d76ca6e

Please sign in to comment.