2023-10-03 18:04:51 +02:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
import capnp
|
2016-06-17 13:50:42 -07:00
|
|
|
import test_response_capnp
|
|
|
|
|
2019-12-11 22:44:44 -08:00
|
|
|
|
2023-10-03 18:04:51 +02:00
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
async def kj_loop():
|
|
|
|
async with capnp.kj_loop():
|
|
|
|
yield
|
|
|
|
|
|
|
|
|
2016-06-17 13:50:42 -07:00
|
|
|
class FooServer(test_response_capnp.Foo.Server):
|
|
|
|
def __init__(self, val=1):
|
|
|
|
self.val = val
|
|
|
|
|
2023-06-08 03:56:57 +02:00
|
|
|
async def foo(self, **kwargs):
|
2016-06-17 13:50:42 -07:00
|
|
|
return 1
|
|
|
|
|
2019-12-11 22:44:44 -08:00
|
|
|
|
2016-06-17 13:50:42 -07:00
|
|
|
class BazServer(test_response_capnp.Baz.Server):
|
|
|
|
def __init__(self, val=1):
|
|
|
|
self.val = val
|
|
|
|
|
2023-06-08 03:56:57 +02:00
|
|
|
async def grault(self, **kwargs):
|
2016-06-17 13:50:42 -07:00
|
|
|
return {"foo": FooServer()}
|
|
|
|
|
2019-12-11 22:44:44 -08:00
|
|
|
|
2023-06-08 02:27:38 +02:00
|
|
|
async def test_response_reference():
|
2016-06-17 13:50:42 -07:00
|
|
|
baz = test_response_capnp.Baz._new_client(BazServer())
|
|
|
|
|
2023-06-08 03:56:57 +02:00
|
|
|
bar = (await baz.grault()).bar
|
2016-06-17 13:50:42 -07:00
|
|
|
|
|
|
|
foo = bar.foo
|
|
|
|
# This used to cause an exception about invalid pointers because the response got garbage collected
|
2023-06-08 03:56:57 +02:00
|
|
|
assert (await foo.foo()).val == 1
|
2016-06-17 13:50:42 -07:00
|
|
|
|
2019-12-11 22:44:44 -08:00
|
|
|
|
2023-06-08 02:27:38 +02:00
|
|
|
async def test_response_reference2():
|
2016-06-17 13:50:42 -07:00
|
|
|
baz = test_response_capnp.Baz._new_client(BazServer())
|
|
|
|
|
2023-06-08 03:56:57 +02:00
|
|
|
bar = (await baz.grault()).bar
|
2016-06-17 13:50:42 -07:00
|
|
|
|
|
|
|
# This always worked since it saved the intermediate response object
|
2023-06-08 03:56:57 +02:00
|
|
|
response = await baz.grault()
|
2016-06-17 13:50:42 -07:00
|
|
|
bar = response.bar
|
|
|
|
foo = bar.foo
|
2023-06-08 03:56:57 +02:00
|
|
|
assert (await foo.foo()).val == 1
|