Changeset View
Changeset View
Standalone View
Standalone View
test/functional/test_framework/util.py
#!/usr/bin/env python3 | #!/usr/bin/env python3 | ||||
# Copyright (c) 2014-2016 The Bitcoin Core developers | # Copyright (c) 2014-2016 The Bitcoin Core developers | ||||
# Distributed under the MIT software license, see the accompanying | # Distributed under the MIT software license, see the accompanying | ||||
# file COPYING or http://www.opensource.org/licenses/mit-license.php. | # file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
"""Helpful routines for regression testing.""" | """Helpful routines for regression testing.""" | ||||
from base64 import b64encode | 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 | ||||
import random | import random | ||||
import re | import re | ||||
from subprocess import CalledProcessError | from subprocess import CalledProcessError | ||||
import time | import time | ||||
▲ Show 20 Lines • Show All 218 Lines • ▼ Show 20 Lines | |||||
def satoshi_round(amount): | def satoshi_round(amount): | ||||
return Decimal(amount).quantize(Decimal('0.00000001'), rounding=ROUND_DOWN) | return Decimal(amount).quantize(Decimal('0.00000001'), rounding=ROUND_DOWN) | ||||
def wait_until(predicate, *, attempts=float('inf'), timeout=float('inf'), lock=None): | def wait_until(predicate, *, attempts=float('inf'), timeout=float('inf'), lock=None): | ||||
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(): | ||||
return | return | ||||
else: | else: | ||||
if predicate(): | if predicate(): | ||||
return | return | ||||
attempt += 1 | attempt += 1 | ||||
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 | ||||
############################################ | ############################################ | ||||
# The maximum number of nodes a single test can spawn | # The maximum number of nodes a single test can spawn | ||||
MAX_NODES = 8 | MAX_NODES = 8 | ||||
▲ Show 20 Lines • Show All 353 Lines • Show Last 20 Lines |