77sa = SAClient ()
88
99
10+ class DummyIterator :
11+ def __init__ (self , delay , to ):
12+ self .delay = delay
13+ self .i = 0
14+ self .to = to
15+
16+ def __aiter__ (self ):
17+ return self
18+
19+ async def __anext__ (self ):
20+ i = self .i
21+ if i >= self .to :
22+ raise StopAsyncIteration
23+ self .i += 1
24+ if i :
25+ await asyncio .sleep (self .delay )
26+ return i
27+
28+
1029class TestAsyncFunctions (TestCase ):
1130 PROJECT_NAME = "TestAsync"
1231 PROJECT_DESCRIPTION = "Desc"
@@ -26,31 +45,35 @@ def setUpClass(cls):
2645 def tearDownClass (cls ):
2746 sa .delete_project (cls .PROJECT_NAME )
2847
48+ @staticmethod
49+ async def nested ():
50+ annotations = sa .get_annotations (TestAsyncFunctions .PROJECT_NAME )
51+ assert len (annotations ) == 4
52+
2953 def test_get_annotations_in_running_event_loop (self ):
3054 async def _test ():
3155 annotations = sa .get_annotations (self .PROJECT_NAME )
3256 assert len (annotations ) == 4
3357 asyncio .run (_test ())
3458
35- def test_multiple_get_annotations_in_running_event_loop (self ):
36- # TODO add handling of nested loop
37- async def nested ():
38- sa .attach_items (self .PROJECT_NAME , self .ATTACH_PAYLOAD )
39- annotations = sa .get_annotations (self .PROJECT_NAME )
40- assert len (annotations ) == 4
41- async def create_task_test ():
42- import nest_asyncio
43- nest_asyncio .apply ()
44- task1 = asyncio .create_task (nested ())
45- task2 = asyncio .create_task (nested ())
59+ def test_create_task_get_annotations_in_running_event_loop (self ):
60+ async def _test ():
61+ task1 = asyncio .create_task (self .nested ())
62+ task2 = asyncio .create_task (self .nested ())
4663 await task1
4764 await task2
48- asyncio .run (create_task_test ())
65+ asyncio .run (_test ())
66+
67+ def test_gather_get_annotations_in_running_event_loop (self ):
68+ async def gather_test ():
69+ await asyncio .gather (self .nested (), self .nested ())
70+ asyncio .run (gather_test ())
4971
72+ def test_gather_async_for (self ):
5073 async def gather_test ():
51- import nest_asyncio
52- nest_asyncio . apply ( )
53- await asyncio . gather ( nested (), nested ())
74+ async for _ in DummyIterator ( delay = 0.01 , to = 2 ):
75+ annotations = sa . get_annotations ( TestAsyncFunctions . PROJECT_NAME )
76+ assert len ( annotations ) == 4
5477 asyncio .run (gather_test ())
5578
5679 def test_upload_annotations_in_running_event_loop (self ):
0 commit comments