2013-10-15 22:36:14 -07:00
|
|
|
import pytest
|
2014-04-13 18:15:36 -07:00
|
|
|
import time
|
2013-10-15 22:36:14 -07:00
|
|
|
|
2019-09-26 22:18:28 -07:00
|
|
|
import capnp
|
2013-12-09 17:13:43 -08:00
|
|
|
import test_capability_capnp as capability
|
2013-10-15 22:36:14 -07:00
|
|
|
|
2013-12-09 17:13:43 -08:00
|
|
|
class Server(capability.TestInterface.Server):
|
2013-10-17 22:42:14 -07:00
|
|
|
def __init__(self, val=1):
|
|
|
|
self.val = val
|
|
|
|
|
2013-11-14 23:06:14 -08:00
|
|
|
def foo(self, i, j, **kwargs):
|
|
|
|
extra = 0
|
|
|
|
if j:
|
|
|
|
extra = 1
|
|
|
|
return str(i * 5 + extra + self.val)
|
|
|
|
|
|
|
|
def buz(self, i, **kwargs):
|
|
|
|
return i.host + '_test'
|
2013-10-17 22:42:14 -07:00
|
|
|
|
2013-12-11 01:17:23 -08:00
|
|
|
def bam(self, i, **kwargs):
|
|
|
|
return str(i) + '_test', i
|
|
|
|
|
2013-12-09 17:13:43 -08:00
|
|
|
class PipelineServer(capability.TestPipeline.Server):
|
2013-12-05 00:07:51 -08:00
|
|
|
def getCap(self, n, inCap, _context, **kwargs):
|
2013-10-17 22:42:14 -07:00
|
|
|
def _then(response):
|
2013-12-05 00:07:51 -08:00
|
|
|
_results = _context.results
|
2013-11-14 23:06:14 -08:00
|
|
|
_results.s = response.x + '_foo'
|
2013-12-09 17:13:43 -08:00
|
|
|
_results.outBox.cap = Server(100)
|
2013-10-17 22:42:14 -07:00
|
|
|
|
2013-11-14 23:06:14 -08:00
|
|
|
return inCap.foo(i=n).then(_then)
|
2013-10-15 22:36:14 -07:00
|
|
|
|
2013-12-09 17:13:43 -08:00
|
|
|
def test_client():
|
2013-12-02 17:38:32 -08:00
|
|
|
client = capability.TestInterface._new_client(Server())
|
2014-11-30 14:27:10 -08:00
|
|
|
|
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()
|
2013-12-02 17:38:32 -08:00
|
|
|
response = remote.wait()
|
2013-10-15 22:36:14 -07:00
|
|
|
|
2013-10-17 19:15:40 -07:00
|
|
|
assert response.x == '26'
|
2014-11-30 14:27:10 -08:00
|
|
|
|
2013-10-17 19:15:40 -07:00
|
|
|
req = client.foo_request()
|
|
|
|
req.i = 5
|
|
|
|
|
|
|
|
remote = req.send()
|
2013-12-02 17:38:32 -08:00
|
|
|
response = remote.wait()
|
2013-10-17 19:15:40 -07:00
|
|
|
|
|
|
|
assert response.x == '26'
|
|
|
|
|
2014-04-17 20:53:52 -07:00
|
|
|
with pytest.raises(AttributeError):
|
2013-10-17 19:15:40 -07:00
|
|
|
client.foo2_request()
|
|
|
|
|
|
|
|
req = client.foo_request()
|
|
|
|
|
2014-11-30 14:27:10 -08:00
|
|
|
with pytest.raises(Exception):
|
2013-10-17 19:15:40 -07:00
|
|
|
req.i = 'foo'
|
|
|
|
|
|
|
|
req = client.foo_request()
|
|
|
|
|
2014-01-14 09:44:21 -08:00
|
|
|
with pytest.raises(AttributeError):
|
2013-10-17 19:15:40 -07:00
|
|
|
req.baz = 1
|
|
|
|
|
2013-12-09 17:13:43 -08:00
|
|
|
def test_simple_client():
|
2013-12-02 17:38:32 -08:00
|
|
|
client = capability.TestInterface._new_client(Server())
|
2014-11-30 14:27:10 -08:00
|
|
|
|
2013-10-17 19:15:40 -07:00
|
|
|
remote = client._send('foo', i=5)
|
2013-12-02 17:38:32 -08:00
|
|
|
response = remote.wait()
|
2013-10-17 19:15:40 -07:00
|
|
|
|
|
|
|
assert response.x == '26'
|
|
|
|
|
2014-11-30 14:27:10 -08:00
|
|
|
|
2013-10-17 19:15:40 -07:00
|
|
|
remote = client.foo(i=5)
|
2013-12-02 17:38:32 -08:00
|
|
|
response = remote.wait()
|
2013-10-17 19:15:40 -07:00
|
|
|
|
|
|
|
assert response.x == '26'
|
2014-11-30 14:27:10 -08:00
|
|
|
|
2013-11-14 23:06:14 -08:00
|
|
|
remote = client.foo(i=5, j=True)
|
2013-12-02 17:38:32 -08:00
|
|
|
response = remote.wait()
|
2013-11-14 23:06:14 -08:00
|
|
|
|
|
|
|
assert response.x == '27'
|
2013-10-17 19:15:40 -07:00
|
|
|
|
2013-11-14 20:59:21 -08:00
|
|
|
remote = client.foo(5)
|
2013-12-02 17:38:32 -08:00
|
|
|
response = remote.wait()
|
2013-11-14 20:59:21 -08:00
|
|
|
|
|
|
|
assert response.x == '26'
|
|
|
|
|
2013-11-14 23:06:14 -08:00
|
|
|
remote = client.foo(5, True)
|
2013-12-02 17:38:32 -08:00
|
|
|
response = remote.wait()
|
2013-11-14 23:06:14 -08:00
|
|
|
|
|
|
|
assert response.x == '27'
|
|
|
|
|
|
|
|
remote = client.foo(5, j=True)
|
2013-12-02 17:38:32 -08:00
|
|
|
response = remote.wait()
|
2013-11-14 23:06:14 -08:00
|
|
|
|
|
|
|
assert response.x == '27'
|
|
|
|
|
|
|
|
remote = client.buz(capability.TestSturdyRefHostId.new_message(host='localhost'))
|
2013-12-02 17:38:32 -08:00
|
|
|
response = remote.wait()
|
2013-11-14 23:06:14 -08:00
|
|
|
|
|
|
|
assert response.x == 'localhost_test'
|
|
|
|
|
2013-12-11 01:17:23 -08:00
|
|
|
remote = client.bam(i=5)
|
|
|
|
response = remote.wait()
|
|
|
|
|
|
|
|
assert response.x == '5_test'
|
|
|
|
assert response.i == 5
|
|
|
|
|
2014-11-30 14:27:10 -08:00
|
|
|
with pytest.raises(Exception):
|
2013-11-14 23:06:14 -08:00
|
|
|
remote = client.foo(5, 10)
|
|
|
|
|
2014-11-30 14:27:10 -08:00
|
|
|
with pytest.raises(Exception):
|
2013-11-14 23:06:14 -08:00
|
|
|
remote = client.foo(5, True, 100)
|
|
|
|
|
2014-11-30 14:27:10 -08:00
|
|
|
with pytest.raises(Exception):
|
2013-10-17 19:15:40 -07:00
|
|
|
remote = client.foo(i='foo')
|
|
|
|
|
2014-04-17 20:53:52 -07:00
|
|
|
with pytest.raises(AttributeError):
|
2013-10-17 19:15:40 -07:00
|
|
|
remote = client.foo2(i=5)
|
2013-10-15 22:36:14 -07:00
|
|
|
|
2014-11-30 14:27:10 -08:00
|
|
|
with pytest.raises(Exception):
|
2013-10-17 19:15:40 -07:00
|
|
|
remote = client.foo(baz=5)
|
2013-10-17 22:42:14 -07:00
|
|
|
|
2013-12-09 17:13:43 -08:00
|
|
|
def test_pipeline():
|
2013-12-02 17:38:32 -08:00
|
|
|
client = capability.TestPipeline._new_client(PipelineServer())
|
|
|
|
foo_client = capability.TestInterface._new_client(Server())
|
2013-10-17 22:42:14 -07:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2013-12-02 17:38:32 -08:00
|
|
|
response = pipelinePromise.wait()
|
2013-10-19 22:38:10 -07:00
|
|
|
assert response.x == '150'
|
|
|
|
|
2013-12-02 17:38:32 -08:00
|
|
|
response = remote.wait()
|
2013-10-17 22:42:14 -07:00
|
|
|
assert response.s == '26_foo'
|
2013-10-20 17:24:59 -07:00
|
|
|
|
2013-12-09 17:13:43 -08:00
|
|
|
class BadServer(capability.TestInterface.Server):
|
2013-10-20 17:24:59 -07:00
|
|
|
def __init__(self, val=1):
|
|
|
|
self.val = val
|
|
|
|
|
2013-11-14 23:06:14 -08:00
|
|
|
def foo(self, i, j, **kwargs):
|
|
|
|
extra = 0
|
|
|
|
if j:
|
|
|
|
extra = 1
|
|
|
|
return str(i * 5 + extra + self.val), 10 # returning too many args
|
2013-10-20 17:24:59 -07:00
|
|
|
|
2013-12-09 17:13:43 -08:00
|
|
|
def test_exception_client():
|
2013-12-02 17:38:32 -08:00
|
|
|
client = capability.TestInterface._new_client(BadServer())
|
2014-11-30 14:27:10 -08:00
|
|
|
|
2013-10-20 17:24:59 -07:00
|
|
|
remote = client._send('foo', i=5)
|
2013-11-13 20:54:57 -08:00
|
|
|
with pytest.raises(capnp.KjException):
|
2013-12-02 17:38:32 -08:00
|
|
|
remote.wait()
|
2013-10-20 17:24:59 -07:00
|
|
|
|
2013-12-09 17:13:43 -08:00
|
|
|
class BadPipelineServer(capability.TestPipeline.Server):
|
2013-12-05 00:07:51 -08:00
|
|
|
def getCap(self, n, inCap, _context, **kwargs):
|
2013-10-20 17:24:59 -07:00
|
|
|
def _then(response):
|
2013-12-05 00:07:51 -08:00
|
|
|
_results = _context.results
|
2013-11-14 23:06:14 -08:00
|
|
|
_results.s = response.x + '_foo'
|
2013-12-09 17:13:43 -08:00
|
|
|
_results.outBox.cap = Server(100)
|
2019-09-26 22:18:28 -07:00
|
|
|
|
2013-10-20 17:24:59 -07:00
|
|
|
def _error(error):
|
2013-11-13 20:54:57 -08:00
|
|
|
raise Exception('test was a success')
|
2013-10-20 17:24:59 -07:00
|
|
|
|
2013-11-14 23:06:14 -08:00
|
|
|
return inCap.foo(i=n).then(_then, _error)
|
2013-10-20 17:24:59 -07:00
|
|
|
|
2013-12-09 17:13:43 -08:00
|
|
|
def test_exception_chain():
|
2013-12-02 17:38:32 -08:00
|
|
|
client = capability.TestPipeline._new_client(BadPipelineServer())
|
|
|
|
foo_client = capability.TestInterface._new_client(BadServer())
|
2013-10-20 17:24:59 -07:00
|
|
|
|
|
|
|
remote = client.getCap(n=5, inCap=foo_client)
|
|
|
|
|
|
|
|
try:
|
2013-12-02 17:38:32 -08:00
|
|
|
remote.wait()
|
2013-10-20 17:24:59 -07:00
|
|
|
except Exception as e:
|
2013-11-13 20:54:57 -08:00
|
|
|
assert 'test was a success' in str(e)
|
2013-10-20 17:24:59 -07:00
|
|
|
|
2013-12-09 17:13:43 -08:00
|
|
|
def test_pipeline_exception():
|
2013-12-02 17:38:32 -08:00
|
|
|
client = capability.TestPipeline._new_client(BadPipelineServer())
|
|
|
|
foo_client = capability.TestInterface._new_client(BadServer())
|
2013-10-20 17:24:59 -07:00
|
|
|
|
|
|
|
remote = client.getCap(n=5, inCap=foo_client)
|
|
|
|
|
|
|
|
outCap = remote.outBox.cap
|
|
|
|
pipelinePromise = outCap.foo(i=10)
|
|
|
|
|
|
|
|
with pytest.raises(Exception):
|
2019-09-26 22:18:28 -07:00
|
|
|
pipelinePromise.wait()
|
2013-10-20 17:24:59 -07:00
|
|
|
|
|
|
|
with pytest.raises(Exception):
|
2013-12-02 17:38:32 -08:00
|
|
|
remote.wait()
|
2013-11-12 20:28:23 -08:00
|
|
|
|
2013-12-09 17:13:43 -08:00
|
|
|
def test_casting():
|
2013-12-02 17:38:32 -08:00
|
|
|
client = capability.TestExtends._new_client(Server())
|
2013-11-12 20:28:23 -08:00
|
|
|
client2 = client.upcast(capability.TestInterface)
|
2019-09-26 22:18:28 -07:00
|
|
|
_ = client2.cast_as(capability.TestInterface)
|
2013-11-12 20:28:23 -08:00
|
|
|
|
|
|
|
with pytest.raises(Exception):
|
|
|
|
client.upcast(capability.TestPipeline)
|
2013-12-05 00:07:51 -08:00
|
|
|
|
2013-12-09 17:13:43 -08:00
|
|
|
class TailCallOrder(capability.TestCallOrder.Server):
|
2013-12-05 00:07:51 -08:00
|
|
|
def __init__(self):
|
|
|
|
self.count = -1
|
|
|
|
|
|
|
|
def getCallSequence(self, expected, **kwargs):
|
|
|
|
self.count += 1
|
|
|
|
return self.count
|
|
|
|
|
2013-12-09 17:13:43 -08:00
|
|
|
class TailCaller(capability.TestTailCaller.Server):
|
2013-12-05 00:07:51 -08:00
|
|
|
def __init__(self):
|
|
|
|
self.count = 0
|
|
|
|
|
|
|
|
def foo(self, i, callee, _context, **kwargs):
|
|
|
|
self.count += 1
|
|
|
|
|
|
|
|
tail = callee.foo_request(i=i, t='from TailCaller')
|
|
|
|
return _context.tail_call(tail)
|
|
|
|
|
2013-12-09 17:13:43 -08:00
|
|
|
class TailCallee(capability.TestTailCallee.Server):
|
2013-12-05 00:07:51 -08:00
|
|
|
def __init__(self):
|
|
|
|
self.count = 0
|
|
|
|
|
|
|
|
def foo(self, i, t, _context, **kwargs):
|
|
|
|
self.count += 1
|
|
|
|
|
|
|
|
results = _context.results
|
|
|
|
results.i = i
|
|
|
|
results.t = t
|
2013-12-09 17:13:43 -08:00
|
|
|
results.c = TailCallOrder()
|
2013-12-05 00:07:51 -08:00
|
|
|
|
2013-12-09 17:13:43 -08:00
|
|
|
def test_tail_call():
|
2013-12-05 00:07:51 -08:00
|
|
|
callee_server = TailCallee()
|
|
|
|
caller_server = TailCaller()
|
|
|
|
|
|
|
|
callee = capability.TestTailCallee._new_client(callee_server)
|
|
|
|
caller = capability.TestTailCaller._new_client(caller_server)
|
|
|
|
|
|
|
|
promise = caller.foo(i=456, callee=callee)
|
|
|
|
dependent_call1 = promise.c.getCallSequence()
|
|
|
|
|
|
|
|
response = promise.wait()
|
|
|
|
|
|
|
|
assert response.i == 456
|
|
|
|
assert response.i == 456
|
|
|
|
|
|
|
|
dependent_call2 = response.c.getCallSequence()
|
|
|
|
dependent_call3 = response.c.getCallSequence()
|
|
|
|
|
|
|
|
result = dependent_call1.wait()
|
|
|
|
assert result.n == 0
|
|
|
|
result = dependent_call2.wait()
|
|
|
|
assert result.n == 1
|
|
|
|
result = dependent_call3.wait()
|
|
|
|
assert result.n == 2
|
|
|
|
|
|
|
|
assert callee_server.count == 1
|
2014-04-10 19:08:24 -07:00
|
|
|
assert caller_server.count == 1
|
|
|
|
|
|
|
|
|
2014-04-13 18:15:36 -07:00
|
|
|
def test_cancel():
|
|
|
|
client = capability.TestInterface._new_client(Server())
|
|
|
|
|
|
|
|
req = client._request('foo')
|
|
|
|
req.i = 5
|
|
|
|
|
|
|
|
remote = req.send()
|
|
|
|
remote.cancel()
|
|
|
|
|
2014-11-30 14:27:10 -08:00
|
|
|
with pytest.raises(Exception):
|
2014-04-13 18:15:36 -07:00
|
|
|
remote.wait()
|
|
|
|
|
|
|
|
|
2014-04-10 19:08:24 -07:00
|
|
|
def test_timer():
|
|
|
|
global test_timer_var
|
|
|
|
test_timer_var = False
|
|
|
|
|
|
|
|
def set_timer_var():
|
|
|
|
global test_timer_var
|
|
|
|
test_timer_var = True
|
|
|
|
capnp.getTimer().after_delay(1).then(set_timer_var).wait()
|
|
|
|
|
|
|
|
assert test_timer_var is True
|
2014-04-13 18:15:36 -07:00
|
|
|
|
2014-04-17 20:53:52 -07:00
|
|
|
test_timer_var = False
|
|
|
|
promise = capnp.Promise(0).then(lambda x: time.sleep(.1)).then(lambda x: time.sleep(.1)).then(lambda x: set_timer_var())
|
2014-04-13 18:15:36 -07:00
|
|
|
|
2014-04-17 20:53:52 -07:00
|
|
|
canceller = capnp.getTimer().after_delay(1).then(lambda: promise.cancel())
|
2014-04-13 18:15:36 -07:00
|
|
|
|
2014-04-17 20:53:52 -07:00
|
|
|
joined = capnp.join_promises([canceller, promise])
|
|
|
|
joined.wait()
|
|
|
|
|
|
|
|
# faling for now, not sure why...
|
|
|
|
# assert test_timer_var is False
|
2014-04-13 18:26:01 -07:00
|
|
|
|
|
|
|
|
2014-04-13 18:35:53 -07:00
|
|
|
def test_double_send():
|
|
|
|
client = capability.TestInterface._new_client(Server())
|
|
|
|
|
|
|
|
req = client._request('foo')
|
|
|
|
req.i = 5
|
|
|
|
|
|
|
|
req.send()
|
2014-11-30 14:27:10 -08:00
|
|
|
with pytest.raises(Exception):
|
2014-04-13 18:35:53 -07:00
|
|
|
req.send()
|
|
|
|
|
|
|
|
|
2014-04-13 18:26:01 -07:00
|
|
|
def test_then_args():
|
|
|
|
capnp.Promise(0).then(lambda x: 1)
|
|
|
|
|
2014-11-30 14:27:10 -08:00
|
|
|
with pytest.raises(Exception):
|
2014-04-13 18:26:01 -07:00
|
|
|
capnp.Promise(0).then(lambda: 1)
|
|
|
|
|
2014-11-30 14:27:10 -08:00
|
|
|
with pytest.raises(Exception):
|
2014-04-13 18:26:01 -07:00
|
|
|
capnp.Promise(0).then(lambda x, y: 1)
|
2014-04-13 18:35:53 -07:00
|
|
|
|
|
|
|
capnp.getTimer().after_delay(1).then(lambda: 1) # after_delay is a VoidPromise
|
|
|
|
|
2014-11-30 14:27:10 -08:00
|
|
|
with pytest.raises(Exception):
|
2014-04-13 18:35:53 -07:00
|
|
|
capnp.getTimer().after_delay(1).then(lambda x: 1)
|
|
|
|
|
|
|
|
client = capability.TestInterface._new_client(Server())
|
|
|
|
|
|
|
|
client.foo(i=5).then(lambda x: 1)
|
|
|
|
|
2014-11-30 14:27:10 -08:00
|
|
|
with pytest.raises(Exception):
|
2014-04-13 18:35:53 -07:00
|
|
|
client.foo(i=5).then(lambda: 1)
|
|
|
|
|
2014-11-30 14:27:10 -08:00
|
|
|
with pytest.raises(Exception):
|
2014-04-13 18:35:53 -07:00
|
|
|
client.foo(i=5).then(lambda x, y: 1)
|
2014-04-18 18:18:54 -07:00
|
|
|
|
|
|
|
|
|
|
|
class ExtendsServer(Server):
|
|
|
|
def qux(self, **kwargs):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def test_inheritance():
|
|
|
|
client = capability.TestExtends._new_client(ExtendsServer())
|
|
|
|
client.qux().wait()
|
|
|
|
|
|
|
|
remote = client.foo(i=5)
|
|
|
|
response = remote.wait()
|
|
|
|
|
|
|
|
assert response.x == '26'
|
2015-06-03 14:35:39 -07:00
|
|
|
|
|
|
|
|
|
|
|
class TestPassedCap(capability.TestPassedCap.Server):
|
|
|
|
def foo(self, cap, _context, **kwargs):
|
|
|
|
def set_result(res):
|
|
|
|
_context.results.x = res.x
|
|
|
|
return cap.foo(5).then(set_result)
|
|
|
|
|
|
|
|
|
|
|
|
def test_null_cap():
|
|
|
|
client = capability.TestPassedCap._new_client(TestPassedCap())
|
|
|
|
assert client.foo(Server()).wait().x == '26'
|
|
|
|
|
|
|
|
with pytest.raises(capnp.KjException):
|
|
|
|
client.foo().wait()
|
2015-06-08 13:39:10 -07:00
|
|
|
|
|
|
|
|
|
|
|
class TestStructArg(capability.TestStructArg.Server):
|
|
|
|
def bar(self, a, b, **kwargs):
|
|
|
|
return a + str(b)
|
|
|
|
|
|
|
|
|
|
|
|
def test_struct_args():
|
|
|
|
client = capability.TestStructArg._new_client(TestStructArg())
|
|
|
|
assert client.bar(a='test', b=1).wait().c == 'test1'
|
|
|
|
with pytest.raises(capnp.KjException):
|
|
|
|
assert client.bar('test', 1).wait().c == 'test1'
|
2015-06-08 14:59:21 -07:00
|
|
|
|
|
|
|
|
|
|
|
class TestGeneric(capability.TestGeneric.Server):
|
|
|
|
def foo(self, a, **kwargs):
|
|
|
|
return a.as_text() + 'test'
|
|
|
|
|
|
|
|
|
|
|
|
def test_generic():
|
|
|
|
client = capability.TestGeneric._new_client(TestGeneric())
|
|
|
|
|
|
|
|
obj = capnp._MallocMessageBuilder().get_root_as_any()
|
|
|
|
obj.set_as_text("anypointer_")
|
|
|
|
assert client.foo(obj).wait().b == 'anypointer_test'
|