-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_ujrpc.py
200 lines (173 loc) · 5.98 KB
/
test_ujrpc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
from ujrpc import JRPCService, JRPCException
import json, sys
try:
from typing import Dict, Union
except:
pass
try:
import uasyncio as asyncio
except:
import asyncio
jrpc = JRPCService(api_version=1)
jrpc.debug = False
jrpc.ret_str = False
def req(parameters, params=None, id=0, jstr=False) -> Union[str,Dict]:
req = {"jsonrpc": "2.0",
"method": parameters,
"id": id+1}
if params:
req["params"] = params
return json.dumps(req) if jstr else req
# test 1st param "r" is jrpc
@jrpc.fn()
def test_self(self):
return self
# test paramters
@jrpc.fn()
def test_args(r, a, b):
return a+b
@jrpc.fn()
def test_kwargs(r, a=0, b=0):
return a+b
@jrpc.fn()
def test_no_params(r):
return 100
tests = {}
tests["test_self"] = [
# fnc_name, params, checks[(key, value),...]
["test_self", None, [("result", jrpc)]]
]
_100 = [("result", 100)]
tests["params test"] = [
["test_args", [1,2], [("result", 3)]],
["test_kwargs", {"a":1,"b":2}, [("result", 3)]],
["test_no_params", None, _100]
]
_e1 = [("error.message", "Invalid params")]
_3 = [("result", 3)]
tests["invalid params test"] = [
["test_no_params",[1], _e1], #" excessive params"
["test_args",[1], _e1], # less params
["test_args",[1,2,3], _e1], # more params
["test_args",{"a":1, "b":2}, _3], # mismatch params
["test_args",{"b":2, "a":1}, _3], # mismatch params
["test_args",{"b":1}, _e1], # mismatch params
]
_0 = [("result", 0)]
tests["empty params test"] = [
["test_kwargs",[], _0],
["test_kwargs",{}, _0],
["test_kwargs",None, _0],
["test_no_params",None, _100],
["test_args",[], _e1],
]
tests["non-structured params test"] = [
["test_args","123", _e1],
["test_kwargs","123", _e1],
["test_args",123, _e1],
["test_kwargs",123, _e1],
]
tests["non-structured params test"] = [
["test_args","123", _e1],
["test_kwargs","123", _e1],
["test_args",123, _e1],
["test_kwargs",123, _e1],
]
_e2 = [("error.message",'Method not found')]
# should check method existence before validating paramter
tests["methord test"] = [
["test_not_found_method",None, _e2],
["test_not_found_method",{"a":1,"d":22}, _e2],
]
def test_rpc_error(tests, test_async=False):
for test_group, tests in tests.items():
print(f"Testing {test_group}")
for test in tests:
method_name, parameters, checks = test
if parameters:
if test_async:
ret = asyncio.run(jrpc.handle_rpca(req(method_name,parameters)))
else:
ret = jrpc.handle_rpc(req(method_name,parameters))
else:
if test_async:
ret = asyncio.run(jrpc.handle_rpca(req(method_name)))
else:
ret = jrpc.handle_rpc(req(method_name))
for ck in checks: # check values in response
keys = ck[0].split(".")
v = ret
for k in keys:
v = v[k]
try:
assert(v == ck[1])
except AssertionError as ex:
print(f" Method: {method_name}, Param:{parameters}")
print(f" Expecting {ck[0]} == {ck[1]}, but got {v}")
def test_parser_error(test_async=False):
# test json error
for s in ["dsfsdfds}ew223}", "{123}}", "self"]:
q = req("test_args", 123, jstr=True).replace("123",s)
if test_async:
ret:dict = asyncio.run(jrpc.handle_rpca(q))
else:
ret:dict = jrpc.handle_rpc(q)
assert(ret["error"]["message"] == "Parse error")
#assert(ret1["error"]["message"] == 'Invalid params')
#[{},] will not trigger parse error in ujson
ret = jrpc.handle_rpc('[{"jsonrpc": "2.0", "method": "test_args", "params": [1,2], "id": 1},]')
if "MicroPython" in sys.version:
assert("error" not in ret)
else:
assert(ret["error"]["message"] == "Parse error")
#test for batched RPC"
req1 = """[
{"jsonrpc": "2.0", "method": "test_args", "params": {}, "id": 1},
{"jsonrpc": "2.0", "method": "test_kwargs", "params": ["Hello"], "id": 2},
{"jsonrpc": "2.0", "method": "test_no_params", "params": ["Hello"], "id": 3},
{"jsonrpc": "2.0", "method": "test_args", "params": [11,22], "id": 4},
{"jsonrpc": "2.0", "method": "test_kwargs", "params": {"a":1,"b":2}, "id": 5},
{"jsonrpc": "2.0", "method": "test_no_params", "params": {}, "id": 6}
]"""
def test_batched_request(test_async=False):
if test_async:
ret:dict = asyncio.run(jrpc.handle_rpca(req1))
else:
ret:dict = jrpc.handle_rpc(req1)
assert(type(ret) == list and len(ret) == 6)
for i in range(0,3):
assert(ret[i]["error"]["message"] == 'Invalid params')
for i, v in enumerate([33,3,100]):
assert(ret[i+3]["result"] == v)
for test_async in [False, True]:
print(f"{'=== handle_rpca() ===' if test_async else '=== handle_rpc() ==='}")
test_rpc_error(tests, test_async=test_async)
test_parser_error(test_async=test_async)
test_batched_request(test_async=test_async)
print("=== async pattern ===")
@jrpc.fn("test_async_exeorder")
async def test_async_exeorder(r, pace, id):
for i in range(0,10):
await asyncio.sleep(pace)
if "Micro" in sys.version:
print(id, end="")
else:
print(id, end="", flush=True)
async def handle_async(req, r):
ret = await jrpc.handle_rpca(json.dumps(req))
r += [ret]
async def main():
reqs = [
{"jsonrpc": "2.0", "method": "test_async_exeorder", "params": [0.2, 1], "id": 1},
{"jsonrpc": "2.0", "method": "test_async_exeorder", "params": [0.4, 2], "id": 2},
{"jsonrpc": "2.0", "method": "test_async_exeorder", "params": [0.6, 3], "id": 3}
]
rsps = []
print("test pattern1: ", end="")
tasks = map(lambda req: handle_async(req, rsps), reqs)
await asyncio.gather(*tasks)
for i in range(0,3):
# print(rsps[i])
assert(rsps[i]["id"] == i+1)
print(" done")
asyncio.run(main())