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
|
|
|
assert await rpc.batch(('method_1', 'method_2',)) == [[1, 2, 1], [1]] |
61
|
|
|
|
62
|
|
|
mocker.patch.object(rpc, 'send_json', new_callable=lambda: test_send_json_2) |
63
|
|
|
unlinked_results = aiohttp_rpc.UnlinkedResults(data=[[1], [1]]) |
64
|
|
|
assert await rpc.batch(('method_1', 'method_2', 'method_2',)) == [[1, 2, 1], unlinked_results, unlinked_results] |
65
|
|
|
|