pycapnp/test/test_rpc.py

52 lines
1.4 KiB
Python
Raw Normal View History

"""
rpc test
"""
2013-11-12 15:55:57 -08:00
import pytest
import capnp
import socket
2013-11-12 15:55:57 -08:00
import test_capability_capnp
2013-11-12 15:55:57 -08:00
2013-12-12 00:02:44 -08:00
class Server(test_capability_capnp.TestInterface.Server):
def __init__(self, val=100):
2013-11-12 15:55:57 -08:00
self.val = val
async def foo(self, i, j, **kwargs):
2013-11-14 23:06:14 -08:00
return str(i * 5 + self.val)
2013-11-12 15:55:57 -08:00
async def test_simple_rpc_with_options():
read, write = socket.socketpair()
read = await capnp.AsyncIoStream.create_connection(sock = read)
write = await capnp.AsyncIoStream.create_connection(sock = write)
_ = capnp.TwoPartyServer(write, bootstrap=Server())
# This traversal limit is too low to receive the response in, so we expect
# an exception during the call.
client = capnp.TwoPartyClient(read, traversal_limit_in_words=1)
with pytest.raises(capnp.KjException):
cap = client.bootstrap().cast_as(test_capability_capnp.TestInterface)
remote = cap.foo(i=5)
_ = remote.wait()
async def test_simple_rpc_bootstrap():
read, write = socket.socketpair()
read = await capnp.AsyncIoStream.create_connection(sock = read)
write = await capnp.AsyncIoStream.create_connection(sock = write)
_ = capnp.TwoPartyServer(write, bootstrap=Server(100))
client = capnp.TwoPartyClient(read)
cap = client.bootstrap()
cap = cap.cast_as(test_capability_capnp.TestInterface)
remote = cap.foo(i=5)
response = await remote
assert response.x == "125"