Force server methods to be async and client calls to use await

This commit is contained in:
Lasse Blaauwbroek
2023-06-08 03:56:57 +02:00
parent a69bc72a0b
commit 4b5c4211f1
11 changed files with 193 additions and 310 deletions

View File

@@ -8,27 +8,25 @@ class Server(capability.TestInterface.Server):
def __init__(self, val=1):
self.val = val
def foo(self, i, j, **kwargs):
async def foo(self, i, j, **kwargs):
extra = 0
if j:
extra = 1
return str(i * 5 + extra + self.val)
def buz(self, i, **kwargs):
async def buz(self, i, **kwargs):
return i.host + "_test"
def bam(self, i, **kwargs):
async def bam(self, i, **kwargs):
return str(i) + "_test", i
class PipelineServer(capability.TestPipeline.Server):
def getCap(self, n, inCap, _context, **kwargs):
def _then(response):
_results = _context.results
_results.s = response.x + "_foo"
_results.outBox.cap = Server(100)
return inCap.foo(i=n).then(_then)
async def getCap(self, n, inCap, _context, **kwargs):
response = await inCap.foo(i=n)
_results = _context.results
_results.s = response.x + "_foo"
_results.outBox.cap = Server(100)
async def test_client():
@@ -38,7 +36,7 @@ async def test_client():
req.i = 5
remote = req.send()
response = remote.wait()
response = await remote
assert response.x == "26"
@@ -46,7 +44,7 @@ async def test_client():
req.i = 5
remote = req.send()
response = remote.wait()
response = await remote
assert response.x == "26"
@@ -68,42 +66,42 @@ async def test_simple_client():
client = capability.TestInterface._new_client(Server())
remote = client._send("foo", i=5)
response = remote.wait()
response = await remote
assert response.x == "26"
remote = client.foo(i=5)
response = remote.wait()
response = await remote
assert response.x == "26"
remote = client.foo(i=5, j=True)
response = remote.wait()
response = await remote
assert response.x == "27"
remote = client.foo(5)
response = remote.wait()
response = await remote
assert response.x == "26"
remote = client.foo(5, True)
response = remote.wait()
response = await remote
assert response.x == "27"
remote = client.foo(5, j=True)
response = remote.wait()
response = await remote
assert response.x == "27"
remote = client.buz(capability.TestSturdyRefHostId.new_message(host="localhost"))
response = remote.wait()
response = await remote
assert response.x == "localhost_test"
remote = client.bam(i=5)
response = remote.wait()
response = await remote
assert response.x == "5_test"
assert response.i == 5
@@ -133,10 +131,10 @@ async def test_pipeline():
outCap = remote.outBox.cap
pipelinePromise = outCap.foo(i=10)
response = pipelinePromise.wait()
response = await pipelinePromise
assert response.x == "150"
response = remote.wait()
response = await remote
assert response.s == "26_foo"
@@ -144,7 +142,7 @@ class BadServer(capability.TestInterface.Server):
def __init__(self, val=1):
self.val = val
def foo(self, i, j, **kwargs):
async def foo(self, i, j, **kwargs):
extra = 0
if j:
extra = 1
@@ -156,21 +154,16 @@ async def test_exception_client():
remote = client._send("foo", i=5)
with pytest.raises(capnp.KjException):
remote.wait()
await remote
class BadPipelineServer(capability.TestPipeline.Server):
def getCap(self, n, inCap, _context, **kwargs):
def _then(response):
_results = _context.results
_results.s = response.x + "_foo"
_results.outBox.cap = Server(100)
def _error(error):
async def getCap(self, n, inCap, _context, **kwargs):
try:
await inCap.foo(i=n)
except capnp.KjException:
raise Exception("test was a success")
return inCap.foo(i=n).then(_then, _error)
async def test_exception_chain():
client = capability.TestPipeline._new_client(BadPipelineServer())
@@ -179,7 +172,7 @@ async def test_exception_chain():
remote = client.getCap(n=5, inCap=foo_client)
try:
remote.wait()
await remote
except Exception as e:
assert "test was a success" in str(e)
@@ -194,10 +187,10 @@ async def test_pipeline_exception():
pipelinePromise = outCap.foo(i=10)
with pytest.raises(Exception):
pipelinePromise.wait()
await pipelinePromise
with pytest.raises(Exception):
remote.wait()
await remote
async def test_casting():
@@ -213,7 +206,7 @@ class TailCallOrder(capability.TestCallOrder.Server):
def __init__(self):
self.count = -1
def getCallSequence(self, expected, **kwargs):
async def getCallSequence(self, expected, **kwargs):
self.count += 1
return self.count
@@ -222,18 +215,18 @@ class TailCaller(capability.TestTailCaller.Server):
def __init__(self):
self.count = 0
def foo(self, i, callee, _context, **kwargs):
async def foo(self, i, callee, _context, **kwargs):
self.count += 1
tail = callee.foo_request(i=i, t="from TailCaller")
return _context.tail_call(tail)
return await _context.tail_call(tail)
class TailCallee(capability.TestTailCallee.Server):
def __init__(self):
self.count = 0
def foo(self, i, t, _context, **kwargs):
async def foo(self, i, t, _context, **kwargs):
self.count += 1
results = _context.results
@@ -252,7 +245,7 @@ async def test_tail_call():
promise = caller.foo(i=456, callee=callee)
dependent_call1 = promise.c.getCallSequence()
response = promise.wait()
response = await promise
assert response.i == 456
assert response.i == 456
@@ -260,11 +253,11 @@ async def test_tail_call():
dependent_call2 = response.c.getCallSequence()
dependent_call3 = response.c.getCallSequence()
result = dependent_call1.wait()
result = await dependent_call1
assert result.n == 0
result = dependent_call2.wait()
result = await dependent_call2
assert result.n == 1
result = dependent_call3.wait()
result = await dependent_call3
assert result.n == 2
assert callee_server.count == 1
@@ -281,22 +274,21 @@ async def test_cancel():
remote.cancel()
with pytest.raises(Exception):
remote.wait()
await remote
req = client.foo(5)
trans = req.then(lambda x: 5)
await req
req.cancel() # Cancel a promise that was already consumed
assert trans.wait() == 5
req = client.foo(5)
req.cancel()
with pytest.raises(Exception):
trans = req.then(lambda x: 5)
await req
req = client.foo(5)
assert req.wait().x == "26"
assert (await req).x == "26"
with pytest.raises(Exception):
req.wait()
await req
async def test_double_send():
@@ -305,48 +297,18 @@ async def test_double_send():
req = client._request("foo")
req.i = 5
req.send()
await req.send()
with pytest.raises(Exception):
req.send()
async def test_then_args():
capnp.Promise(0).then(lambda x: 1)
with pytest.raises(Exception):
capnp.Promise(0).then(lambda: 1)
with pytest.raises(Exception):
capnp.Promise(0).then(lambda x, y: 1)
client = capability.TestInterface._new_client(Server())
client.foo(i=5).then(lambda x: 1)
with pytest.raises(Exception):
client.foo(i=5).then(lambda: 1)
with pytest.raises(Exception):
client.foo(i=5).then(lambda x, y: 1)
await req.send()
class PromiseJoinServer(capability.TestPipeline.Server):
def getCap(self, n, inCap, _context, **kwargs):
def _then(response):
_results = _context.results
_results.s = response.x + "_bar"
_results.outBox.cap = inCap
return (
inCap.foo(i=n)
.then(
lambda res: capnp.Promise(int(res.x))
) # Make sure that Promise is flattened
.then(
lambda x: inCap.foo(i=x + 1)
) # Make sure that RemotePromise is flattened
.then(_then)
)
async def getCap(self, n, inCap, _context, **kwargs):
res = await inCap.foo(i=n)
response = await inCap.foo(i = int(res.x) + 1)
_results = _context.results
_results.s = response.x + "_bar"
_results.outBox.cap = inCap
async def test_promise_joining():
@@ -354,54 +316,52 @@ async def test_promise_joining():
foo_client = capability.TestInterface._new_client(Server())
remote = client.getCap(n=5, inCap=foo_client)
assert remote.wait().s == "136_bar"
assert (await remote).s == "136_bar"
class ExtendsServer(Server):
def qux(self, **kwargs):
async def qux(self, **kwargs):
pass
async def test_inheritance():
client = capability.TestExtends._new_client(ExtendsServer())
client.qux().wait()
await client.qux()
remote = client.foo(i=5)
response = remote.wait()
response = await remote
assert response.x == "26"
class PassedCapTest(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)
async def foo(self, cap, _context, **kwargs):
res = await cap.foo(5)
_context.results.x = res.x
async def test_null_cap():
client = capability.TestPassedCap._new_client(PassedCapTest())
assert client.foo(Server()).wait().x == "26"
assert (await client.foo(Server())).x == "26"
with pytest.raises(capnp.KjException):
client.foo().wait()
await client.foo()
class StructArgTest(capability.TestStructArg.Server):
def bar(self, a, b, **kwargs):
async def bar(self, a, b, **kwargs):
return a + str(b)
async def test_struct_args():
client = capability.TestStructArg._new_client(StructArgTest())
assert client.bar(a="test", b=1).wait().c == "test1"
assert (await client.bar(a="test", b=1)).c == "test1"
with pytest.raises(capnp.KjException):
assert client.bar("test", 1).wait().c == "test1"
assert (await client.bar("test", 1)).c == "test1"
class GenericTest(capability.TestGeneric.Server):
def foo(self, a, **kwargs):
async def foo(self, a, **kwargs):
return a.as_text() + "test"
@@ -410,4 +370,4 @@ async def test_generic():
obj = capnp._MallocMessageBuilder().get_root_as_any()
obj.set_as_text("anypointer_")
assert client.foo(obj).wait().b == "anypointer_test"
assert (await client.foo(obj)).b == "anypointer_test"

View File

@@ -18,13 +18,13 @@ class Server:
def __init__(self, val=1):
self.val = val
def foo_context(self, context):
async def foo_context(self, context):
extra = 0
if context.params.j:
extra = 1
context.results.x = str(context.params.i * 5 + extra + self.val)
def buz_context(self, context):
async def buz_context(self, context):
context.results.x = context.params.i.host + "_test"
@@ -32,14 +32,12 @@ class PipelineServer:
def __init__(self, capability):
self.capability = capability
def getCap_context(self, context):
def _then(response):
context.results.s = response.x + "_foo"
context.results.outBox.cap = self.capability.TestInterface._new_server(
Server(100)
)
return context.params.inCap.foo(i=context.params.n).then(_then)
async def getCap_context(self, context):
response = await context.params.inCap.foo(i=context.params.n)
context.results.s = response.x + "_foo"
context.results.outBox.cap = self.capability.TestInterface._new_server(
Server(100)
)
async def test_client_context(capability):
@@ -49,7 +47,7 @@ async def test_client_context(capability):
req.i = 5
remote = req.send()
response = remote.wait()
response = await remote
assert response.x == "26"
@@ -57,7 +55,7 @@ async def test_client_context(capability):
req.i = 5
remote = req.send()
response = remote.wait()
response = await remote
assert response.x == "26"
@@ -79,37 +77,37 @@ async def test_simple_client_context(capability):
client = capability.TestInterface._new_client(Server())
remote = client._send("foo", i=5)
response = remote.wait()
response = await remote
assert response.x == "26"
remote = client.foo(i=5)
response = remote.wait()
response = await remote
assert response.x == "26"
remote = client.foo(i=5, j=True)
response = remote.wait()
response = await remote
assert response.x == "27"
remote = client.foo(5)
response = remote.wait()
response = await remote
assert response.x == "26"
remote = client.foo(5, True)
response = remote.wait()
response = await remote
assert response.x == "27"
remote = client.foo(5, j=True)
response = remote.wait()
response = await remote
assert response.x == "27"
remote = client.buz(capability.TestSturdyRefHostId.new_message(host="localhost"))
response = remote.wait()
response = await remote
assert response.x == "localhost_test"
@@ -138,10 +136,10 @@ async def test_pipeline_context(capability):
outCap = remote.outBox.cap
pipelinePromise = outCap.foo(i=10)
response = pipelinePromise.wait()
response = await pipelinePromise
assert response.x == "150"
response = remote.wait()
response = await remote
assert response.s == "26_foo"
@@ -149,7 +147,7 @@ class BadServer:
def __init__(self, val=1):
self.val = val
def foo_context(self, context):
async def foo_context(self, context):
context.results.x = str(context.params.i * 5 + self.val)
context.results.x2 = 5 # raises exception
@@ -159,25 +157,19 @@ async def test_exception_client_context(capability):
remote = client._send("foo", i=5)
with pytest.raises(capnp.KjException):
remote.wait()
await remote
class BadPipelineServer:
def __init__(self, capability):
self.capability = capability
def getCap_context(self, context):
def _then(response):
context.results.s = response.x + "_foo"
context.results.outBox.cap = self.capability.TestInterface._new_server(
Server(100)
)
def _error(error):
async def getCap_context(self, context):
try:
await context.params.inCap.foo(i=context.params.n)
except capnp.KjException:
raise Exception("test was a success")
return context.params.inCap.foo(i=context.params.n).then(_then, _error)
async def test_exception_chain_context(capability):
client = capability.TestPipeline._new_client(BadPipelineServer(capability))
@@ -186,7 +178,7 @@ async def test_exception_chain_context(capability):
remote = client.getCap(n=5, inCap=foo_client)
try:
remote.wait()
await remote
except Exception as e:
assert "test was a success" in str(e)
@@ -201,10 +193,10 @@ async def test_pipeline_exception_context(capability):
pipelinePromise = outCap.foo(i=10)
with pytest.raises(Exception):
pipelinePromise.wait()
await pipelinePromise
with pytest.raises(Exception):
remote.wait()
await remote
async def test_casting_context(capability):
@@ -220,7 +212,7 @@ class TailCallOrder:
def __init__(self):
self.count = -1
def getCallSequence_context(self, context):
async def getCallSequence_context(self, context):
self.count += 1
context.results.n = self.count
@@ -229,13 +221,13 @@ class TailCaller:
def __init__(self):
self.count = 0
def foo_context(self, context):
async def foo_context(self, context):
self.count += 1
tail = context.params.callee.foo_request(
i=context.params.i, t="from TailCaller"
)
return context.tail_call(tail)
await context.tail_call(tail)
class TailCallee:
@@ -243,7 +235,7 @@ class TailCallee:
self.count = 0
self.capability = capability
def foo_context(self, context):
async def foo_context(self, context):
self.count += 1
results = context.results
@@ -262,7 +254,7 @@ async def test_tail_call(capability):
promise = caller.foo(i=456, callee=callee)
dependent_call1 = promise.c.getCallSequence()
response = promise.wait()
response = await promise
assert response.i == 456
assert response.i == 456
@@ -270,11 +262,11 @@ async def test_tail_call(capability):
dependent_call2 = response.c.getCallSequence()
dependent_call3 = response.c.getCallSequence()
result = dependent_call1.wait()
result = await dependent_call1
assert result.n == 0
result = dependent_call2.wait()
result = await dependent_call2
assert result.n == 1
result = dependent_call3.wait()
result = await dependent_call3
assert result.n == 2
assert callee_server.count == 1

View File

@@ -17,13 +17,13 @@ class Server:
def __init__(self, val=1):
self.val = val
def foo(self, i, j, **kwargs):
async def foo(self, i, j, **kwargs):
extra = 0
if j:
extra = 1
return str(i * 5 + extra + self.val)
def buz(self, i, **kwargs):
async def buz(self, i, **kwargs):
return i.host + "_test"
@@ -31,13 +31,11 @@ class PipelineServer:
def __init__(self, capability):
self.capability = capability
def getCap(self, n, inCap, _context, **kwargs):
def _then(response):
_results = _context.results
_results.s = response.x + "_foo"
_results.outBox.cap = self.capability.TestInterface._new_server(Server(100))
return inCap.foo(i=n).then(_then)
async def getCap(self, n, inCap, _context, **kwargs):
response = await inCap.foo(i=n)
_results = _context.results
_results.s = response.x + "_foo"
_results.outBox.cap = self.capability.TestInterface._new_server(Server(100))
async def test_client(capability):
@@ -47,7 +45,7 @@ async def test_client(capability):
req.i = 5
remote = req.send()
response = remote.wait()
response = await remote
assert response.x == "26"
@@ -55,7 +53,7 @@ async def test_client(capability):
req.i = 5
remote = req.send()
response = remote.wait()
response = await remote
assert response.x == "26"
@@ -77,37 +75,37 @@ async def test_simple_client(capability):
client = capability.TestInterface._new_client(Server())
remote = client._send("foo", i=5)
response = remote.wait()
response = await remote
assert response.x == "26"
remote = client.foo(i=5)
response = remote.wait()
response = await remote
assert response.x == "26"
remote = client.foo(i=5, j=True)
response = remote.wait()
response = await remote
assert response.x == "27"
remote = client.foo(5)
response = remote.wait()
response = await remote
assert response.x == "26"
remote = client.foo(5, True)
response = remote.wait()
response = await remote
assert response.x == "27"
remote = client.foo(5, j=True)
response = remote.wait()
response = await remote
assert response.x == "27"
remote = client.buz(capability.TestSturdyRefHostId.new_message(host="localhost"))
response = remote.wait()
response = await remote
assert response.x == "localhost_test"
@@ -136,10 +134,10 @@ async def test_pipeline(capability):
outCap = remote.outBox.cap
pipelinePromise = outCap.foo(i=10)
response = pipelinePromise.wait()
response = await pipelinePromise
assert response.x == "150"
response = remote.wait()
response = await remote
assert response.s == "26_foo"
@@ -147,7 +145,7 @@ class BadServer:
def __init__(self, val=1):
self.val = val
def foo(self, i, j, **kwargs):
async def foo(self, i, j, **kwargs):
extra = 0
if j:
extra = 1
@@ -159,24 +157,19 @@ async def test_exception_client(capability):
remote = client._send("foo", i=5)
with pytest.raises(capnp.KjException):
remote.wait()
await remote
class BadPipelineServer:
def __init__(self, capability):
self.capability = capability
def getCap(self, n, inCap, _context, **kwargs):
def _then(response):
_results = _context.results
_results.s = response.x + "_foo"
_results.outBox.cap = self.capability.TestInterface._new_server(Server(100))
def _error(error):
async def getCap(self, n, inCap, _context, **kwargs):
try:
await inCap.foo(i=n)
except capnp.KjException:
raise Exception("test was a success")
return inCap.foo(i=n).then(_then, _error)
async def test_exception_chain(capability):
client = capability.TestPipeline._new_client(BadPipelineServer(capability))
@@ -185,7 +178,7 @@ async def test_exception_chain(capability):
remote = client.getCap(n=5, inCap=foo_client)
try:
remote.wait()
await remote
except Exception as e:
assert "test was a success" in str(e)
@@ -200,10 +193,10 @@ async def test_pipeline_exception(capability):
pipelinePromise = outCap.foo(i=10)
with pytest.raises(Exception):
pipelinePromise.wait()
await pipelinePromise
with pytest.raises(Exception):
remote.wait()
await remote
async def test_casting(capability):
@@ -219,7 +212,7 @@ class TailCallOrder:
def __init__(self):
self.count = -1
def getCallSequence(self, expected, **kwargs):
async def getCallSequence(self, expected, **kwargs):
self.count += 1
return self.count
@@ -228,11 +221,11 @@ class TailCaller:
def __init__(self):
self.count = 0
def foo(self, i, callee, _context, **kwargs):
async def foo(self, i, callee, _context, **kwargs):
self.count += 1
tail = callee.foo_request(i=i, t="from TailCaller")
return _context.tail_call(tail)
await _context.tail_call(tail)
class TailCallee:
@@ -240,7 +233,7 @@ class TailCallee:
self.count = 0
self.capability = capability
def foo(self, i, t, _context, **kwargs):
async def foo(self, i, t, _context, **kwargs):
self.count += 1
results = _context.results
@@ -259,7 +252,7 @@ async def test_tail_call(capability):
promise = caller.foo(i=456, callee=callee)
dependent_call1 = promise.c.getCallSequence()
response = promise.wait()
response = await promise
assert response.i == 456
assert response.i == 456
@@ -267,11 +260,11 @@ async def test_tail_call(capability):
dependent_call2 = response.c.getCallSequence()
dependent_call3 = response.c.getCallSequence()
result = dependent_call1.wait()
result = await dependent_call1
assert result.n == 0
result = dependent_call2.wait()
result = await dependent_call2
assert result.n == 1
result = dependent_call3.wait()
result = await dependent_call3
assert result.n == 2
assert callee_server.count == 1

View File

@@ -5,7 +5,7 @@ class FooServer(test_response_capnp.Foo.Server):
def __init__(self, val=1):
self.val = val
def foo(self, **kwargs):
async def foo(self, **kwargs):
return 1
@@ -13,27 +13,27 @@ class BazServer(test_response_capnp.Baz.Server):
def __init__(self, val=1):
self.val = val
def grault(self, **kwargs):
async def grault(self, **kwargs):
return {"foo": FooServer()}
async def test_response_reference():
baz = test_response_capnp.Baz._new_client(BazServer())
bar = baz.grault().wait().bar
bar = (await baz.grault()).bar
foo = bar.foo
# This used to cause an exception about invalid pointers because the response got garbage collected
assert foo.foo().wait().val == 1
assert (await foo.foo()).val == 1
async def test_response_reference2():
baz = test_response_capnp.Baz._new_client(BazServer())
bar = baz.grault().wait().bar
bar = (await baz.grault()).bar
# This always worked since it saved the intermediate response object
response = baz.grault().wait()
response = await baz.grault()
bar = response.bar
foo = bar.foo
assert foo.foo().wait().val == 1
assert (await foo.foo()).val == 1

View File

@@ -13,7 +13,7 @@ class Server(test_capability_capnp.TestInterface.Server):
def __init__(self, val=100):
self.val = val
def foo(self, i, j, **kwargs):
async def foo(self, i, j, **kwargs):
return str(i * 5 + self.val)