|
1
|
|
|
import aiohttp_rpc |
|
2
|
|
|
from tests import utils |
|
3
|
|
|
|
|
4
|
|
|
|
|
5
|
|
|
async def test_batch(aiohttp_client): |
|
6
|
|
|
def method_1(a=1): |
|
7
|
|
|
return [1, 2, a] |
|
8
|
|
|
|
|
9
|
|
|
def method_2(): |
|
10
|
|
|
return [1] |
|
11
|
|
|
|
|
12
|
|
|
rpc_server = aiohttp_rpc.JsonRpcServer() |
|
13
|
|
|
rpc_server.add_methods(( |
|
14
|
|
|
method_1, |
|
15
|
|
|
method_2, |
|
16
|
|
|
)) |
|
17
|
|
|
|
|
18
|
|
|
assert await rpc_server.call('method_1') == [1, 2, 1] |
|
19
|
|
|
assert await rpc_server.call('method_2') == [1] |
|
20
|
|
|
|
|
21
|
|
|
client = await utils.make_client(aiohttp_client, rpc_server) |
|
22
|
|
|
|
|
23
|
|
|
async with aiohttp_rpc.JsonRpcClient('/rpc', session=client) as rpc: |
|
24
|
|
|
assert await rpc.batch(('method_1', 'method_2',)) == ([1, 2, 1], [1],) |
|
25
|
|
|
assert await rpc.batch((('method_1', 4), ('method_1', [], {'a': 5}),)) == ([1, 2, 4], [1, 2, 5],) |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
async def test_unlinked_results(aiohttp_client, mocker): |
|
29
|
|
|
def method_1(a=1): |
|
30
|
|
|
return [1, 2, a] |
|
31
|
|
|
|
|
32
|
|
|
def method_2(): |
|
33
|
|
|
return [1] |
|
34
|
|
|
|
|
35
|
|
|
rpc_server = aiohttp_rpc.JsonRpcServer() |
|
36
|
|
|
rpc_server.add_methods(( |
|
37
|
|
|
method_1, |
|
38
|
|
|
method_2, |
|
39
|
|
|
)) |
|
40
|
|
|
|
|
41
|
|
|
client = await utils.make_client(aiohttp_client, rpc_server) |
|
42
|
|
|
|
|
43
|
|
|
async def test_send_json_1(data, **kwargs): |
|
44
|
|
|
data = [ |
|
45
|
|
|
{'id': None, 'jsonrpc': '2.0', 'result': [1]}, |
|
46
|
|
|
{'id': data[0]['id'], 'jsonrpc': '2.0', 'result': [1, 2, 1]}, |
|
47
|
|
|
] |
|
48
|
|
|
return data, {} |
|
49
|
|
|
|
|
50
|
|
|
async def test_send_json_2(data, **kwargs): |
|
51
|
|
|
data = [ |
|
52
|
|
|
{'id': None, 'jsonrpc': '2.0', 'result': [1]}, |
|
53
|
|
|
{'id': data[0]['id'], 'jsonrpc': '2.0', 'result': [1, 2, 1]}, |
|
54
|
|
|
{'id': None, 'jsonrpc': '2.0', 'result': [1]}, |
|
55
|
|
|
] |
|
56
|
|
|
return data, {} |
|
57
|
|
|
|
|
58
|
|
|
async with aiohttp_rpc.JsonRpcClient('/rpc', session=client) as rpc: |
|
59
|
|
|
mocker.patch.object(rpc, 'send_json', new_callable=lambda: test_send_json_1) |
|
60
|
|
|
unlinked_results = aiohttp_rpc.JsonRpcUnlinkedResults(results=[[1]]) |
|
61
|
|
|
assert await rpc.batch(('method_1', 'method_2',)) == ([1, 2, 1], unlinked_results,) |
|
62
|
|
|
|
|
63
|
|
|
mocker.patch.object(rpc, 'send_json', new_callable=lambda: test_send_json_2) |
|
64
|
|
|
unlinked_results = aiohttp_rpc.JsonRpcUnlinkedResults(results=[[1], [1]]) |
|
65
|
|
|
assert await rpc.batch(('method_1', 'method_2', 'method_2',)) == ( |
|
66
|
|
|
[1, 2, 1], |
|
67
|
|
|
unlinked_results, |
|
68
|
|
|
unlinked_results, |
|
69
|
|
|
) |
|
70
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
async def test_duplicated_results(aiohttp_client, mocker): |
|
73
|
|
|
def method_1(a=1): |
|
74
|
|
|
return [1, 2, a] |
|
75
|
|
|
|
|
76
|
|
|
def method_2(): |
|
77
|
|
|
return [1] |
|
78
|
|
|
|
|
79
|
|
|
rpc_server = aiohttp_rpc.JsonRpcServer() |
|
80
|
|
|
rpc_server.add_methods(( |
|
81
|
|
|
method_1, |
|
82
|
|
|
method_2, |
|
83
|
|
|
)) |
|
84
|
|
|
|
|
85
|
|
|
client = await utils.make_client(aiohttp_client, rpc_server) |
|
86
|
|
|
|
|
87
|
|
|
async def test_send_json_1(data, **kwargs): |
|
88
|
|
|
data = [ |
|
89
|
|
|
{'id': None, 'jsonrpc': '2.0', 'result': [1]}, |
|
90
|
|
|
{'id': data[0]['id'], 'jsonrpc': '2.0', 'result': [1, 2, 1]}, |
|
91
|
|
|
] |
|
92
|
|
|
return data, {} |
|
93
|
|
|
|
|
94
|
|
|
async def test_send_json_2(data, **kwargs): |
|
95
|
|
|
data = [ |
|
96
|
|
|
{'id': None, 'jsonrpc': '2.0', 'result': [1]}, |
|
97
|
|
|
{'id': data[0]['id'], 'jsonrpc': '2.0', 'result': [1, 2, 1]}, |
|
98
|
|
|
{'id': data[0]['id'], 'jsonrpc': '2.0', 'result': [1, 2, 3]}, |
|
99
|
|
|
{'id': None, 'jsonrpc': '2.0', 'result': [1]}, |
|
100
|
|
|
] |
|
101
|
|
|
return data, {} |
|
102
|
|
|
|
|
103
|
|
|
async with aiohttp_rpc.JsonRpcClient('/rpc', session=client) as rpc: |
|
104
|
|
|
mocker.patch.object(rpc, 'send_json', new_callable=lambda: test_send_json_1) |
|
105
|
|
|
unlinked_results = aiohttp_rpc.JsonRpcUnlinkedResults(results=[[1]]) |
|
106
|
|
|
assert await rpc.batch(('method_1', 'method_2',)) == ([1, 2, 1], unlinked_results,) |
|
107
|
|
|
|
|
108
|
|
|
mocker.patch.object(rpc, 'send_json', new_callable=lambda: test_send_json_2) |
|
109
|
|
|
unlinked_results = aiohttp_rpc.JsonRpcUnlinkedResults(results=[[1], [1]]) |
|
110
|
|
|
duplicated_results = aiohttp_rpc.JsonRpcDuplicatedResults(results=[[1, 2, 1], [1, 2, 3]]) |
|
111
|
|
|
assert await rpc.batch(('method_1', 'method_2', 'method_2',)) == ( |
|
112
|
|
|
duplicated_results, |
|
113
|
|
|
unlinked_results, |
|
114
|
|
|
unlinked_results, |
|
115
|
|
|
) |
|
116
|
|
|
|