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,37 +45,37 @@ 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
3458 asyncio .run (_test ())
3559
36- def test_multiple_get_annotations_in_running_event_loop (self ):
37- # TODO add handling of nested loop
38- async def nested ():
39- sa .attach_items (self .PROJECT_NAME , self .ATTACH_PAYLOAD )
40- annotations = sa .get_annotations (self .PROJECT_NAME )
41- assert len (annotations ) == 4
42-
43- async def create_task_test ():
44- import nest_asyncio
60+ def test_create_task_get_annotations_in_running_event_loop (self ):
61+ async def _test ():
62+ task1 = asyncio .create_task (self .nested ())
63+ task2 = asyncio .create_task (self .nested ())
64+ await task1
65+ await task2
4566
46- nest_asyncio .apply ()
47- task1 = asyncio .create_task (nested ())
48- task2 = asyncio .create_task (nested ())
49- await task1
50- await task2
51-
52- asyncio .run (create_task_test ())
67+ asyncio .run (_test ())
5368
69+ def test_gather_get_annotations_in_running_event_loop (self ):
5470 async def gather_test ():
55- import nest_asyncio
56-
57- nest_asyncio .apply ()
58- await asyncio .gather (nested (), nested ())
71+ await asyncio .gather (self .nested (), self .nested ())
72+ asyncio .run (gather_test ())
5973
74+ def test_gather_async_for (self ):
75+ async def gather_test ():
76+ async for _ in DummyIterator (delay = 0.01 , to = 2 ):
77+ annotations = sa .get_annotations (TestAsyncFunctions .PROJECT_NAME )
78+ assert len (annotations ) == 4
6079 asyncio .run (gather_test ())
6180
6281 def test_upload_annotations_in_running_event_loop (self ):
0 commit comments