Skip to content

Commit 92fe537

Browse files
committed
test: fix intermittent race condition in interface_bitcoin_cli.py
by calling wait_for_cookie_credentials() to ensure the cookie file is written and auth credentials available for testing the CLI -rpcwait option before the RPC connection is up.
1 parent c648e63 commit 92fe537

File tree

1 file changed

+12
-16
lines changed

1 file changed

+12
-16
lines changed

test/functional/interface_bitcoin_cli.py

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
BALANCE = (BLOCKS - 100) * 50
1414

1515
class TestBitcoinCli(BitcoinTestFramework):
16-
1716
def set_test_params(self):
1817
self.setup_clean_chain = True
1918
self.num_nodes = 1
@@ -33,12 +32,12 @@ def run_test(self):
3332
user, password = get_auth_cookie(self.nodes[0].datadir, self.chain)
3433

3534
self.log.info("Test -stdinrpcpass option")
36-
assert_equal(BLOCKS, self.nodes[0].cli('-rpcuser=%s' % user, '-stdinrpcpass', input=password).getblockcount())
37-
assert_raises_process_error(1, "Incorrect rpcuser or rpcpassword", self.nodes[0].cli('-rpcuser=%s' % user, '-stdinrpcpass', input="foo").echo)
35+
assert_equal(BLOCKS, self.nodes[0].cli('-rpcuser={}'.format(user), '-stdinrpcpass', input=password).getblockcount())
36+
assert_raises_process_error(1, 'Incorrect rpcuser or rpcpassword', self.nodes[0].cli('-rpcuser={}'.format(user), '-stdinrpcpass', input='foo').echo)
3837

3938
self.log.info("Test -stdin and -stdinrpcpass")
40-
assert_equal(["foo", "bar"], self.nodes[0].cli('-rpcuser=%s' % user, '-stdin', '-stdinrpcpass', input=password + "\nfoo\nbar").echo())
41-
assert_raises_process_error(1, "Incorrect rpcuser or rpcpassword", self.nodes[0].cli('-rpcuser=%s' % user, '-stdin', '-stdinrpcpass', input="foo").echo)
39+
assert_equal(['foo', 'bar'], self.nodes[0].cli('-rpcuser={}'.format(user), '-stdin', '-stdinrpcpass', input=password + '\nfoo\nbar').echo())
40+
assert_raises_process_error(1, 'Incorrect rpcuser or rpcpassword', self.nodes[0].cli('-rpcuser={}'.format(user), '-stdin', '-stdinrpcpass', input='foo').echo)
4241

4342
self.log.info("Test connecting to a non-existing server")
4443
assert_raises_process_error(1, "Could not connect to the server", self.nodes[0].cli('-rpcport=1').echo)
@@ -52,7 +51,7 @@ def run_test(self):
5251
self.log.info("Test -getinfo returns expected network and blockchain info")
5352
if self.is_wallet_compiled():
5453
self.nodes[0].encryptwallet(password)
55-
cli_get_info = self.nodes[0].cli('-getinfo').send_cli()
54+
cli_get_info = self.nodes[0].cli().send_cli('-getinfo')
5655
network_info = self.nodes[0].getnetworkinfo()
5756
blockchain_info = self.nodes[0].getblockchaininfo()
5857
assert_equal(cli_get_info['version'], network_info['version'])
@@ -76,20 +75,17 @@ def run_test(self):
7675
else:
7776
self.log.info("*** Wallet not compiled; cli getwalletinfo and -getinfo wallet tests skipped")
7877

79-
self.stop_node(0)
80-
8178
self.log.info("Test -version with node stopped")
82-
cli_response = self.nodes[0].cli("-version").send_cli()
79+
self.stop_node(0)
80+
cli_response = self.nodes[0].cli().send_cli('-version')
8381
assert "{} RPC client version".format(self.config['environment']['PACKAGE_NAME']) in cli_response
8482

85-
self.log.info("Test -rpcwait option waits for RPC connection instead of failing")
86-
# Start node without RPC connection.
87-
self.nodes[0].start()
88-
# Verify failure without -rpcwait.
89-
assert_raises_process_error(1, "Could not connect to the server", self.nodes[0].cli('getblockcount').echo)
90-
# Verify success using -rpcwait.
91-
assert_equal(BLOCKS, self.nodes[0].cli('-rpcwait', 'getblockcount').send_cli())
83+
self.log.info("Test -rpcwait option successfully waits for RPC connection")
84+
self.nodes[0].start() # start node without RPC connection
85+
self.nodes[0].wait_for_cookie_credentials() # ensure cookie file is available to avoid race condition
86+
blocks = self.nodes[0].cli('-rpcwait').send_cli('getblockcount')
9287
self.nodes[0].wait_for_rpc_connection()
88+
assert_equal(blocks, BLOCKS)
9389

9490

9591
if __name__ == '__main__':

0 commit comments

Comments
 (0)