2013-11-12 15:55:57 -08:00
|
|
|
import pytest
|
|
|
|
import capnp
|
|
|
|
import os
|
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-09 17:13:43 -08:00
|
|
|
class Server(test_capability_capnp.TestInterface.Server):
|
2013-11-12 15:55:57 -08:00
|
|
|
def __init__(self, val=1):
|
|
|
|
self.val = val
|
|
|
|
|
2013-11-14 23:06:14 -08:00
|
|
|
def foo(self, i, j, **kwargs):
|
|
|
|
return str(i * 5 + self.val)
|
2013-11-12 15:55:57 -08:00
|
|
|
|
2013-12-10 22:57:21 -08:00
|
|
|
class TypelessRestorer:
|
|
|
|
def restore(self, ref_id):
|
|
|
|
return Server(100)
|
|
|
|
|
|
|
|
def restore_func(ref_id):
|
|
|
|
return Server(100)
|
|
|
|
|
|
|
|
class SimpleRestorer(test_capability_capnp.TestSturdyRefObjectId.Restorer):
|
|
|
|
def restore(self, ref_id):
|
|
|
|
assert ref_id.tag == 'testInterface'
|
2013-12-09 17:13:43 -08:00
|
|
|
return Server(100)
|
2013-12-11 13:50:50 -08:00
|
|
|
|
2013-12-10 22:57:21 -08:00
|
|
|
def test_simple_rpc():
|
|
|
|
|
|
|
|
read, write = socket.socketpair(socket.AF_UNIX)
|
|
|
|
|
|
|
|
restorer = SimpleRestorer()
|
|
|
|
server = capnp.TwoPartyServer(write, restorer)
|
|
|
|
client = capnp.TwoPartyClient(read)
|
|
|
|
|
|
|
|
ref = test_capability_capnp.TestSturdyRefObjectId.new_message(tag='testInterface')
|
|
|
|
cap = client.restore(ref)
|
|
|
|
cap = cap.cast_as(test_capability_capnp.TestInterface)
|
|
|
|
|
|
|
|
remote = cap.foo(i=5)
|
|
|
|
response = remote.wait()
|
|
|
|
|
|
|
|
assert response.x == '125'
|
|
|
|
|
|
|
|
def test_simple_rpc_typeless_restorer():
|
|
|
|
|
|
|
|
read, write = socket.socketpair(socket.AF_UNIX)
|
|
|
|
|
|
|
|
restorer = TypelessRestorer()
|
|
|
|
server = capnp.TwoPartyServer(write, restorer)
|
|
|
|
client = capnp.TwoPartyClient(read)
|
|
|
|
|
|
|
|
ref = test_capability_capnp.TestSturdyRefObjectId.new_message(tag='testInterface')
|
|
|
|
cap = client.restore(ref)
|
|
|
|
cap = cap.cast_as(test_capability_capnp.TestInterface)
|
|
|
|
|
|
|
|
remote = cap.foo(i=5)
|
|
|
|
response = remote.wait()
|
|
|
|
|
|
|
|
assert response.x == '125'
|
|
|
|
|
|
|
|
def test_simple_rpc_restore_func():
|
|
|
|
|
2013-11-12 19:38:34 -08:00
|
|
|
read, write = socket.socketpair(socket.AF_UNIX)
|
|
|
|
|
2013-12-10 22:57:21 -08:00
|
|
|
server = capnp.TwoPartyServer(write, restore_func)
|
|
|
|
client = capnp.TwoPartyClient(read)
|
2013-11-12 15:55:57 -08:00
|
|
|
|
2013-12-10 22:57:21 -08:00
|
|
|
ref = test_capability_capnp.TestSturdyRefObjectId.new_message(tag='testInterface')
|
2013-11-12 19:38:34 -08:00
|
|
|
cap = client.restore(ref)
|
2013-12-09 17:13:43 -08:00
|
|
|
cap = cap.cast_as(test_capability_capnp.TestInterface)
|
2013-11-12 15:55:57 -08:00
|
|
|
|
|
|
|
remote = cap.foo(i=5)
|
2013-12-02 17:38:32 -08:00
|
|
|
response = remote.wait()
|
2013-11-12 15:55:57 -08:00
|
|
|
|
|
|
|
assert response.x == '125'
|