2021-10-01 11:00:22 -07:00
|
|
|
"""
|
2019-10-14 23:19:39 -07:00
|
|
|
rpc test
|
2021-10-01 11:00:22 -07:00
|
|
|
"""
|
2019-10-14 23:19:39 -07:00
|
|
|
|
2013-11-12 15:55:57 -08:00
|
|
|
import pytest
|
|
|
|
import capnp
|
2013-11-12 19:38:34 -08:00
|
|
|
import socket
|
2013-11-12 15:55:57 -08:00
|
|
|
|
2013-12-09 17:13:43 -08:00
|
|
|
import test_capability_capnp
|
2013-11-12 15:55:57 -08:00
|
|
|
|
2013-12-12 00:02:44 -08:00
|
|
|
|
2013-12-09 17:13:43 -08:00
|
|
|
class Server(test_capability_capnp.TestInterface.Server):
|
2019-10-14 23:19:39 -07:00
|
|
|
def __init__(self, val=100):
|
2013-11-12 15:55:57 -08:00
|
|
|
self.val = val
|
|
|
|
|
2023-06-08 03:56:57 +02:00
|
|
|
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
|
|
|
|
2013-12-10 22:57:21 -08:00
|
|
|
|
2023-06-08 02:27:38 +02:00
|
|
|
async def test_simple_rpc_with_options():
|
2019-10-19 00:22:59 -07:00
|
|
|
read, write = socket.socketpair()
|
2023-06-08 02:27:38 +02:00
|
|
|
read = await capnp.AsyncIoStream.create_connection(sock = read)
|
|
|
|
write = await capnp.AsyncIoStream.create_connection(sock = write)
|
2017-01-22 01:57:01 +00:00
|
|
|
|
2019-10-14 23:19:39 -07:00
|
|
|
_ = capnp.TwoPartyServer(write, bootstrap=Server())
|
2017-01-22 01:57:01 +00:00
|
|
|
# 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):
|
2019-10-14 23:19:39 -07:00
|
|
|
cap = client.bootstrap().cast_as(test_capability_capnp.TestInterface)
|
2019-09-27 01:09:28 -07:00
|
|
|
|
|
|
|
remote = cap.foo(i=5)
|
2019-09-26 22:18:28 -07:00
|
|
|
_ = remote.wait()
|
2017-01-22 01:57:01 +00:00
|
|
|
|
2020-11-20 23:38:13 -08:00
|
|
|
|
2023-06-08 02:27:38 +02:00
|
|
|
async def test_simple_rpc_bootstrap():
|
2019-10-19 00:22:59 -07:00
|
|
|
read, write = socket.socketpair()
|
2023-06-08 02:27:38 +02:00
|
|
|
read = await capnp.AsyncIoStream.create_connection(sock = read)
|
|
|
|
write = await capnp.AsyncIoStream.create_connection(sock = write)
|
2015-02-24 11:22:43 -08:00
|
|
|
|
2019-09-26 22:18:28 -07:00
|
|
|
_ = capnp.TwoPartyServer(write, bootstrap=Server(100))
|
2015-02-24 11:22:43 -08:00
|
|
|
client = capnp.TwoPartyClient(read)
|
|
|
|
|
|
|
|
cap = client.bootstrap()
|
|
|
|
cap = cap.cast_as(test_capability_capnp.TestInterface)
|
|
|
|
|
|
|
|
remote = cap.foo(i=5)
|
2023-06-08 02:27:38 +02:00
|
|
|
response = await remote
|
2015-02-24 11:22:43 -08:00
|
|
|
|
2021-10-01 11:00:22 -07:00
|
|
|
assert response.x == "125"
|