2013-10-15 22:36:14 -07:00
|
|
|
import pytest
|
|
|
|
import capnp
|
|
|
|
import os
|
|
|
|
|
|
|
|
this_dir = os.path.dirname(__file__)
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def capability():
|
|
|
|
return capnp.load(os.path.join(this_dir, 'test_capability.capnp'))
|
|
|
|
|
|
|
|
class Server:
|
2013-10-17 22:42:14 -07:00
|
|
|
def __init__(self, val=1):
|
|
|
|
self.val = val
|
|
|
|
|
2013-10-15 22:36:14 -07:00
|
|
|
def foo(self, context):
|
2013-10-17 22:42:14 -07:00
|
|
|
context.results.x = str(context.params.i * 5 + self.val)
|
|
|
|
|
|
|
|
class PipelineServer:
|
2013-10-19 22:38:10 -07:00
|
|
|
def __init__(self, capability):
|
|
|
|
self.capability = capability
|
|
|
|
|
2013-10-17 22:42:14 -07:00
|
|
|
def getCap(self, context):
|
|
|
|
def _then(response):
|
|
|
|
context.results.s = response.x + '_foo'
|
2013-10-19 22:38:10 -07:00
|
|
|
context.results.outBox.cap = self.capability.TestInterface.new_server(Server(100))
|
2013-10-17 22:42:14 -07:00
|
|
|
|
|
|
|
return context.params.inCap.foo(i=context.params.n).then(_then)
|
2013-10-15 22:36:14 -07:00
|
|
|
|
2013-10-17 22:42:14 -07:00
|
|
|
def test_client(capability):
|
2013-10-15 22:36:14 -07:00
|
|
|
loop = capnp.EventLoop()
|
|
|
|
|
|
|
|
client = capability.TestInterface.new_client(Server(), loop)
|
|
|
|
|
2013-10-17 19:15:40 -07:00
|
|
|
req = client._request('foo')
|
2013-10-15 22:36:14 -07:00
|
|
|
req.i = 5
|
|
|
|
|
|
|
|
remote = req.send()
|
|
|
|
response = loop.wait_remote(remote)
|
|
|
|
|
2013-10-17 19:15:40 -07:00
|
|
|
assert response.x == '26'
|
|
|
|
|
|
|
|
req = client.foo_request()
|
|
|
|
req.i = 5
|
|
|
|
|
|
|
|
remote = req.send()
|
|
|
|
response = loop.wait_remote(remote)
|
|
|
|
|
|
|
|
assert response.x == '26'
|
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
client.foo2_request()
|
|
|
|
|
|
|
|
req = client.foo_request()
|
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
req.i = 'foo'
|
|
|
|
|
|
|
|
req = client.foo_request()
|
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
req.baz = 1
|
|
|
|
|
|
|
|
def test_simple_client(capability):
|
|
|
|
loop = capnp.EventLoop()
|
|
|
|
|
|
|
|
client = capability.TestInterface.new_client(Server(), loop)
|
|
|
|
|
|
|
|
remote = client._send('foo', i=5)
|
|
|
|
response = loop.wait_remote(remote)
|
|
|
|
|
|
|
|
assert response.x == '26'
|
|
|
|
|
|
|
|
|
|
|
|
remote = client.foo(i=5)
|
|
|
|
response = loop.wait_remote(remote)
|
|
|
|
|
|
|
|
assert response.x == '26'
|
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
remote = client.foo(i='foo')
|
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
remote = client.foo2(i=5)
|
2013-10-15 22:36:14 -07:00
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
2013-10-17 19:15:40 -07:00
|
|
|
remote = client.foo(baz=5)
|
2013-10-17 22:42:14 -07:00
|
|
|
|
|
|
|
def test_pipeline(capability):
|
|
|
|
loop = capnp.EventLoop()
|
|
|
|
|
2013-10-19 22:38:10 -07:00
|
|
|
client = capability.TestPipeline.new_client(PipelineServer(capability), loop)
|
2013-10-17 22:42:14 -07:00
|
|
|
foo_client = capability.TestInterface.new_client(Server(), loop)
|
|
|
|
|
|
|
|
remote = client.getCap(n=5, inCap=foo_client)
|
|
|
|
|
2013-10-19 22:38:10 -07:00
|
|
|
outCap = remote.outBox.cap
|
|
|
|
pipelinePromise = outCap.foo(i=10)
|
|
|
|
|
|
|
|
response = loop.wait_remote(pipelinePromise)
|
|
|
|
assert response.x == '150'
|
|
|
|
|
|
|
|
response = loop.wait_remote(remote)
|
2013-10-17 22:42:14 -07:00
|
|
|
assert response.s == '26_foo'
|
|
|
|
|
|
|
|
|
2013-10-19 22:38:10 -07:00
|
|
|
|
|
|
|
|