Skip to content

Instantly share code, notes, and snippets.

@sha1n
Created March 23, 2020 10:54
Show Gist options
  • Save sha1n/ce7be84df5903369a9a84384a6b57f9c to your computer and use it in GitHub Desktop.
Save sha1n/ce7be84df5903369a9a84384a6b57f9c to your computer and use it in GitHub Desktop.
Handy utility function for use in Python tests to wait for a state change triggered by an external or async source
from time import time, sleep
def wait_until(condition_eval_fn: Callable[[], bool], interval_sec: float, timeout_sec: float):
start_time = time()
while not condition_eval_fn() and time() - start_time < timeout_sec:
sleep(interval_sec)
if not condition_eval_fn():
raise TimeoutError("The condition has not been fulfilled within the specified {}s timeout".format(timeout_sec))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment