1
|
|
|
import asyncio |
2
|
|
|
import datetime |
3
|
|
|
|
4
|
|
|
import aiohttp_rpc |
5
|
|
|
from tests import utils |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
async def test_args(aiohttp_client): |
9
|
|
|
def method(a=1): |
10
|
|
|
return [1, 2, a] |
11
|
|
|
|
12
|
|
|
rpc_server = aiohttp_rpc.WsJsonRpcServer() |
13
|
|
|
rpc_server.add_method(method) |
14
|
|
|
|
15
|
|
|
client = await utils.make_ws_client(aiohttp_client, rpc_server) |
16
|
|
|
|
17
|
|
|
async with aiohttp_rpc.WsJsonRpcClient('/rpc', session=client) as rpc: |
18
|
|
|
assert await rpc.call('method') == [1, 2, 1] |
19
|
|
|
assert await rpc.call('method', 1) == [1, 2, 1] |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
async def test_batch(aiohttp_client): |
23
|
|
|
def method_1(a=1): |
24
|
|
|
return [1, a] |
25
|
|
|
|
26
|
|
|
def method_2(): |
27
|
|
|
return 1 |
28
|
|
|
|
29
|
|
|
rpc_server = aiohttp_rpc.WsJsonRpcServer() |
30
|
|
|
rpc_server.add_methods((method_1, method_2,)) |
31
|
|
|
|
32
|
|
|
client = await utils.make_ws_client(aiohttp_client, rpc_server) |
33
|
|
|
|
34
|
|
|
async with aiohttp_rpc.WsJsonRpcClient('/rpc', session=client) as rpc: |
35
|
|
|
assert await rpc.batch(('method_1', 'method_2',)) == ([1, 1], 1,) |
36
|
|
|
assert await rpc.batch((('method_1', 4), ('method_1', [], {'a': 5},),)) == ([1, 4], [1, 5],) |
37
|
|
|
|
38
|
|
|
async with aiohttp_rpc.WsJsonRpcClient('/rpc', session=client) as rpc: |
39
|
|
|
assert await rpc.batch_notify(('method_1', 'method_2',)) is None |
40
|
|
|
assert await rpc.batch_notify((('method_1', 4), ('method_1', [], {'a': 5},),)) is None |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
async def test_several_requests(aiohttp_client): |
44
|
|
|
async def method(a): |
45
|
|
|
await asyncio.sleep(0.2) |
46
|
|
|
return a |
47
|
|
|
|
48
|
|
|
rpc_server = aiohttp_rpc.WsJsonRpcServer() |
49
|
|
|
rpc_server.add_method(method) |
50
|
|
|
|
51
|
|
|
client = await utils.make_ws_client(aiohttp_client, rpc_server) |
52
|
|
|
|
53
|
|
|
async with aiohttp_rpc.WsJsonRpcClient('/rpc', session=client) as rpc: |
54
|
|
|
started_at = datetime.datetime.now() |
55
|
|
|
|
56
|
|
|
result = await asyncio.gather(*( |
57
|
|
|
rpc.call('method', i) |
|
|
|
|
58
|
|
|
for i in range(10) |
59
|
|
|
)) |
60
|
|
|
|
61
|
|
|
finished_at = datetime.datetime.now() |
62
|
|
|
|
63
|
|
|
assert finished_at - started_at < datetime.timedelta(seconds=1) |
64
|
|
|
assert result == list(range(10)) |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
async def test_ws_client_for_server_response(aiohttp_client, mocker): |
68
|
|
|
async def method(rpc_ws_client: aiohttp_rpc.WsJsonRpcClient): |
69
|
|
|
await rpc_ws_client.notify('ping') |
70
|
|
|
await rpc_ws_client.notify('ping') |
71
|
|
|
await rpc_ws_client.notify('ping') |
72
|
|
|
|
73
|
|
|
rpc_server = aiohttp_rpc.WsJsonRpcServer( |
74
|
|
|
middlewares=[ |
75
|
|
|
*aiohttp_rpc.middlewares.DEFAULT_MIDDLEWARES, |
76
|
|
|
aiohttp_rpc.middlewares.ws_client_for_server_response, |
77
|
|
|
], |
78
|
|
|
) |
79
|
|
|
rpc_server.add_method(method) |
80
|
|
|
|
81
|
|
|
client = await utils.make_ws_client(aiohttp_client, rpc_server) |
82
|
|
|
|
83
|
|
|
future = asyncio.Future() |
84
|
|
|
|
85
|
|
|
results = [] |
86
|
|
|
|
87
|
|
|
def json_request_handler(*, ws_connect, ws_msg, json_request): |
88
|
|
|
results.append(json_request) |
89
|
|
|
|
90
|
|
|
if len(results) == 3: |
91
|
|
|
future.set_result(results) |
92
|
|
|
|
93
|
|
|
async with aiohttp_rpc.WsJsonRpcClient( |
94
|
|
|
'/rpc', |
95
|
|
|
session=client, |
96
|
|
|
json_request_handler=json_request_handler, |
97
|
|
|
) as rpc: |
98
|
|
|
json_request_handler = mocker.patch.object(rpc, '_json_request_handler', side_effect=rpc._json_request_handler) |
99
|
|
|
await rpc.method() |
100
|
|
|
|
101
|
|
|
await asyncio.wait_for(future, timeout=3) |
102
|
|
|
assert json_request_handler.call_count == 3 |
103
|
|
|
assert results[0]['method'] == 'ping' |
104
|
|
|
assert results[1]['method'] == 'ping' |
105
|
|
|
assert results[2]['method'] == 'ping' |
106
|
|
|
|