OLD | NEW |
| (Empty) |
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
2 # Use of this source code is governed by a BSD-style license that can be | |
3 # found in the LICENSE file. | |
4 import inspect | |
5 import time | |
6 | |
7 class TimeoutException(Exception): | |
8 pass | |
9 | |
10 def WaitFor(condition, | |
11 timeout, poll_interval=0.1, | |
12 pass_time_left_to_func=False): | |
13 assert isinstance(condition, type(lambda: None)) # is function | |
14 start_time = time.time() | |
15 while True: | |
16 if pass_time_left_to_func: | |
17 res = condition((start_time + timeout) - time.time()) | |
18 else: | |
19 res = condition() | |
20 if res: | |
21 break | |
22 if time.time() - start_time > timeout: | |
23 if condition.__name__ == '<lambda>': | |
24 try: | |
25 condition_string = inspect.getsource(condition).strip() | |
26 except IOError: | |
27 condition_string = condition.__name__ | |
28 else: | |
29 condition_string = condition.__name__ | |
30 raise TimeoutException('Timed out while waiting %ds for %s.' % | |
31 (timeout, condition_string)) | |
32 time.sleep(poll_interval) | |
OLD | NEW |