1
|
|
|
import asyncio |
2
|
|
|
import datetime |
3
|
|
|
|
4
|
|
|
|
5
|
|
|
import aiohttp_rpc |
6
|
|
|
from tests import utils |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
async def test_args(aiohttp_client): |
10
|
|
|
def method(a=1): |
11
|
|
|
return [1, 2, a] |
12
|
|
|
|
13
|
|
|
rpc_server = aiohttp_rpc.WsJsonRpcServer() |
14
|
|
|
rpc_server.add_method(method) |
15
|
|
|
|
16
|
|
|
client = await utils.make_ws_client(aiohttp_client, rpc_server) |
17
|
|
|
|
18
|
|
|
async with aiohttp_rpc.WsJsonRpcClient('/rpc', session=client) as rpc: |
19
|
|
|
assert await rpc.call('method') == [1, 2, 1] |
20
|
|
|
assert await rpc.call('method', 1) == [1, 2, 1] |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
async def test_batch(aiohttp_client): |
24
|
|
|
def method_1(a=1): |
25
|
|
|
return [1, a] |
26
|
|
|
|
27
|
|
|
def method_2(): |
28
|
|
|
return 1 |
29
|
|
|
|
30
|
|
|
rpc_server = aiohttp_rpc.WsJsonRpcServer() |
31
|
|
|
rpc_server.add_methods((method_1, method_2,)) |
32
|
|
|
|
33
|
|
|
client = await utils.make_ws_client(aiohttp_client, rpc_server) |
34
|
|
|
|
35
|
|
|
async with aiohttp_rpc.WsJsonRpcClient('/rpc', session=client) as rpc: |
36
|
|
|
assert await rpc.batch(('method_1', 'method_2',)) == [[1, 1], 1] |
37
|
|
|
assert await rpc.batch((('method_1', 4), ('method_1', [], {'a': 5},),)) == [[1, 4], [1, 5]] |
38
|
|
|
|
39
|
|
|
async with aiohttp_rpc.WsJsonRpcClient('/rpc', session=client) as rpc: |
40
|
|
|
assert await rpc.batch_notify(('method_1', 'method_2',)) is None |
41
|
|
|
assert await rpc.batch_notify((('method_1', 4), ('method_1', [], {'a': 5},),)) is None |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
async def test_several_requests(aiohttp_client): |
45
|
|
|
async def method(a): |
46
|
|
|
await asyncio.sleep(0.2) |
47
|
|
|
return a |
48
|
|
|
|
49
|
|
|
rpc_server = aiohttp_rpc.WsJsonRpcServer() |
50
|
|
|
rpc_server.add_method(method) |
51
|
|
|
|
52
|
|
|
client = await utils.make_ws_client(aiohttp_client, rpc_server) |
53
|
|
|
|
54
|
|
|
async with aiohttp_rpc.WsJsonRpcClient('/rpc', session=client) as rpc: |
55
|
|
|
started_at = datetime.datetime.now() |
56
|
|
|
|
57
|
|
|
result = await asyncio.gather(*( |
58
|
|
|
rpc.call('method', i) |
|
|
|
|
59
|
|
|
for i in range(10) |
60
|
|
|
)) |
61
|
|
|
|
62
|
|
|
finished_at = datetime.datetime.now() |
63
|
|
|
|
64
|
|
|
assert finished_at - started_at < datetime.timedelta(seconds=1) |
65
|
|
|
assert result == list(range(10)) |
66
|
|
|
|