Skip to content

Commit c648e63

Browse files
committed
test: add wait_for_cookie_credentials() to test framework
to be able to ensure the cookie file is written and auth credentials available when testing CLI/RPC commands before the RPC connection is up.
1 parent f8102d9 commit c648e63

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

test/functional/test_framework/test_node.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
MAX_NODES,
2727
append_config,
2828
delete_cookie_file,
29+
get_auth_cookie,
2930
get_rpc_proxy,
3031
rpc_url,
3132
wait_until,
@@ -237,12 +238,27 @@ def wait_for_rpc_connection(self):
237238
except OSError as e:
238239
if e.errno != errno.ECONNREFUSED: # Port not yet open?
239240
raise # unknown OS error
240-
except ValueError as e: # cookie file not found and no rpcuser or rpcassword. bitcoind still starting
241+
except ValueError as e: # cookie file not found and no rpcuser or rpcpassword; bitcoind is still starting
241242
if "No RPC credentials" not in str(e):
242243
raise
243244
time.sleep(1.0 / poll_per_s)
244245
self._raise_assertion_error("Unable to connect to bitcoind after {}s".format(self.rpc_timeout))
245246

247+
def wait_for_cookie_credentials(self):
248+
"""Ensures auth cookie credentials can be read, e.g. for testing CLI with -rpcwait before RPC connection is up."""
249+
self.log.debug("Waiting for cookie credentials")
250+
# Poll at a rate of four times per second.
251+
poll_per_s = 4
252+
for _ in range(poll_per_s * self.rpc_timeout):
253+
try:
254+
get_auth_cookie(self.datadir, self.chain)
255+
self.log.debug("Cookie credentials successfully retrieved")
256+
return
257+
except ValueError: # cookie file not found and no rpcuser or rpcpassword; bitcoind is still starting
258+
pass # so we continue polling until RPC credentials are retrieved
259+
time.sleep(1.0 / poll_per_s)
260+
self._raise_assertion_error("Unable to retrieve cookie credentials after {}s".format(self.rpc_timeout))
261+
246262
def generate(self, nblocks, maxtries=1000000):
247263
self.log.debug("TestNode.generate() dispatches `generate` call to `generatetoaddress`")
248264
return self.generatetoaddress(nblocks=nblocks, address=self.get_deterministic_priv_key().address, maxtries=maxtries)

0 commit comments

Comments
 (0)