Skip to content

Commit b860915

Browse files
author
MarcoFalke
committed
Merge #9707: Fix RPC failure testing
9db8eec Fix RPC failure testing (John Newbery)
2 parents 2447c10 + 9db8eec commit b860915

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

qa/rpc-tests/rpcnamedargs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def run_test(self):
3737
h = node.help(command='getinfo')
3838
assert(h.startswith('getinfo\n'))
3939

40-
assert_raises_jsonrpc(-8, node.help, random='getinfo')
40+
assert_raises_jsonrpc(-8, 'Unknown named parameter', node.help, random='getinfo')
4141

4242
h = node.getblockhash(height=0)
4343
node.getblock(blockhash=h)

qa/rpc-tests/test_framework/util.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -550,13 +550,30 @@ def assert_raises_message(exc, message, fun, *args, **kwds):
550550
else:
551551
raise AssertionError("No exception raised")
552552

553-
def assert_raises_jsonrpc(code, fun, *args, **kwds):
554-
'''Check for specific JSONRPC exception code'''
553+
def assert_raises_jsonrpc(code, message, fun, *args, **kwds):
554+
"""Run an RPC and verify that a specific JSONRPC exception code and message is raised.
555+
556+
Calls function `fun` with arguments `args` and `kwds`. Catches a JSONRPCException
557+
and verifies that the error code and message are as expected. Throws AssertionError if
558+
no JSONRPCException was returned or if the error code/message are not as expected.
559+
560+
Args:
561+
code (int), optional: the error code returned by the RPC call (defined
562+
in src/rpc/protocol.h). Set to None if checking the error code is not required.
563+
message (string), optional: [a substring of] the error string returned by the
564+
RPC call. Set to None if checking the error string is not required
565+
fun (function): the function to call. This should be the name of an RPC.
566+
args*: positional arguments for the function.
567+
kwds**: named arguments for the function.
568+
"""
555569
try:
556570
fun(*args, **kwds)
557571
except JSONRPCException as e:
558-
if e.error["code"] != code:
572+
# JSONRPCException was thrown as expected. Check the code and message values are correct.
573+
if (code is not None) and (code != e.error["code"]):
559574
raise AssertionError("Unexpected JSONRPC error code %i" % e.error["code"])
575+
if (message is not None) and (message not in e.error['message']):
576+
raise AssertionError("Expected substring not found:"+e.error['message'])
560577
except Exception as e:
561578
raise AssertionError("Unexpected exception raised: "+type(e).__name__)
562579
else:

0 commit comments

Comments
 (0)