Sleep until the predicate resolves to be True. Warning: Note that this method is not recommended to be used in tests as it is not aware of the context of the test framework. Using the `wait_until()` members from `BitcoinTestFramework` or `P2PInterface` class ensures the timeout is p
(predicate, *, attempts=float('inf'), timeout=float('inf'), lock=None, timeout_factor=1.0)
| 254 | |
| 255 | |
| 256 | def wait_until_helper(predicate, *, attempts=float('inf'), timeout=float('inf'), lock=None, timeout_factor=1.0): |
| 257 | """Sleep until the predicate resolves to be True. |
| 258 | |
| 259 | Warning: Note that this method is not recommended to be used in tests as it is |
| 260 | not aware of the context of the test framework. Using the `wait_until()` members |
| 261 | from `BitcoinTestFramework` or `P2PInterface` class ensures the timeout is |
| 262 | properly scaled. Furthermore, `wait_until()` from `P2PInterface` class in |
| 263 | `p2p.py` has a preset lock. |
| 264 | """ |
| 265 | if attempts == float('inf') and timeout == float('inf'): |
| 266 | timeout = 60 |
| 267 | timeout = timeout * timeout_factor |
| 268 | attempt = 0 |
| 269 | time_end = time.time() + timeout |
| 270 | |
| 271 | while attempt < attempts and time.time() < time_end: |
| 272 | if lock: |
| 273 | with lock: |
| 274 | if predicate(): |
| 275 | return |
| 276 | else: |
| 277 | if predicate(): |
| 278 | return |
| 279 | attempt += 1 |
| 280 | time.sleep(0.05) |
| 281 | |
| 282 | # Print the cause of the timeout |
| 283 | predicate_source = "''''\n" + inspect.getsource(predicate) + "'''" |
| 284 | logger.error("wait_until() failed. Predicate: {}".format(predicate_source)) |
| 285 | if attempt >= attempts: |
| 286 | raise AssertionError("Predicate {} not true after {} attempts".format(predicate_source, attempts)) |
| 287 | elif time.time() >= time_end: |
| 288 | raise AssertionError("Predicate {} not true after {} seconds".format(predicate_source, timeout)) |
| 289 | raise RuntimeError('Unreachable') |
| 290 | |
| 291 | |
| 292 | def sha256sum_file(filename): |