aboutsummaryrefslogtreecommitdiff
path: root/test/functional/test_framework/util.py
diff options
context:
space:
mode:
Diffstat (limited to 'test/functional/test_framework/util.py')
-rw-r--r--test/functional/test_framework/util.py49
1 files changed, 36 insertions, 13 deletions
diff --git a/test/functional/test_framework/util.py b/test/functional/test_framework/util.py
index ce68de7eaa..14930ef671 100644
--- a/test/functional/test_framework/util.py
+++ b/test/functional/test_framework/util.py
@@ -268,7 +268,28 @@ def satoshi_round(amount: Union[int, float, str], *, rounding: str) -> Decimal:
return Decimal(amount).quantize(SATOSHI_PRECISION, rounding=rounding)
-def wait_until_helper_internal(predicate, *, attempts=float('inf'), timeout=float('inf'), lock=None, timeout_factor=1.0):
+def ensure_for(*, duration, f, check_interval=0.2):
+ """Check if the predicate keeps returning True for duration.
+
+ check_interval can be used to configure the wait time between checks.
+ Setting check_interval to 0 will allow to have two checks: one in the
+ beginning and one after duration.
+ """
+ # If check_interval is 0 or negative or larger than duration, we fall back
+ # to checking once in the beginning and once at the end of duration
+ if check_interval <= 0 or check_interval > duration:
+ check_interval = duration
+ time_end = time.time() + duration
+ predicate_source = "''''\n" + inspect.getsource(f) + "'''"
+ while True:
+ if not f():
+ raise AssertionError(f"Predicate {predicate_source} became false within {duration} seconds")
+ if time.time() > time_end:
+ return
+ time.sleep(check_interval)
+
+
+def wait_until_helper_internal(predicate, *, timeout=60, lock=None, timeout_factor=1.0, check_interval=0.05):
"""Sleep until the predicate resolves to be True.
Warning: Note that this method is not recommended to be used in tests as it is
@@ -277,13 +298,10 @@ def wait_until_helper_internal(predicate, *, attempts=float('inf'), timeout=floa
properly scaled. Furthermore, `wait_until()` from `P2PInterface` class in
`p2p.py` has a preset lock.
"""
- if attempts == float('inf') and timeout == float('inf'):
- timeout = 60
timeout = timeout * timeout_factor
- attempt = 0
time_end = time.time() + timeout
- while attempt < attempts and time.time() < time_end:
+ while time.time() < time_end:
if lock:
with lock:
if predicate():
@@ -291,17 +309,12 @@ def wait_until_helper_internal(predicate, *, attempts=float('inf'), timeout=floa
else:
if predicate():
return
- attempt += 1
- time.sleep(0.05)
+ time.sleep(check_interval)
# Print the cause of the timeout
predicate_source = "''''\n" + inspect.getsource(predicate) + "'''"
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 AssertionError("Predicate {} not true after {} seconds".format(predicate_source, timeout))
def sha256sum_file(filename):
@@ -433,7 +446,6 @@ def write_config(config_path, *, n, chain, extra_config="", disable_autoconnect=
# in tests.
f.write("peertimeout=999999999\n")
f.write("printtoconsole=0\n")
- f.write("upnp=0\n")
f.write("natpmp=0\n")
f.write("shrinkdebugfile=0\n")
f.write("deprecatedrpc=create_bdb\n") # Required to run the tests
@@ -441,6 +453,17 @@ def write_config(config_path, *, n, chain, extra_config="", disable_autoconnect=
f.write("unsafesqlitesync=1\n")
if disable_autoconnect:
f.write("connect=0\n")
+ # Limit max connections to mitigate test failures on some systems caused by the warning:
+ # "Warning: Reducing -maxconnections from <...> to <...> due to system limitations".
+ # The value is calculated as follows:
+ # available_fds = 256 // Same as FD_SETSIZE on NetBSD.
+ # MIN_CORE_FDS = 151 // Number of file descriptors required for core functionality.
+ # MAX_ADDNODE_CONNECTIONS = 8 // Maximum number of -addnode outgoing nodes.
+ # nBind == 3 // Maximum number of bound interfaces used in a test.
+ #
+ # min_required_fds = MIN_CORE_FDS + MAX_ADDNODE_CONNECTIONS + nBind = 151 + 8 + 3 = 162;
+ # nMaxConnections = available_fds - min_required_fds = 256 - 161 = 94;
+ f.write("maxconnections=94\n")
f.write(extra_config)