Created
March 23, 2020 10:54
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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