From c025761f15bb09c308ffc44f4776e71a4d2a60f4 Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Thu, 7 Mar 2024 21:33:07 -0500 Subject: [PATCH] Adjust `asyncio` test for stricter ctx-self-cancels Use `expect_ctx()` around the portal cancellation case, toss in a `'context'` parametrization and return just the `Context.outcome` from `main()` B) --- tests/test_infected_asyncio.py | 35 +++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/tests/test_infected_asyncio.py b/tests/test_infected_asyncio.py index 56b5fde..24bd371 100644 --- a/tests/test_infected_asyncio.py +++ b/tests/test_infected_asyncio.py @@ -19,6 +19,8 @@ from tractor import ( ) from tractor.trionics import BroadcastReceiver +from conftest import expect_ctxc + async def sleep_and_err( sleep_for: float = 0.1, @@ -190,7 +192,8 @@ async def trio_ctx( @pytest.mark.parametrize( - 'parent_cancels', [False, True], + 'parent_cancels', + ['context', 'actor', False], ids='parent_actor_cancels_child={}'.format ) def test_context_spawns_aio_task_that_errors( @@ -214,18 +217,36 @@ def test_context_spawns_aio_task_that_errors( # debug_mode=True, loglevel='cancel', ) - async with p.open_context( - trio_ctx, - ) as (ctx, first): + async with ( + expect_ctxc( + yay=parent_cancels == 'actor', + ), + p.open_context( + trio_ctx, + ) as (ctx, first), + ): assert first == 'start' - if parent_cancels: + if parent_cancels == 'actor': await p.cancel_actor() - await trio.sleep_forever() + elif parent_cancels == 'context': + await ctx.cancel() - return await ctx.result() + else: + await trio.sleep_forever() + + async with expect_ctxc( + yay=parent_cancels == 'actor', + ): + await ctx.result() + + if parent_cancels == 'context': + # to tear down sub-acor + await p.cancel_actor() + + return ctx.outcome if parent_cancels: # bc the parent made the cancel request,