• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# Lint as: python3
3"""
4Utility class for various test usage.
5"""
6
7import time
8
9from collections.abc import Callable
10
11WAIT_DEFAULT_TIMEOUT = 5
12WAIT_DEFAULT_POLLING_INTERVAL = 0.2
13
14def wait(condition: Callable[[], bool], timeout: int = WAIT_DEFAULT_TIMEOUT, interval: int = WAIT_DEFAULT_POLLING_INTERVAL) -> bool:
15    """
16    Wait until condition becomes true before timing out.
17    Return true if condition is met, and false otherwise.
18    """
19    start_time = time.time()
20    while not condition():
21        elapsed_time = time.time() - start_time
22        if elapsed_time >= timeout:
23            return False
24        time.sleep(interval)
25    return True
26