1
0

cli: "./run_electrum daemon -d" to block until daemon becomes ready

Without this,
`$ python3 -m unittest electrum.tests.regtest.TestUnixSockets.test_unixsockets`
was failing on my machine but succeeding on CI, due to timing differences.
This commit is contained in:
SomberNight
2023-08-03 17:02:40 +00:00
parent 58a9904a34
commit 8b195ee77a
2 changed files with 21 additions and 2 deletions

View File

@@ -151,6 +151,19 @@ def request(config: SimpleConfig, endpoint, args=(), timeout: Union[float, int]
time.sleep(1.0)
def wait_until_daemon_becomes_ready(*, config: SimpleConfig, timeout=5) -> bool:
t0 = time.monotonic()
while True:
if time.monotonic() > t0 + timeout:
return False # timeout
try:
request(config, 'ping')
return True # success
except DaemonNotRunning:
time.sleep(0.05)
continue
def get_rpc_credentials(config: SimpleConfig) -> Tuple[str, str]:
rpc_user = config.RPC_USERNAME or None
rpc_password = config.RPC_PASSWORD or None

View File

@@ -284,6 +284,7 @@ def sys_exit(i):
def main():
global loop, stop_loop, loop_thread
# The hook will only be used in the Qt GUI right now
util.setup_thread_excepthook()
# on macOS, delete Process Serial Number arg generated for apps launched in Finder
@@ -416,7 +417,13 @@ def main():
sys.exit(1)
if pid:
print_stderr("starting daemon (PID %d)" % pid)
sys.exit(0)
loop, stop_loop, loop_thread = create_and_start_event_loop()
ready = daemon.wait_until_daemon_becomes_ready(config=config, timeout=5)
if ready:
sys_exit(0)
else:
print_stderr("timed out waiting for daemon to get ready")
sys_exit(1)
else:
# redirect standard file descriptors
sys.stdout.flush()
@@ -428,7 +435,6 @@ def main():
os.dup2(so.fileno(), sys.stdout.fileno())
os.dup2(se.fileno(), sys.stderr.fileno())
global loop, stop_loop, loop_thread
loop, stop_loop, loop_thread = create_and_start_event_loop()
try: