tests: Improve functional_tests_rpc startup

* Use a global startup timeout, and apply it to each socket's connect
  call, rather than repeating connection attempts with short timeouts.

* Avoid leaking sockets by closing them.
This commit is contained in:
iamamyth 2025-01-31 14:53:37 -08:00
parent 90359e31fd
commit 274b5748da

View File

@ -7,6 +7,7 @@ from signal import SIGTERM
import socket import socket
import string import string
import os import os
import time
USAGE = 'usage: functional_tests_rpc.py <python> <srcdir> <builddir> [<tests-to-run> | all]' USAGE = 'usage: functional_tests_rpc.py <python> <srcdir> <builddir> [<tests-to-run> | all]'
DEFAULT_TESTS = [ DEFAULT_TESTS = [
@ -120,23 +121,26 @@ def kill():
except: pass except: pass
# wait for error/startup # wait for error/startup
for i in range(10): startup_timeout = 10
time.sleep(1) deadline = time.monotonic() + startup_timeout
all_open = True for port in ports:
for port in ports: addr = ('127.0.0.1', port)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) delay = 0
s.settimeout(1) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
if s.connect_ex(('127.0.0.1', port)) != 0: try:
all_open = False while True:
break timeout = deadline - time.monotonic() - delay
if timeout <= 0:
print('Failed to start wallet or daemon')
kill()
sys.exit(1)
time.sleep(delay)
s.settimeout(timeout)
if s.connect_ex(addr) == 0:
break
delay = .1
finally:
s.close() s.close()
if all_open:
break
if not all_open:
print('Failed to start wallet or daemon')
kill()
sys.exit(1)
# online daemons need some time to connect to peers to be ready # online daemons need some time to connect to peers to be ready
time.sleep(2) time.sleep(2)