Skip to content

Commit 265d7c4

Browse files
committed
[tests] Improve assert message when wait_until() fails
1 parent ebf053a commit 265d7c4

File tree

1 file changed

+9
-4
lines changed
  • test/functional/test_framework

1 file changed

+9
-4
lines changed

test/functional/test_framework/util.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from binascii import hexlify, unhexlify
99
from decimal import Decimal, ROUND_DOWN
1010
import hashlib
11+
import inspect
1112
import json
1213
import logging
1314
import os
@@ -204,9 +205,9 @@ def wait_until(predicate, *, attempts=float('inf'), timeout=float('inf'), lock=N
204205
if attempts == float('inf') and timeout == float('inf'):
205206
timeout = 60
206207
attempt = 0
207-
timeout += time.time()
208+
time_end = time.time() + timeout
208209

209-
while attempt < attempts and time.time() < timeout:
210+
while attempt < attempts and time.time() < time_end:
210211
if lock:
211212
with lock:
212213
if predicate():
@@ -218,8 +219,12 @@ def wait_until(predicate, *, attempts=float('inf'), timeout=float('inf'), lock=N
218219
time.sleep(0.05)
219220

220221
# Print the cause of the timeout
221-
assert_greater_than(attempts, attempt)
222-
assert_greater_than(timeout, time.time())
222+
predicate_source = inspect.getsourcelines(predicate)
223+
logger.error("wait_until() failed. Predicate: {}".format(predicate_source))
224+
if attempt >= attempts:
225+
raise AssertionError("Predicate {} not true after {} attempts".format(predicate_source, attempts))
226+
elif time.time() >= time_end:
227+
raise AssertionError("Predicate {} not true after {} seconds".format(predicate_source, timeout))
223228
raise RuntimeError('Unreachable')
224229

225230
# RPC/P2P connection constants and functions

0 commit comments

Comments
 (0)