Skip to content

Commit 6de66c0

Browse files
committed
test cancel during async iterator
1 parent db4d820 commit 6de66c0

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

tests/p2p/test_service.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import asyncio
22

3+
from cancel_token import OperationCancelled
34
import pytest
45

56
from p2p.service import BaseService
@@ -41,6 +42,33 @@ async def test_daemon_exit_causes_parent_cancellation():
4142
await asyncio.wait_for(service.events.cleaned_up.wait(), timeout=1)
4243

4344

45+
@pytest.mark.asyncio
46+
async def test_cancel_exits_async_generator():
47+
service = WaitService()
48+
asyncio.ensure_future(service.run())
49+
50+
async def cancel_soon():
51+
await service.sleep(0.05)
52+
await service.cancel()
53+
54+
asyncio.ensure_future(cancel_soon())
55+
56+
async def async_iterator():
57+
yield 1
58+
await asyncio.sleep(0.5)
59+
assert False, "iterator should have been cancelled by now"
60+
61+
try:
62+
async for val in service.wait_iter(async_iterator()):
63+
assert val == 1
64+
except OperationCancelled:
65+
pass
66+
else:
67+
assert False, "iterator should have been cancelled during iteration"
68+
69+
await service.cancel()
70+
71+
4472
@pytest.mark.asyncio
4573
async def test_service_tasks_do_not_leak_memory():
4674
service = WaitService()

0 commit comments

Comments
 (0)