Skip to content

Commit 48f7614

Browse files
committed
add async rpc test
1 parent 9db1ed8 commit 48f7614

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import os
2+
import asyncio
3+
import hashlib
4+
5+
import pytest
6+
from wampproto import serializers
7+
8+
from xconn import AsyncClient
9+
from xconn.types import Invocation
10+
from xconn.async_client import connect_anonymous
11+
from xconn.exception import ApplicationError
12+
13+
14+
async def test_rpc():
15+
client1 = await connect_anonymous("ws://localhost:8079/ws", "realm1")
16+
client2 = await connect_anonymous("ws://localhost:8079/ws", "realm1")
17+
18+
args = ["client1", "client2"]
19+
20+
async def inv_handler_with_args(inv: Invocation):
21+
assert inv.args == args
22+
assert inv.kwargs is None
23+
24+
args_registration = await client1.register("io.xconn.rpc.args", inv_handler_with_args)
25+
await client2.call("io.xconn.rpc.args", args)
26+
await args_registration.unregister()
27+
28+
with pytest.raises(ApplicationError, match="wamp.error.no_such_procedure"):
29+
await client2.call("io.xconn.rpc.args", args)
30+
31+
kwargs = {"foo": "bar", "baz": {"k": "v"}}
32+
33+
async def inv_handler_with_kwargs(inv: Invocation):
34+
assert inv.args == []
35+
assert inv.kwargs == kwargs
36+
37+
registration = await client1.register("io.xconn.rpc.kwargs", inv_handler_with_kwargs)
38+
await client2.call("io.xconn.rpc.kwargs", kwargs=kwargs)
39+
40+
await registration.unregister()
41+
42+
await client2.leave()
43+
await client1.leave()
44+
45+
46+
@pytest.mark.parametrize(
47+
"serializer", [serializers.CBORSerializer(), serializers.MsgPackSerializer()]
48+
)
49+
async def test_rpc_with_various_data(serializer: serializers.Serializer):
50+
async_client = AsyncClient(serializer=serializer)
51+
client1 = await async_client.connect("ws://localhost:8079/ws", "realm1")
52+
async_client2 = AsyncClient(serializer=serializer)
53+
client2 = await async_client2.connect("ws://localhost:8079/ws", "realm1")
54+
55+
async def inv_handler(inv: Invocation):
56+
payload: bytes = inv.kwargs["payload"]
57+
checksum: bytes = inv.kwargs["checksum"]
58+
59+
calculated_checksum = hashlib.sha256(payload).digest()
60+
assert calculated_checksum == checksum, f"Checksum mismatch! got {calculated_checksum}, expected {checksum}"
61+
62+
await client1.register("io.xconn.rpc.inv_handler", inv_handler)
63+
64+
async def send_payload(size_bytes: int):
65+
payload = os.urandom(size_bytes)
66+
checksum = hashlib.sha256(payload).digest()
67+
68+
await client2.call("io.xconn.rpc.inv_handler", kwargs={"payload": payload, "checksum": checksum})
69+
70+
# test call with different payload sizes
71+
sizes = [1024 * n for n in [1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1023]]
72+
73+
await asyncio.gather(*(send_payload(size) for size in sizes))
74+
75+
await client1.leave()
76+
await client2.leave()

0 commit comments

Comments
 (0)