[tests] Improve assert message when wait_until() fails

This commit is contained in:
John Newbery 2017-12-13 14:39:44 -05:00
parent ebf053ac61
commit 265d7c44b1

View File

@ -8,6 +8,7 @@ from base64 import b64encode
from binascii import hexlify, unhexlify from binascii import hexlify, unhexlify
from decimal import Decimal, ROUND_DOWN from decimal import Decimal, ROUND_DOWN
import hashlib import hashlib
import inspect
import json import json
import logging import logging
import os import os
@ -204,9 +205,9 @@ def wait_until(predicate, *, attempts=float('inf'), timeout=float('inf'), lock=N
if attempts == float('inf') and timeout == float('inf'): if attempts == float('inf') and timeout == float('inf'):
timeout = 60 timeout = 60
attempt = 0 attempt = 0
timeout += time.time() time_end = time.time() + timeout
while attempt < attempts and time.time() < timeout: while attempt < attempts and time.time() < time_end:
if lock: if lock:
with lock: with lock:
if predicate(): if predicate():
@ -218,8 +219,12 @@ def wait_until(predicate, *, attempts=float('inf'), timeout=float('inf'), lock=N
time.sleep(0.05) time.sleep(0.05)
# Print the cause of the timeout # Print the cause of the timeout
assert_greater_than(attempts, attempt) predicate_source = inspect.getsourcelines(predicate)
assert_greater_than(timeout, time.time()) logger.error("wait_until() failed. Predicate: {}".format(predicate_source))
if attempt >= attempts:
raise AssertionError("Predicate {} not true after {} attempts".format(predicate_source, attempts))
elif time.time() >= time_end:
raise AssertionError("Predicate {} not true after {} seconds".format(predicate_source, timeout))
raise RuntimeError('Unreachable') raise RuntimeError('Unreachable')
# RPC/P2P connection constants and functions # RPC/P2P connection constants and functions