From 04c0eda69dee09df0135c62d62bad1bcbedb9112 Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Wed, 17 Nov 2021 13:32:42 -0500 Subject: [PATCH] Add an `asyncio`-internal cancel test Verify that if the `asyncio` side task cancels (itself) that we raise that `asyncio.CancelledError` on the `trio` side. In the case where `trio` initiated the cancel whether or not the `asyncio` side ended up raising `CancelledError` doesn't really matter to us as long as the far task did indeed terminate. --- tests/test_infected_asyncio.py | 46 ++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 8 deletions(-) diff --git a/tests/test_infected_asyncio.py b/tests/test_infected_asyncio.py index ad7758d..6cb742a 100644 --- a/tests/test_infected_asyncio.py +++ b/tests/test_infected_asyncio.py @@ -35,17 +35,20 @@ async def asyncio_actor( if '.' in expect_err: modpath, _, name = expect_err.rpartition('.') mod = importlib.import_module(modpath) - error = getattr(mod, name) - error = builtins.__dict__.get(expect_err) + error_type = getattr(mod, name) + + else: # toplevel builtin error type + error_type = builtins.__dict__.get(expect_err) try: # spawn an ``asyncio`` task to run a func and return result await tractor.to_asyncio.run_task(target) - except Exception as err: - if expect_err: - assert isinstance(err, error) - raise + except BaseException as err: + if expect_err: + assert isinstance(err, error_type) + + raise err def test_aio_simple_error(arb_addr): @@ -84,16 +87,43 @@ def test_tractor_cancels_aio(arb_addr): portal = await n.run_in_actor( asyncio_actor, target='sleep_forever', - expect_err='asyncio.CancelledError', + expect_err='trio.Cancelled', infect_asyncio=True, ) + # cancel the entire remote runtime await portal.cancel_actor() trio.run(main) +async def aio_cancel(): + ''''Cancel urself boi. + + ''' + await asyncio.sleep(0.5) + task = asyncio.current_task() + + # cancel and enter sleep + task.cancel() + await sleep_forever() + + def test_aio_cancelled_from_aio_causes_trio_cancelled(arb_addr): - ... + + async def main(): + async with tractor.open_nursery() as n: + portal = await n.run_in_actor( + asyncio_actor, + target='aio_cancel', + expect_err='asyncio.CancelledError', + infect_asyncio=True, + ) + + # with trio.CancelScope(shield=True): + await portal.result() + + with pytest.raises(RemoteActorError) as excinfo: + trio.run(main) def test_trio_cancels_aio(arb_addr):