test: test availability of ports before assigning them
Summary:
Previously the functional tests would use a range of ports for each test process's RPC connections and peer connections, and assume these ports are available. If a port happened to be already used, the test would fail because the nodes could not bind on the address:
OSError: [Errno 98] error while attempting to bind on address ('127.0.0.1', 21325): address already in use
The port that occasionally caused issues is 21325, which is used by trezord. We could try to find another range of consecutive available ports, but after a few attempts this turns out to not be easy. With functional tests running in parallel, and with the max number of peers for each individual test having recently increased, a wider range of ports is now necessary. Better to actually check if a port is available.
With this change, each port's availability is checked before using it, and if the tried port is unavailable it is simply incremented until one is found. A cache is used to ensure the same port is returned when the functions are called multiple times for the same node/peer number.
Test Plan: ninja check-functional
Reviewers: #bitcoin_abc, Fabien
Reviewed By: #bitcoin_abc, Fabien
Subscribers: Fabien
Differential Revision: https://reviews.bitcoinabc.org/D11796