Skip to content

Commit f078a3d

Browse files
test: bound reentrant adopted-Task shutdown regressions
Add tests for TaskSupervisor and Connection closure behavior.
1 parent 167f5f4 commit f078a3d

1 file changed

Lines changed: 93 additions & 0 deletions

File tree

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
"""Keep cancellation-cycle regressions isolated so a broken loop cannot hang pytest."""
2+
3+
from __future__ import annotations
4+
5+
import subprocess
6+
import sys
7+
import textwrap
8+
9+
import pytest
10+
11+
12+
@pytest.mark.parametrize("nested", [False, True])
13+
@pytest.mark.parametrize("registrations", [1, 2])
14+
@pytest.mark.parametrize("started", [False, True])
15+
def test_adopted_task_can_close_its_supervisor(nested, registrations, started):
16+
code = textwrap.dedent(f"""
17+
import asyncio
18+
from acp.task import TaskSupervisor
19+
20+
async def run():
21+
supervisor = TaskSupervisor(source="reentrant-adapter")
22+
start = asyncio.Event()
23+
finished = asyncio.Event()
24+
async def worker():
25+
await start.wait()
26+
await supervisor.shutdown()
27+
finished.set()
28+
caller = asyncio.create_task(worker(), name="caller")
29+
adapters = []
30+
for _ in range({registrations}):
31+
adapter = supervisor.create(caller)
32+
adapters.append(adapter)
33+
if {nested}:
34+
adapters.append(supervisor.create(adapter))
35+
unrelated = supervisor.create(asyncio.sleep(60))
36+
if {started}:
37+
await asyncio.sleep(0)
38+
start.set()
39+
await asyncio.wait_for(asyncio.gather(caller, *adapters), 1)
40+
assert finished.is_set()
41+
assert not caller.cancelled()
42+
assert unrelated.cancelled()
43+
await asyncio.sleep(0)
44+
assert not supervisor._tasks
45+
assert not supervisor._adopted
46+
await supervisor.shutdown()
47+
48+
asyncio.run(run())
49+
""")
50+
result = subprocess.run( # noqa: S603 - fixed local test program, no external input
51+
[sys.executable, "-c", code], capture_output=True, text=True, timeout=5
52+
)
53+
assert result.returncode == 0, result.stdout + result.stderr
54+
55+
56+
@pytest.mark.parametrize("started", [False, True])
57+
def test_task_observer_can_close_its_connection(started):
58+
code = textwrap.dedent(f"""
59+
import asyncio
60+
from acp._transport import memory_transport_pair
61+
from acp.connection import Connection
62+
63+
async def run():
64+
source, peer = memory_transport_pair()
65+
start = asyncio.Event()
66+
async def handler(method, params, notification):
67+
return None
68+
async def observer_work():
69+
await start.wait()
70+
await connection.close()
71+
task = asyncio.create_task(observer_work())
72+
connection = Connection(handler, source, observers=[lambda event: task], listening=False)
73+
try:
74+
await connection.send_notification("example")
75+
assert (await peer.receive())["method"] == "example"
76+
if {started}:
77+
await asyncio.sleep(0)
78+
start.set()
79+
await asyncio.wait_for(task, 1)
80+
await asyncio.sleep(0)
81+
await asyncio.sleep(0)
82+
assert not task.cancelled()
83+
assert not connection._tasks._tasks
84+
finally:
85+
await connection.close()
86+
await peer.close()
87+
88+
asyncio.run(run())
89+
""")
90+
result = subprocess.run( # noqa: S603 - fixed local test program, no external input
91+
[sys.executable, "-c", code], capture_output=True, text=True, timeout=5
92+
)
93+
assert result.returncode == 0, result.stdout + result.stderr

0 commit comments

Comments
 (0)