test: wait_for_peers counts keys of getpeerinfo, not the actual peers
Description
In tests/functional/test_framework/rpc_client.py:
def get_peer_info(self) -> dict:
return self._call("getpeerinfo")
def wait_for_peers(self, minimum: int, timeout: float = 30.0) -> None:
"""Poll get_peer_info until peer count >= minimum."""
wait_until(
lambda: len(self.get_peer_info()) >= minimum,
...
)
getpeerinfo returns one object, not a list of peers
{"avg_latency_ms": 0.0, "connected": 0, "inbound": 0, "network_groups": 0,
"outbound": 0, "peers": [], "total_peers": 0}
len() of the dict is 7, whatever the number of peers. So
wait_for_peers(minimum) returns immediately for any minimum up to 7, even with no peers
connected, and a test that relies on it proves nothing about connectivity. It is documented in tests/functional/docs/FRAMEWORK_GUIDE.md (wait_for_peers(1, ...)) but
no test uses it yet. It will mislead the first test that does.
How to Reproduce
# with a node that has no peers
rpc.get_peer_info() # -> the object above
len(rpc.get_peer_info()) # -> 7
rpc.wait_for_peers(1) # returns immediately instead of timing out
Expected Behavior
wait_for_peers(n) waits until the node reports at least n connected peers, and times out
otherwise.
Hi @bolajahmad,
I've gone through the issue and inspected tests/functional/test_framework/rpc_client.py (lines 318–324).
Currently, len(self.get_peer_info()) evaluates the dictionary length (the 7 top-level response keys) rather than the actual peer collection. Because 7 >= minimum holds for any minimum <= 7, wait_for_peers() returns immediately even when connected: 0 and peers: [].
I can resolve this by:
- Updating
wait_for_peers()to check the actual peers:lambda: len(self.get_peer_info().get("peers", [])) >= minimum(or checking the"connected"/"total_peers"count field). - Adding a unit test in
feature_framework_unit_tests.pywith mock responses to verify thatwait_for_peers()correctly blocks/times out when peers are below the threshold and passes when reached.
Could you please assign this issue to me? I'd be happy to open a PR for it.
This issue is already resolved by #600. I'll be closing it now