Skip to content

Commit

Permalink
test: add wait_for_cookie_credentials() to test framework
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jonatack committed Apr 19, 2020
1 parent f8102d9 commit c648e63
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion test/functional/test_framework/test_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
MAX_NODES,
append_config,
delete_cookie_file,
get_auth_cookie,
get_rpc_proxy,
rpc_url,
wait_until,
Expand Down Expand Up @@ -237,12 +238,27 @@ def wait_for_rpc_connection(self):
except OSError as e:
if e.errno != errno.ECONNREFUSED: # Port not yet open?
raise # unknown OS error
except ValueError as e: # cookie file not found and no rpcuser or rpcassword. bitcoind still starting
except ValueError as e: # cookie file not found and no rpcuser or rpcpassword; bitcoind is still starting
if "No RPC credentials" not in str(e):
raise
time.sleep(1.0 / poll_per_s)
self._raise_assertion_error("Unable to connect to bitcoind after {}s".format(self.rpc_timeout))

def wait_for_cookie_credentials(self):
"""Ensures auth cookie credentials can be read, e.g. for testing CLI with -rpcwait before RPC connection is up."""
self.log.debug("Waiting for cookie credentials")
# Poll at a rate of four times per second.
poll_per_s = 4
for _ in range(poll_per_s * self.rpc_timeout):
try:
get_auth_cookie(self.datadir, self.chain)
self.log.debug("Cookie credentials successfully retrieved")
return
except ValueError: # cookie file not found and no rpcuser or rpcpassword; bitcoind is still starting
pass # so we continue polling until RPC credentials are retrieved
time.sleep(1.0 / poll_per_s)
self._raise_assertion_error("Unable to retrieve cookie credentials after {}s".format(self.rpc_timeout))

def generate(self, nblocks, maxtries=1000000):
self.log.debug("TestNode.generate() dispatches `generate` call to `generatetoaddress`")
return self.generatetoaddress(nblocks=nblocks, address=self.get_deterministic_priv_key().address, maxtries=maxtries)
Expand Down

0 comments on commit c648e63

Please sign in to comment.