File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change 1
1
import asyncio
2
2
3
+ from cancel_token import OperationCancelled
3
4
import pytest
4
5
5
6
from p2p .service import BaseService
@@ -41,6 +42,33 @@ async def test_daemon_exit_causes_parent_cancellation():
41
42
await asyncio .wait_for (service .events .cleaned_up .wait (), timeout = 1 )
42
43
43
44
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
+
44
72
@pytest .mark .asyncio
45
73
async def test_service_tasks_do_not_leak_memory ():
46
74
service = WaitService ()
You can’t perform that action at this time.
0 commit comments