Passed
Push — master ( 135e2c...0076f8 )
by Michael
09:26 queued 05:17
created

tests.test_websocket.test_several_requests()   A

Complexity

Conditions 2

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 22
rs 9.65
c 0
b 0
f 0
cc 2
nop 1
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)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable i does not seem to be defined.
Loading history...
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