Debian Codegraph @

tests/run/asyncio_generators.srctree

raw
1 # mode: run
2 # tag: asyncio, pep492
3
4 """
5 PYTHON setup.py build_ext -i
6 PYTHON test_from_import.py
7 PYTHON test_import.py
8 PYTHON test_async_def.py
9 PYTHON test_async_def_future.py
10 PYTHON test_all.py
11 """
12
13 ######## setup.py ########
14
15 from Cython.Build.Dependencies import cythonize
16 from distutils.core import setup
17
18 setup(
19 ext_modules = cythonize("*.pyx"),
20 )
21
22
23 ######## test_from_import.py ########
24
25 import from_asyncio_import
26 import asyncio
27 from contextlib import closing
28 new_event_loop = asyncio.new_event_loop
29
30 def runloop(task):
31 with closing(new_event_loop()) as loop:
32 result = loop.run_until_complete(task())
33 assert 3 == result, result
34
35
36 ######## test_import.py ########
37
38 import import_asyncio
39 import asyncio
40 from contextlib import closing
41 new_event_loop = asyncio.new_event_loop
42
43 def runloop(task):
44 with closing(new_event_loop()) as loop:
45 result = loop.run_until_complete(task())
46 assert 3 == result, result
47
48
49 ######## test_async_def.py ########
50
51 import async_def
52 import asyncio
53 from contextlib import closing
54
55 def runloop(task):
56 with closing(asyncio.new_event_loop()) as loop:
57 result = loop.run_until_complete(task())
58 assert 3 == result, result
59
60 runloop(async_def.wait3)
61
62
63 ######## test_async_def_future.py ########
64
65 from async_def_future import await_future
66 import asyncio
67 from contextlib import closing
68
69 def runloop():
70 with closing(asyncio.new_event_loop()) as loop:
71 task, events, expected = await_future(loop)
72 result = loop.run_until_complete(task())
73 assert events == expected, 'expected %s, got %s' % (expected, events)
74
75 runloop()
76
77
78 ######## test_all.py ########
79
80 import asyncio
81 from contextlib import closing
82 new_event_loop = asyncio.new_event_loop
83
84 def runloop(task):
85 with closing(new_event_loop()) as loop:
86 result = loop.run_until_complete(task())
87 assert 3 == result, result
88
89 import import_asyncio
90 import from_asyncio_import
91
92 import async_def
93 runloop(async_def.wait3) # 1c)
94
95 runloop(async_def.wait3) # 2c)
96
97 runloop(async_def.wait3) # 3c)
98
99 from collections.abc import Generator
100 assert isinstance(from_asyncio_import.wait3(), Generator)
101 assert isinstance(import_asyncio.wait3(), Generator)
102 assert isinstance((lambda:(yield))(), Generator)
103
104 from collections.abc import Awaitable
105 assert isinstance(async_def.wait3(), Awaitable)
106
107 from collections.abc import Coroutine
108 assert isinstance(async_def.wait3(), Coroutine)
109
110
111 ######## import_asyncio.pyx ########
112 # cython: binding=True
113
114 try:
115 from types import coroutine as types_coroutine
116 except ImportError:
117 types_coroutine = lambda f:f
118
119 import asyncio
120
121 @types_coroutine
122 def wait3():
123 counter = 0
124 for i in range(3):
125 print(counter)
126 yield from asyncio.sleep(0.01)
127 counter += 1
128 return counter
129
130
131 ######## from_asyncio_import.pyx ########
132 # cython: binding=True
133
134 try:
135 from types import coroutine as types_coroutine
136 except ImportError:
137 types_coroutine = lambda f:f
138
139 from asyncio import sleep
140
141 @types_coroutine
142 def wait3():
143 counter = 0
144 for i in range(3):
145 print(counter)
146 yield from sleep(0.01)
147 counter += 1
148 return counter
149
150
151 ######## async_def.pyx ########
152 # cython: binding=True
153
154 import asyncio
155
156 async def wait3():
157 counter = 0
158 for i in range(3):
159 print(counter)
160 await asyncio.sleep(0.01)
161 counter += 1
162 return counter
163
164
165 ######## async_def_future.pyx ########
166 # cython: binding=True
167
168 import asyncio
169
170 def await_future(loop):
171 events = []
172 async def worker():
173 print("worker running")
174 fut = asyncio.Future()
175
176 def setval():
177 print("setval running")
178 events.append('setval')
179 fut.set_result(123)
180 print("setval done")
181
182 events.append('setup')
183 loop.call_later(0.2, setval)
184 events.append(await fut)
185 print("worker done")
186
187 async def test():
188 print("Test running")
189 await worker()
190
191 expected = ['setup', 'setval', 123]
192 return test, events, expected