1 | n/a | import inspect |
---|
2 | n/a | import types |
---|
3 | n/a | import unittest |
---|
4 | n/a | |
---|
5 | n/a | from unittest import mock |
---|
6 | n/a | |
---|
7 | n/a | from test.support import import_module |
---|
8 | n/a | asyncio = import_module("asyncio") |
---|
9 | n/a | |
---|
10 | n/a | |
---|
11 | n/a | class AwaitException(Exception): |
---|
12 | n/a | pass |
---|
13 | n/a | |
---|
14 | n/a | |
---|
15 | n/a | @types.coroutine |
---|
16 | n/a | def awaitable(*, throw=False): |
---|
17 | n/a | if throw: |
---|
18 | n/a | yield ('throw',) |
---|
19 | n/a | else: |
---|
20 | n/a | yield ('result',) |
---|
21 | n/a | |
---|
22 | n/a | |
---|
23 | n/a | def run_until_complete(coro): |
---|
24 | n/a | exc = False |
---|
25 | n/a | while True: |
---|
26 | n/a | try: |
---|
27 | n/a | if exc: |
---|
28 | n/a | exc = False |
---|
29 | n/a | fut = coro.throw(AwaitException) |
---|
30 | n/a | else: |
---|
31 | n/a | fut = coro.send(None) |
---|
32 | n/a | except StopIteration as ex: |
---|
33 | n/a | return ex.args[0] |
---|
34 | n/a | |
---|
35 | n/a | if fut == ('throw',): |
---|
36 | n/a | exc = True |
---|
37 | n/a | |
---|
38 | n/a | |
---|
39 | n/a | def to_list(gen): |
---|
40 | n/a | async def iterate(): |
---|
41 | n/a | res = [] |
---|
42 | n/a | async for i in gen: |
---|
43 | n/a | res.append(i) |
---|
44 | n/a | return res |
---|
45 | n/a | |
---|
46 | n/a | return run_until_complete(iterate()) |
---|
47 | n/a | |
---|
48 | n/a | |
---|
49 | n/a | class AsyncGenSyntaxTest(unittest.TestCase): |
---|
50 | n/a | |
---|
51 | n/a | def test_async_gen_syntax_01(self): |
---|
52 | n/a | code = '''async def foo(): |
---|
53 | n/a | await abc |
---|
54 | n/a | yield from 123 |
---|
55 | n/a | ''' |
---|
56 | n/a | |
---|
57 | n/a | with self.assertRaisesRegex(SyntaxError, 'yield from.*inside async'): |
---|
58 | n/a | exec(code, {}, {}) |
---|
59 | n/a | |
---|
60 | n/a | def test_async_gen_syntax_02(self): |
---|
61 | n/a | code = '''async def foo(): |
---|
62 | n/a | yield from 123 |
---|
63 | n/a | ''' |
---|
64 | n/a | |
---|
65 | n/a | with self.assertRaisesRegex(SyntaxError, 'yield from.*inside async'): |
---|
66 | n/a | exec(code, {}, {}) |
---|
67 | n/a | |
---|
68 | n/a | def test_async_gen_syntax_03(self): |
---|
69 | n/a | code = '''async def foo(): |
---|
70 | n/a | await abc |
---|
71 | n/a | yield |
---|
72 | n/a | return 123 |
---|
73 | n/a | ''' |
---|
74 | n/a | |
---|
75 | n/a | with self.assertRaisesRegex(SyntaxError, 'return.*value.*async gen'): |
---|
76 | n/a | exec(code, {}, {}) |
---|
77 | n/a | |
---|
78 | n/a | def test_async_gen_syntax_04(self): |
---|
79 | n/a | code = '''async def foo(): |
---|
80 | n/a | yield |
---|
81 | n/a | return 123 |
---|
82 | n/a | ''' |
---|
83 | n/a | |
---|
84 | n/a | with self.assertRaisesRegex(SyntaxError, 'return.*value.*async gen'): |
---|
85 | n/a | exec(code, {}, {}) |
---|
86 | n/a | |
---|
87 | n/a | def test_async_gen_syntax_05(self): |
---|
88 | n/a | code = '''async def foo(): |
---|
89 | n/a | if 0: |
---|
90 | n/a | yield |
---|
91 | n/a | return 12 |
---|
92 | n/a | ''' |
---|
93 | n/a | |
---|
94 | n/a | with self.assertRaisesRegex(SyntaxError, 'return.*value.*async gen'): |
---|
95 | n/a | exec(code, {}, {}) |
---|
96 | n/a | |
---|
97 | n/a | |
---|
98 | n/a | class AsyncGenTest(unittest.TestCase): |
---|
99 | n/a | |
---|
100 | n/a | def compare_generators(self, sync_gen, async_gen): |
---|
101 | n/a | def sync_iterate(g): |
---|
102 | n/a | res = [] |
---|
103 | n/a | while True: |
---|
104 | n/a | try: |
---|
105 | n/a | res.append(g.__next__()) |
---|
106 | n/a | except StopIteration: |
---|
107 | n/a | res.append('STOP') |
---|
108 | n/a | break |
---|
109 | n/a | except Exception as ex: |
---|
110 | n/a | res.append(str(type(ex))) |
---|
111 | n/a | return res |
---|
112 | n/a | |
---|
113 | n/a | def async_iterate(g): |
---|
114 | n/a | res = [] |
---|
115 | n/a | while True: |
---|
116 | n/a | try: |
---|
117 | n/a | g.__anext__().__next__() |
---|
118 | n/a | except StopAsyncIteration: |
---|
119 | n/a | res.append('STOP') |
---|
120 | n/a | break |
---|
121 | n/a | except StopIteration as ex: |
---|
122 | n/a | if ex.args: |
---|
123 | n/a | res.append(ex.args[0]) |
---|
124 | n/a | else: |
---|
125 | n/a | res.append('EMPTY StopIteration') |
---|
126 | n/a | break |
---|
127 | n/a | except Exception as ex: |
---|
128 | n/a | res.append(str(type(ex))) |
---|
129 | n/a | return res |
---|
130 | n/a | |
---|
131 | n/a | sync_gen_result = sync_iterate(sync_gen) |
---|
132 | n/a | async_gen_result = async_iterate(async_gen) |
---|
133 | n/a | self.assertEqual(sync_gen_result, async_gen_result) |
---|
134 | n/a | return async_gen_result |
---|
135 | n/a | |
---|
136 | n/a | def test_async_gen_iteration_01(self): |
---|
137 | n/a | async def gen(): |
---|
138 | n/a | await awaitable() |
---|
139 | n/a | a = yield 123 |
---|
140 | n/a | self.assertIs(a, None) |
---|
141 | n/a | await awaitable() |
---|
142 | n/a | yield 456 |
---|
143 | n/a | await awaitable() |
---|
144 | n/a | yield 789 |
---|
145 | n/a | |
---|
146 | n/a | self.assertEqual(to_list(gen()), [123, 456, 789]) |
---|
147 | n/a | |
---|
148 | n/a | def test_async_gen_iteration_02(self): |
---|
149 | n/a | async def gen(): |
---|
150 | n/a | await awaitable() |
---|
151 | n/a | yield 123 |
---|
152 | n/a | await awaitable() |
---|
153 | n/a | |
---|
154 | n/a | g = gen() |
---|
155 | n/a | ai = g.__aiter__() |
---|
156 | n/a | self.assertEqual(ai.__anext__().__next__(), ('result',)) |
---|
157 | n/a | |
---|
158 | n/a | try: |
---|
159 | n/a | ai.__anext__().__next__() |
---|
160 | n/a | except StopIteration as ex: |
---|
161 | n/a | self.assertEqual(ex.args[0], 123) |
---|
162 | n/a | else: |
---|
163 | n/a | self.fail('StopIteration was not raised') |
---|
164 | n/a | |
---|
165 | n/a | self.assertEqual(ai.__anext__().__next__(), ('result',)) |
---|
166 | n/a | |
---|
167 | n/a | try: |
---|
168 | n/a | ai.__anext__().__next__() |
---|
169 | n/a | except StopAsyncIteration as ex: |
---|
170 | n/a | self.assertFalse(ex.args) |
---|
171 | n/a | else: |
---|
172 | n/a | self.fail('StopAsyncIteration was not raised') |
---|
173 | n/a | |
---|
174 | n/a | def test_async_gen_exception_03(self): |
---|
175 | n/a | async def gen(): |
---|
176 | n/a | await awaitable() |
---|
177 | n/a | yield 123 |
---|
178 | n/a | await awaitable(throw=True) |
---|
179 | n/a | yield 456 |
---|
180 | n/a | |
---|
181 | n/a | with self.assertRaises(AwaitException): |
---|
182 | n/a | to_list(gen()) |
---|
183 | n/a | |
---|
184 | n/a | def test_async_gen_exception_04(self): |
---|
185 | n/a | async def gen(): |
---|
186 | n/a | await awaitable() |
---|
187 | n/a | yield 123 |
---|
188 | n/a | 1 / 0 |
---|
189 | n/a | |
---|
190 | n/a | g = gen() |
---|
191 | n/a | ai = g.__aiter__() |
---|
192 | n/a | self.assertEqual(ai.__anext__().__next__(), ('result',)) |
---|
193 | n/a | |
---|
194 | n/a | try: |
---|
195 | n/a | ai.__anext__().__next__() |
---|
196 | n/a | except StopIteration as ex: |
---|
197 | n/a | self.assertEqual(ex.args[0], 123) |
---|
198 | n/a | else: |
---|
199 | n/a | self.fail('StopIteration was not raised') |
---|
200 | n/a | |
---|
201 | n/a | with self.assertRaises(ZeroDivisionError): |
---|
202 | n/a | ai.__anext__().__next__() |
---|
203 | n/a | |
---|
204 | n/a | def test_async_gen_exception_05(self): |
---|
205 | n/a | async def gen(): |
---|
206 | n/a | yield 123 |
---|
207 | n/a | raise StopAsyncIteration |
---|
208 | n/a | |
---|
209 | n/a | with self.assertRaisesRegex(RuntimeError, |
---|
210 | n/a | 'async generator.*StopAsyncIteration'): |
---|
211 | n/a | to_list(gen()) |
---|
212 | n/a | |
---|
213 | n/a | def test_async_gen_exception_06(self): |
---|
214 | n/a | async def gen(): |
---|
215 | n/a | yield 123 |
---|
216 | n/a | raise StopIteration |
---|
217 | n/a | |
---|
218 | n/a | with self.assertRaisesRegex(RuntimeError, |
---|
219 | n/a | 'async generator.*StopIteration'): |
---|
220 | n/a | to_list(gen()) |
---|
221 | n/a | |
---|
222 | n/a | def test_async_gen_exception_07(self): |
---|
223 | n/a | def sync_gen(): |
---|
224 | n/a | try: |
---|
225 | n/a | yield 1 |
---|
226 | n/a | 1 / 0 |
---|
227 | n/a | finally: |
---|
228 | n/a | yield 2 |
---|
229 | n/a | yield 3 |
---|
230 | n/a | |
---|
231 | n/a | yield 100 |
---|
232 | n/a | |
---|
233 | n/a | async def async_gen(): |
---|
234 | n/a | try: |
---|
235 | n/a | yield 1 |
---|
236 | n/a | 1 / 0 |
---|
237 | n/a | finally: |
---|
238 | n/a | yield 2 |
---|
239 | n/a | yield 3 |
---|
240 | n/a | |
---|
241 | n/a | yield 100 |
---|
242 | n/a | |
---|
243 | n/a | self.compare_generators(sync_gen(), async_gen()) |
---|
244 | n/a | |
---|
245 | n/a | def test_async_gen_exception_08(self): |
---|
246 | n/a | def sync_gen(): |
---|
247 | n/a | try: |
---|
248 | n/a | yield 1 |
---|
249 | n/a | finally: |
---|
250 | n/a | yield 2 |
---|
251 | n/a | 1 / 0 |
---|
252 | n/a | yield 3 |
---|
253 | n/a | |
---|
254 | n/a | yield 100 |
---|
255 | n/a | |
---|
256 | n/a | async def async_gen(): |
---|
257 | n/a | try: |
---|
258 | n/a | yield 1 |
---|
259 | n/a | await awaitable() |
---|
260 | n/a | finally: |
---|
261 | n/a | await awaitable() |
---|
262 | n/a | yield 2 |
---|
263 | n/a | 1 / 0 |
---|
264 | n/a | yield 3 |
---|
265 | n/a | |
---|
266 | n/a | yield 100 |
---|
267 | n/a | |
---|
268 | n/a | self.compare_generators(sync_gen(), async_gen()) |
---|
269 | n/a | |
---|
270 | n/a | def test_async_gen_exception_09(self): |
---|
271 | n/a | def sync_gen(): |
---|
272 | n/a | try: |
---|
273 | n/a | yield 1 |
---|
274 | n/a | 1 / 0 |
---|
275 | n/a | finally: |
---|
276 | n/a | yield 2 |
---|
277 | n/a | yield 3 |
---|
278 | n/a | |
---|
279 | n/a | yield 100 |
---|
280 | n/a | |
---|
281 | n/a | async def async_gen(): |
---|
282 | n/a | try: |
---|
283 | n/a | await awaitable() |
---|
284 | n/a | yield 1 |
---|
285 | n/a | 1 / 0 |
---|
286 | n/a | finally: |
---|
287 | n/a | yield 2 |
---|
288 | n/a | await awaitable() |
---|
289 | n/a | yield 3 |
---|
290 | n/a | |
---|
291 | n/a | yield 100 |
---|
292 | n/a | |
---|
293 | n/a | self.compare_generators(sync_gen(), async_gen()) |
---|
294 | n/a | |
---|
295 | n/a | def test_async_gen_exception_10(self): |
---|
296 | n/a | async def gen(): |
---|
297 | n/a | yield 123 |
---|
298 | n/a | with self.assertRaisesRegex(TypeError, |
---|
299 | n/a | "non-None value .* async generator"): |
---|
300 | n/a | gen().__anext__().send(100) |
---|
301 | n/a | |
---|
302 | n/a | def test_async_gen_api_01(self): |
---|
303 | n/a | async def gen(): |
---|
304 | n/a | yield 123 |
---|
305 | n/a | |
---|
306 | n/a | g = gen() |
---|
307 | n/a | |
---|
308 | n/a | self.assertEqual(g.__name__, 'gen') |
---|
309 | n/a | g.__name__ = '123' |
---|
310 | n/a | self.assertEqual(g.__name__, '123') |
---|
311 | n/a | |
---|
312 | n/a | self.assertIn('.gen', g.__qualname__) |
---|
313 | n/a | g.__qualname__ = '123' |
---|
314 | n/a | self.assertEqual(g.__qualname__, '123') |
---|
315 | n/a | |
---|
316 | n/a | self.assertIsNone(g.ag_await) |
---|
317 | n/a | self.assertIsInstance(g.ag_frame, types.FrameType) |
---|
318 | n/a | self.assertFalse(g.ag_running) |
---|
319 | n/a | self.assertIsInstance(g.ag_code, types.CodeType) |
---|
320 | n/a | |
---|
321 | n/a | self.assertTrue(inspect.isawaitable(g.aclose())) |
---|
322 | n/a | |
---|
323 | n/a | |
---|
324 | n/a | class AsyncGenAsyncioTest(unittest.TestCase): |
---|
325 | n/a | |
---|
326 | n/a | def setUp(self): |
---|
327 | n/a | self.loop = asyncio.new_event_loop() |
---|
328 | n/a | asyncio.set_event_loop(None) |
---|
329 | n/a | |
---|
330 | n/a | def tearDown(self): |
---|
331 | n/a | self.loop.close() |
---|
332 | n/a | self.loop = None |
---|
333 | n/a | |
---|
334 | n/a | async def to_list(self, gen): |
---|
335 | n/a | res = [] |
---|
336 | n/a | async for i in gen: |
---|
337 | n/a | res.append(i) |
---|
338 | n/a | return res |
---|
339 | n/a | |
---|
340 | n/a | def test_async_gen_asyncio_01(self): |
---|
341 | n/a | async def gen(): |
---|
342 | n/a | yield 1 |
---|
343 | n/a | await asyncio.sleep(0.01, loop=self.loop) |
---|
344 | n/a | yield 2 |
---|
345 | n/a | await asyncio.sleep(0.01, loop=self.loop) |
---|
346 | n/a | return |
---|
347 | n/a | yield 3 |
---|
348 | n/a | |
---|
349 | n/a | res = self.loop.run_until_complete(self.to_list(gen())) |
---|
350 | n/a | self.assertEqual(res, [1, 2]) |
---|
351 | n/a | |
---|
352 | n/a | def test_async_gen_asyncio_02(self): |
---|
353 | n/a | async def gen(): |
---|
354 | n/a | yield 1 |
---|
355 | n/a | await asyncio.sleep(0.01, loop=self.loop) |
---|
356 | n/a | yield 2 |
---|
357 | n/a | 1 / 0 |
---|
358 | n/a | yield 3 |
---|
359 | n/a | |
---|
360 | n/a | with self.assertRaises(ZeroDivisionError): |
---|
361 | n/a | self.loop.run_until_complete(self.to_list(gen())) |
---|
362 | n/a | |
---|
363 | n/a | def test_async_gen_asyncio_03(self): |
---|
364 | n/a | loop = self.loop |
---|
365 | n/a | |
---|
366 | n/a | class Gen: |
---|
367 | n/a | async def __aiter__(self): |
---|
368 | n/a | yield 1 |
---|
369 | n/a | await asyncio.sleep(0.01, loop=loop) |
---|
370 | n/a | yield 2 |
---|
371 | n/a | |
---|
372 | n/a | res = loop.run_until_complete(self.to_list(Gen())) |
---|
373 | n/a | self.assertEqual(res, [1, 2]) |
---|
374 | n/a | |
---|
375 | n/a | def test_async_gen_asyncio_anext_04(self): |
---|
376 | n/a | async def foo(): |
---|
377 | n/a | yield 1 |
---|
378 | n/a | await asyncio.sleep(0.01, loop=self.loop) |
---|
379 | n/a | try: |
---|
380 | n/a | yield 2 |
---|
381 | n/a | yield 3 |
---|
382 | n/a | except ZeroDivisionError: |
---|
383 | n/a | yield 1000 |
---|
384 | n/a | await asyncio.sleep(0.01, loop=self.loop) |
---|
385 | n/a | yield 4 |
---|
386 | n/a | |
---|
387 | n/a | async def run1(): |
---|
388 | n/a | it = foo().__aiter__() |
---|
389 | n/a | |
---|
390 | n/a | self.assertEqual(await it.__anext__(), 1) |
---|
391 | n/a | self.assertEqual(await it.__anext__(), 2) |
---|
392 | n/a | self.assertEqual(await it.__anext__(), 3) |
---|
393 | n/a | self.assertEqual(await it.__anext__(), 4) |
---|
394 | n/a | with self.assertRaises(StopAsyncIteration): |
---|
395 | n/a | await it.__anext__() |
---|
396 | n/a | with self.assertRaises(StopAsyncIteration): |
---|
397 | n/a | await it.__anext__() |
---|
398 | n/a | |
---|
399 | n/a | async def run2(): |
---|
400 | n/a | it = foo().__aiter__() |
---|
401 | n/a | |
---|
402 | n/a | self.assertEqual(await it.__anext__(), 1) |
---|
403 | n/a | self.assertEqual(await it.__anext__(), 2) |
---|
404 | n/a | try: |
---|
405 | n/a | it.__anext__().throw(ZeroDivisionError) |
---|
406 | n/a | except StopIteration as ex: |
---|
407 | n/a | self.assertEqual(ex.args[0], 1000) |
---|
408 | n/a | else: |
---|
409 | n/a | self.fail('StopIteration was not raised') |
---|
410 | n/a | self.assertEqual(await it.__anext__(), 4) |
---|
411 | n/a | with self.assertRaises(StopAsyncIteration): |
---|
412 | n/a | await it.__anext__() |
---|
413 | n/a | |
---|
414 | n/a | self.loop.run_until_complete(run1()) |
---|
415 | n/a | self.loop.run_until_complete(run2()) |
---|
416 | n/a | |
---|
417 | n/a | def test_async_gen_asyncio_anext_05(self): |
---|
418 | n/a | async def foo(): |
---|
419 | n/a | v = yield 1 |
---|
420 | n/a | v = yield v |
---|
421 | n/a | yield v * 100 |
---|
422 | n/a | |
---|
423 | n/a | async def run(): |
---|
424 | n/a | it = foo().__aiter__() |
---|
425 | n/a | |
---|
426 | n/a | try: |
---|
427 | n/a | it.__anext__().send(None) |
---|
428 | n/a | except StopIteration as ex: |
---|
429 | n/a | self.assertEqual(ex.args[0], 1) |
---|
430 | n/a | else: |
---|
431 | n/a | self.fail('StopIteration was not raised') |
---|
432 | n/a | |
---|
433 | n/a | try: |
---|
434 | n/a | it.__anext__().send(10) |
---|
435 | n/a | except StopIteration as ex: |
---|
436 | n/a | self.assertEqual(ex.args[0], 10) |
---|
437 | n/a | else: |
---|
438 | n/a | self.fail('StopIteration was not raised') |
---|
439 | n/a | |
---|
440 | n/a | try: |
---|
441 | n/a | it.__anext__().send(12) |
---|
442 | n/a | except StopIteration as ex: |
---|
443 | n/a | self.assertEqual(ex.args[0], 1200) |
---|
444 | n/a | else: |
---|
445 | n/a | self.fail('StopIteration was not raised') |
---|
446 | n/a | |
---|
447 | n/a | with self.assertRaises(StopAsyncIteration): |
---|
448 | n/a | await it.__anext__() |
---|
449 | n/a | |
---|
450 | n/a | self.loop.run_until_complete(run()) |
---|
451 | n/a | |
---|
452 | n/a | def test_async_gen_asyncio_anext_06(self): |
---|
453 | n/a | DONE = 0 |
---|
454 | n/a | |
---|
455 | n/a | # test synchronous generators |
---|
456 | n/a | def foo(): |
---|
457 | n/a | try: |
---|
458 | n/a | yield |
---|
459 | n/a | except: |
---|
460 | n/a | pass |
---|
461 | n/a | g = foo() |
---|
462 | n/a | g.send(None) |
---|
463 | n/a | with self.assertRaises(StopIteration): |
---|
464 | n/a | g.send(None) |
---|
465 | n/a | |
---|
466 | n/a | # now with asynchronous generators |
---|
467 | n/a | |
---|
468 | n/a | async def gen(): |
---|
469 | n/a | nonlocal DONE |
---|
470 | n/a | try: |
---|
471 | n/a | yield |
---|
472 | n/a | except: |
---|
473 | n/a | pass |
---|
474 | n/a | DONE = 1 |
---|
475 | n/a | |
---|
476 | n/a | async def run(): |
---|
477 | n/a | nonlocal DONE |
---|
478 | n/a | g = gen() |
---|
479 | n/a | await g.asend(None) |
---|
480 | n/a | with self.assertRaises(StopAsyncIteration): |
---|
481 | n/a | await g.asend(None) |
---|
482 | n/a | DONE += 10 |
---|
483 | n/a | |
---|
484 | n/a | self.loop.run_until_complete(run()) |
---|
485 | n/a | self.assertEqual(DONE, 11) |
---|
486 | n/a | |
---|
487 | n/a | def test_async_gen_asyncio_anext_tuple(self): |
---|
488 | n/a | async def foo(): |
---|
489 | n/a | try: |
---|
490 | n/a | yield (1,) |
---|
491 | n/a | except ZeroDivisionError: |
---|
492 | n/a | yield (2,) |
---|
493 | n/a | |
---|
494 | n/a | async def run(): |
---|
495 | n/a | it = foo().__aiter__() |
---|
496 | n/a | |
---|
497 | n/a | self.assertEqual(await it.__anext__(), (1,)) |
---|
498 | n/a | with self.assertRaises(StopIteration) as cm: |
---|
499 | n/a | it.__anext__().throw(ZeroDivisionError) |
---|
500 | n/a | self.assertEqual(cm.exception.args[0], (2,)) |
---|
501 | n/a | with self.assertRaises(StopAsyncIteration): |
---|
502 | n/a | await it.__anext__() |
---|
503 | n/a | |
---|
504 | n/a | self.loop.run_until_complete(run()) |
---|
505 | n/a | |
---|
506 | n/a | def test_async_gen_asyncio_anext_stopiteration(self): |
---|
507 | n/a | async def foo(): |
---|
508 | n/a | try: |
---|
509 | n/a | yield StopIteration(1) |
---|
510 | n/a | except ZeroDivisionError: |
---|
511 | n/a | yield StopIteration(3) |
---|
512 | n/a | |
---|
513 | n/a | async def run(): |
---|
514 | n/a | it = foo().__aiter__() |
---|
515 | n/a | |
---|
516 | n/a | v = await it.__anext__() |
---|
517 | n/a | self.assertIsInstance(v, StopIteration) |
---|
518 | n/a | self.assertEqual(v.value, 1) |
---|
519 | n/a | with self.assertRaises(StopIteration) as cm: |
---|
520 | n/a | it.__anext__().throw(ZeroDivisionError) |
---|
521 | n/a | v = cm.exception.args[0] |
---|
522 | n/a | self.assertIsInstance(v, StopIteration) |
---|
523 | n/a | self.assertEqual(v.value, 3) |
---|
524 | n/a | with self.assertRaises(StopAsyncIteration): |
---|
525 | n/a | await it.__anext__() |
---|
526 | n/a | |
---|
527 | n/a | self.loop.run_until_complete(run()) |
---|
528 | n/a | |
---|
529 | n/a | def test_async_gen_asyncio_aclose_06(self): |
---|
530 | n/a | async def foo(): |
---|
531 | n/a | try: |
---|
532 | n/a | yield 1 |
---|
533 | n/a | 1 / 0 |
---|
534 | n/a | finally: |
---|
535 | n/a | await asyncio.sleep(0.01, loop=self.loop) |
---|
536 | n/a | yield 12 |
---|
537 | n/a | |
---|
538 | n/a | async def run(): |
---|
539 | n/a | gen = foo() |
---|
540 | n/a | it = gen.__aiter__() |
---|
541 | n/a | await it.__anext__() |
---|
542 | n/a | await gen.aclose() |
---|
543 | n/a | |
---|
544 | n/a | with self.assertRaisesRegex( |
---|
545 | n/a | RuntimeError, |
---|
546 | n/a | "async generator ignored GeneratorExit"): |
---|
547 | n/a | self.loop.run_until_complete(run()) |
---|
548 | n/a | |
---|
549 | n/a | def test_async_gen_asyncio_aclose_07(self): |
---|
550 | n/a | DONE = 0 |
---|
551 | n/a | |
---|
552 | n/a | async def foo(): |
---|
553 | n/a | nonlocal DONE |
---|
554 | n/a | try: |
---|
555 | n/a | yield 1 |
---|
556 | n/a | 1 / 0 |
---|
557 | n/a | finally: |
---|
558 | n/a | await asyncio.sleep(0.01, loop=self.loop) |
---|
559 | n/a | await asyncio.sleep(0.01, loop=self.loop) |
---|
560 | n/a | DONE += 1 |
---|
561 | n/a | DONE += 1000 |
---|
562 | n/a | |
---|
563 | n/a | async def run(): |
---|
564 | n/a | gen = foo() |
---|
565 | n/a | it = gen.__aiter__() |
---|
566 | n/a | await it.__anext__() |
---|
567 | n/a | await gen.aclose() |
---|
568 | n/a | |
---|
569 | n/a | self.loop.run_until_complete(run()) |
---|
570 | n/a | self.assertEqual(DONE, 1) |
---|
571 | n/a | |
---|
572 | n/a | def test_async_gen_asyncio_aclose_08(self): |
---|
573 | n/a | DONE = 0 |
---|
574 | n/a | |
---|
575 | n/a | fut = asyncio.Future(loop=self.loop) |
---|
576 | n/a | |
---|
577 | n/a | async def foo(): |
---|
578 | n/a | nonlocal DONE |
---|
579 | n/a | try: |
---|
580 | n/a | yield 1 |
---|
581 | n/a | await fut |
---|
582 | n/a | DONE += 1000 |
---|
583 | n/a | yield 2 |
---|
584 | n/a | finally: |
---|
585 | n/a | await asyncio.sleep(0.01, loop=self.loop) |
---|
586 | n/a | await asyncio.sleep(0.01, loop=self.loop) |
---|
587 | n/a | DONE += 1 |
---|
588 | n/a | DONE += 1000 |
---|
589 | n/a | |
---|
590 | n/a | async def run(): |
---|
591 | n/a | gen = foo() |
---|
592 | n/a | it = gen.__aiter__() |
---|
593 | n/a | self.assertEqual(await it.__anext__(), 1) |
---|
594 | n/a | t = self.loop.create_task(it.__anext__()) |
---|
595 | n/a | await asyncio.sleep(0.01, loop=self.loop) |
---|
596 | n/a | await gen.aclose() |
---|
597 | n/a | return t |
---|
598 | n/a | |
---|
599 | n/a | t = self.loop.run_until_complete(run()) |
---|
600 | n/a | self.assertEqual(DONE, 1) |
---|
601 | n/a | |
---|
602 | n/a | # Silence ResourceWarnings |
---|
603 | n/a | fut.cancel() |
---|
604 | n/a | t.cancel() |
---|
605 | n/a | self.loop.run_until_complete(asyncio.sleep(0.01, loop=self.loop)) |
---|
606 | n/a | |
---|
607 | n/a | def test_async_gen_asyncio_gc_aclose_09(self): |
---|
608 | n/a | DONE = 0 |
---|
609 | n/a | |
---|
610 | n/a | async def gen(): |
---|
611 | n/a | nonlocal DONE |
---|
612 | n/a | try: |
---|
613 | n/a | while True: |
---|
614 | n/a | yield 1 |
---|
615 | n/a | finally: |
---|
616 | n/a | await asyncio.sleep(0.01, loop=self.loop) |
---|
617 | n/a | await asyncio.sleep(0.01, loop=self.loop) |
---|
618 | n/a | DONE = 1 |
---|
619 | n/a | |
---|
620 | n/a | async def run(): |
---|
621 | n/a | g = gen() |
---|
622 | n/a | await g.__anext__() |
---|
623 | n/a | await g.__anext__() |
---|
624 | n/a | del g |
---|
625 | n/a | |
---|
626 | n/a | await asyncio.sleep(0.1, loop=self.loop) |
---|
627 | n/a | |
---|
628 | n/a | self.loop.run_until_complete(run()) |
---|
629 | n/a | self.assertEqual(DONE, 1) |
---|
630 | n/a | |
---|
631 | n/a | def test_async_gen_asyncio_aclose_10(self): |
---|
632 | n/a | DONE = 0 |
---|
633 | n/a | |
---|
634 | n/a | # test synchronous generators |
---|
635 | n/a | def foo(): |
---|
636 | n/a | try: |
---|
637 | n/a | yield |
---|
638 | n/a | except: |
---|
639 | n/a | pass |
---|
640 | n/a | g = foo() |
---|
641 | n/a | g.send(None) |
---|
642 | n/a | g.close() |
---|
643 | n/a | |
---|
644 | n/a | # now with asynchronous generators |
---|
645 | n/a | |
---|
646 | n/a | async def gen(): |
---|
647 | n/a | nonlocal DONE |
---|
648 | n/a | try: |
---|
649 | n/a | yield |
---|
650 | n/a | except: |
---|
651 | n/a | pass |
---|
652 | n/a | DONE = 1 |
---|
653 | n/a | |
---|
654 | n/a | async def run(): |
---|
655 | n/a | nonlocal DONE |
---|
656 | n/a | g = gen() |
---|
657 | n/a | await g.asend(None) |
---|
658 | n/a | await g.aclose() |
---|
659 | n/a | DONE += 10 |
---|
660 | n/a | |
---|
661 | n/a | self.loop.run_until_complete(run()) |
---|
662 | n/a | self.assertEqual(DONE, 11) |
---|
663 | n/a | |
---|
664 | n/a | def test_async_gen_asyncio_aclose_11(self): |
---|
665 | n/a | DONE = 0 |
---|
666 | n/a | |
---|
667 | n/a | # test synchronous generators |
---|
668 | n/a | def foo(): |
---|
669 | n/a | try: |
---|
670 | n/a | yield |
---|
671 | n/a | except: |
---|
672 | n/a | pass |
---|
673 | n/a | yield |
---|
674 | n/a | g = foo() |
---|
675 | n/a | g.send(None) |
---|
676 | n/a | with self.assertRaisesRegex(RuntimeError, 'ignored GeneratorExit'): |
---|
677 | n/a | g.close() |
---|
678 | n/a | |
---|
679 | n/a | # now with asynchronous generators |
---|
680 | n/a | |
---|
681 | n/a | async def gen(): |
---|
682 | n/a | nonlocal DONE |
---|
683 | n/a | try: |
---|
684 | n/a | yield |
---|
685 | n/a | except: |
---|
686 | n/a | pass |
---|
687 | n/a | yield |
---|
688 | n/a | DONE += 1 |
---|
689 | n/a | |
---|
690 | n/a | async def run(): |
---|
691 | n/a | nonlocal DONE |
---|
692 | n/a | g = gen() |
---|
693 | n/a | await g.asend(None) |
---|
694 | n/a | with self.assertRaisesRegex(RuntimeError, 'ignored GeneratorExit'): |
---|
695 | n/a | await g.aclose() |
---|
696 | n/a | DONE += 10 |
---|
697 | n/a | |
---|
698 | n/a | self.loop.run_until_complete(run()) |
---|
699 | n/a | self.assertEqual(DONE, 10) |
---|
700 | n/a | |
---|
701 | n/a | def test_async_gen_asyncio_asend_01(self): |
---|
702 | n/a | DONE = 0 |
---|
703 | n/a | |
---|
704 | n/a | # Sanity check: |
---|
705 | n/a | def sgen(): |
---|
706 | n/a | v = yield 1 |
---|
707 | n/a | yield v * 2 |
---|
708 | n/a | sg = sgen() |
---|
709 | n/a | v = sg.send(None) |
---|
710 | n/a | self.assertEqual(v, 1) |
---|
711 | n/a | v = sg.send(100) |
---|
712 | n/a | self.assertEqual(v, 200) |
---|
713 | n/a | |
---|
714 | n/a | async def gen(): |
---|
715 | n/a | nonlocal DONE |
---|
716 | n/a | try: |
---|
717 | n/a | await asyncio.sleep(0.01, loop=self.loop) |
---|
718 | n/a | v = yield 1 |
---|
719 | n/a | await asyncio.sleep(0.01, loop=self.loop) |
---|
720 | n/a | yield v * 2 |
---|
721 | n/a | await asyncio.sleep(0.01, loop=self.loop) |
---|
722 | n/a | return |
---|
723 | n/a | finally: |
---|
724 | n/a | await asyncio.sleep(0.01, loop=self.loop) |
---|
725 | n/a | await asyncio.sleep(0.01, loop=self.loop) |
---|
726 | n/a | DONE = 1 |
---|
727 | n/a | |
---|
728 | n/a | async def run(): |
---|
729 | n/a | g = gen() |
---|
730 | n/a | |
---|
731 | n/a | v = await g.asend(None) |
---|
732 | n/a | self.assertEqual(v, 1) |
---|
733 | n/a | |
---|
734 | n/a | v = await g.asend(100) |
---|
735 | n/a | self.assertEqual(v, 200) |
---|
736 | n/a | |
---|
737 | n/a | with self.assertRaises(StopAsyncIteration): |
---|
738 | n/a | await g.asend(None) |
---|
739 | n/a | |
---|
740 | n/a | self.loop.run_until_complete(run()) |
---|
741 | n/a | self.assertEqual(DONE, 1) |
---|
742 | n/a | |
---|
743 | n/a | def test_async_gen_asyncio_asend_02(self): |
---|
744 | n/a | DONE = 0 |
---|
745 | n/a | |
---|
746 | n/a | async def sleep_n_crash(delay): |
---|
747 | n/a | await asyncio.sleep(delay, loop=self.loop) |
---|
748 | n/a | 1 / 0 |
---|
749 | n/a | |
---|
750 | n/a | async def gen(): |
---|
751 | n/a | nonlocal DONE |
---|
752 | n/a | try: |
---|
753 | n/a | await asyncio.sleep(0.01, loop=self.loop) |
---|
754 | n/a | v = yield 1 |
---|
755 | n/a | await sleep_n_crash(0.01) |
---|
756 | n/a | DONE += 1000 |
---|
757 | n/a | yield v * 2 |
---|
758 | n/a | finally: |
---|
759 | n/a | await asyncio.sleep(0.01, loop=self.loop) |
---|
760 | n/a | await asyncio.sleep(0.01, loop=self.loop) |
---|
761 | n/a | DONE = 1 |
---|
762 | n/a | |
---|
763 | n/a | async def run(): |
---|
764 | n/a | g = gen() |
---|
765 | n/a | |
---|
766 | n/a | v = await g.asend(None) |
---|
767 | n/a | self.assertEqual(v, 1) |
---|
768 | n/a | |
---|
769 | n/a | await g.asend(100) |
---|
770 | n/a | |
---|
771 | n/a | with self.assertRaises(ZeroDivisionError): |
---|
772 | n/a | self.loop.run_until_complete(run()) |
---|
773 | n/a | self.assertEqual(DONE, 1) |
---|
774 | n/a | |
---|
775 | n/a | def test_async_gen_asyncio_asend_03(self): |
---|
776 | n/a | DONE = 0 |
---|
777 | n/a | |
---|
778 | n/a | async def sleep_n_crash(delay): |
---|
779 | n/a | fut = asyncio.ensure_future(asyncio.sleep(delay, loop=self.loop), |
---|
780 | n/a | loop=self.loop) |
---|
781 | n/a | self.loop.call_later(delay / 2, lambda: fut.cancel()) |
---|
782 | n/a | return await fut |
---|
783 | n/a | |
---|
784 | n/a | async def gen(): |
---|
785 | n/a | nonlocal DONE |
---|
786 | n/a | try: |
---|
787 | n/a | await asyncio.sleep(0.01, loop=self.loop) |
---|
788 | n/a | v = yield 1 |
---|
789 | n/a | await sleep_n_crash(0.01) |
---|
790 | n/a | DONE += 1000 |
---|
791 | n/a | yield v * 2 |
---|
792 | n/a | finally: |
---|
793 | n/a | await asyncio.sleep(0.01, loop=self.loop) |
---|
794 | n/a | await asyncio.sleep(0.01, loop=self.loop) |
---|
795 | n/a | DONE = 1 |
---|
796 | n/a | |
---|
797 | n/a | async def run(): |
---|
798 | n/a | g = gen() |
---|
799 | n/a | |
---|
800 | n/a | v = await g.asend(None) |
---|
801 | n/a | self.assertEqual(v, 1) |
---|
802 | n/a | |
---|
803 | n/a | await g.asend(100) |
---|
804 | n/a | |
---|
805 | n/a | with self.assertRaises(asyncio.CancelledError): |
---|
806 | n/a | self.loop.run_until_complete(run()) |
---|
807 | n/a | self.assertEqual(DONE, 1) |
---|
808 | n/a | |
---|
809 | n/a | def test_async_gen_asyncio_athrow_01(self): |
---|
810 | n/a | DONE = 0 |
---|
811 | n/a | |
---|
812 | n/a | class FooEr(Exception): |
---|
813 | n/a | pass |
---|
814 | n/a | |
---|
815 | n/a | # Sanity check: |
---|
816 | n/a | def sgen(): |
---|
817 | n/a | try: |
---|
818 | n/a | v = yield 1 |
---|
819 | n/a | except FooEr: |
---|
820 | n/a | v = 1000 |
---|
821 | n/a | yield v * 2 |
---|
822 | n/a | sg = sgen() |
---|
823 | n/a | v = sg.send(None) |
---|
824 | n/a | self.assertEqual(v, 1) |
---|
825 | n/a | v = sg.throw(FooEr) |
---|
826 | n/a | self.assertEqual(v, 2000) |
---|
827 | n/a | with self.assertRaises(StopIteration): |
---|
828 | n/a | sg.send(None) |
---|
829 | n/a | |
---|
830 | n/a | async def gen(): |
---|
831 | n/a | nonlocal DONE |
---|
832 | n/a | try: |
---|
833 | n/a | await asyncio.sleep(0.01, loop=self.loop) |
---|
834 | n/a | try: |
---|
835 | n/a | v = yield 1 |
---|
836 | n/a | except FooEr: |
---|
837 | n/a | v = 1000 |
---|
838 | n/a | await asyncio.sleep(0.01, loop=self.loop) |
---|
839 | n/a | yield v * 2 |
---|
840 | n/a | await asyncio.sleep(0.01, loop=self.loop) |
---|
841 | n/a | # return |
---|
842 | n/a | finally: |
---|
843 | n/a | await asyncio.sleep(0.01, loop=self.loop) |
---|
844 | n/a | await asyncio.sleep(0.01, loop=self.loop) |
---|
845 | n/a | DONE = 1 |
---|
846 | n/a | |
---|
847 | n/a | async def run(): |
---|
848 | n/a | g = gen() |
---|
849 | n/a | |
---|
850 | n/a | v = await g.asend(None) |
---|
851 | n/a | self.assertEqual(v, 1) |
---|
852 | n/a | |
---|
853 | n/a | v = await g.athrow(FooEr) |
---|
854 | n/a | self.assertEqual(v, 2000) |
---|
855 | n/a | |
---|
856 | n/a | with self.assertRaises(StopAsyncIteration): |
---|
857 | n/a | await g.asend(None) |
---|
858 | n/a | |
---|
859 | n/a | self.loop.run_until_complete(run()) |
---|
860 | n/a | self.assertEqual(DONE, 1) |
---|
861 | n/a | |
---|
862 | n/a | def test_async_gen_asyncio_athrow_02(self): |
---|
863 | n/a | DONE = 0 |
---|
864 | n/a | |
---|
865 | n/a | class FooEr(Exception): |
---|
866 | n/a | pass |
---|
867 | n/a | |
---|
868 | n/a | async def sleep_n_crash(delay): |
---|
869 | n/a | fut = asyncio.ensure_future(asyncio.sleep(delay, loop=self.loop), |
---|
870 | n/a | loop=self.loop) |
---|
871 | n/a | self.loop.call_later(delay / 2, lambda: fut.cancel()) |
---|
872 | n/a | return await fut |
---|
873 | n/a | |
---|
874 | n/a | async def gen(): |
---|
875 | n/a | nonlocal DONE |
---|
876 | n/a | try: |
---|
877 | n/a | await asyncio.sleep(0.01, loop=self.loop) |
---|
878 | n/a | try: |
---|
879 | n/a | v = yield 1 |
---|
880 | n/a | except FooEr: |
---|
881 | n/a | await sleep_n_crash(0.01) |
---|
882 | n/a | yield v * 2 |
---|
883 | n/a | await asyncio.sleep(0.01, loop=self.loop) |
---|
884 | n/a | # return |
---|
885 | n/a | finally: |
---|
886 | n/a | await asyncio.sleep(0.01, loop=self.loop) |
---|
887 | n/a | await asyncio.sleep(0.01, loop=self.loop) |
---|
888 | n/a | DONE = 1 |
---|
889 | n/a | |
---|
890 | n/a | async def run(): |
---|
891 | n/a | g = gen() |
---|
892 | n/a | |
---|
893 | n/a | v = await g.asend(None) |
---|
894 | n/a | self.assertEqual(v, 1) |
---|
895 | n/a | |
---|
896 | n/a | try: |
---|
897 | n/a | await g.athrow(FooEr) |
---|
898 | n/a | except asyncio.CancelledError: |
---|
899 | n/a | self.assertEqual(DONE, 1) |
---|
900 | n/a | raise |
---|
901 | n/a | else: |
---|
902 | n/a | self.fail('CancelledError was not raised') |
---|
903 | n/a | |
---|
904 | n/a | with self.assertRaises(asyncio.CancelledError): |
---|
905 | n/a | self.loop.run_until_complete(run()) |
---|
906 | n/a | self.assertEqual(DONE, 1) |
---|
907 | n/a | |
---|
908 | n/a | def test_async_gen_asyncio_athrow_03(self): |
---|
909 | n/a | DONE = 0 |
---|
910 | n/a | |
---|
911 | n/a | # test synchronous generators |
---|
912 | n/a | def foo(): |
---|
913 | n/a | try: |
---|
914 | n/a | yield |
---|
915 | n/a | except: |
---|
916 | n/a | pass |
---|
917 | n/a | g = foo() |
---|
918 | n/a | g.send(None) |
---|
919 | n/a | with self.assertRaises(StopIteration): |
---|
920 | n/a | g.throw(ValueError) |
---|
921 | n/a | |
---|
922 | n/a | # now with asynchronous generators |
---|
923 | n/a | |
---|
924 | n/a | async def gen(): |
---|
925 | n/a | nonlocal DONE |
---|
926 | n/a | try: |
---|
927 | n/a | yield |
---|
928 | n/a | except: |
---|
929 | n/a | pass |
---|
930 | n/a | DONE = 1 |
---|
931 | n/a | |
---|
932 | n/a | async def run(): |
---|
933 | n/a | nonlocal DONE |
---|
934 | n/a | g = gen() |
---|
935 | n/a | await g.asend(None) |
---|
936 | n/a | with self.assertRaises(StopAsyncIteration): |
---|
937 | n/a | await g.athrow(ValueError) |
---|
938 | n/a | DONE += 10 |
---|
939 | n/a | |
---|
940 | n/a | self.loop.run_until_complete(run()) |
---|
941 | n/a | self.assertEqual(DONE, 11) |
---|
942 | n/a | |
---|
943 | n/a | def test_async_gen_asyncio_athrow_tuple(self): |
---|
944 | n/a | async def gen(): |
---|
945 | n/a | try: |
---|
946 | n/a | yield 1 |
---|
947 | n/a | except ZeroDivisionError: |
---|
948 | n/a | yield (2,) |
---|
949 | n/a | |
---|
950 | n/a | async def run(): |
---|
951 | n/a | g = gen() |
---|
952 | n/a | v = await g.asend(None) |
---|
953 | n/a | self.assertEqual(v, 1) |
---|
954 | n/a | v = await g.athrow(ZeroDivisionError) |
---|
955 | n/a | self.assertEqual(v, (2,)) |
---|
956 | n/a | with self.assertRaises(StopAsyncIteration): |
---|
957 | n/a | await g.asend(None) |
---|
958 | n/a | |
---|
959 | n/a | self.loop.run_until_complete(run()) |
---|
960 | n/a | |
---|
961 | n/a | def test_async_gen_asyncio_athrow_stopiteration(self): |
---|
962 | n/a | async def gen(): |
---|
963 | n/a | try: |
---|
964 | n/a | yield 1 |
---|
965 | n/a | except ZeroDivisionError: |
---|
966 | n/a | yield StopIteration(2) |
---|
967 | n/a | |
---|
968 | n/a | async def run(): |
---|
969 | n/a | g = gen() |
---|
970 | n/a | v = await g.asend(None) |
---|
971 | n/a | self.assertEqual(v, 1) |
---|
972 | n/a | v = await g.athrow(ZeroDivisionError) |
---|
973 | n/a | self.assertIsInstance(v, StopIteration) |
---|
974 | n/a | self.assertEqual(v.value, 2) |
---|
975 | n/a | with self.assertRaises(StopAsyncIteration): |
---|
976 | n/a | await g.asend(None) |
---|
977 | n/a | |
---|
978 | n/a | self.loop.run_until_complete(run()) |
---|
979 | n/a | |
---|
980 | n/a | def test_async_gen_asyncio_shutdown_01(self): |
---|
981 | n/a | finalized = 0 |
---|
982 | n/a | |
---|
983 | n/a | async def waiter(timeout): |
---|
984 | n/a | nonlocal finalized |
---|
985 | n/a | try: |
---|
986 | n/a | await asyncio.sleep(timeout, loop=self.loop) |
---|
987 | n/a | yield 1 |
---|
988 | n/a | finally: |
---|
989 | n/a | await asyncio.sleep(0, loop=self.loop) |
---|
990 | n/a | finalized += 1 |
---|
991 | n/a | |
---|
992 | n/a | async def wait(): |
---|
993 | n/a | async for _ in waiter(1): |
---|
994 | n/a | pass |
---|
995 | n/a | |
---|
996 | n/a | t1 = self.loop.create_task(wait()) |
---|
997 | n/a | t2 = self.loop.create_task(wait()) |
---|
998 | n/a | |
---|
999 | n/a | self.loop.run_until_complete(asyncio.sleep(0.1, loop=self.loop)) |
---|
1000 | n/a | |
---|
1001 | n/a | self.loop.run_until_complete(self.loop.shutdown_asyncgens()) |
---|
1002 | n/a | self.assertEqual(finalized, 2) |
---|
1003 | n/a | |
---|
1004 | n/a | # Silence warnings |
---|
1005 | n/a | t1.cancel() |
---|
1006 | n/a | t2.cancel() |
---|
1007 | n/a | self.loop.run_until_complete(asyncio.sleep(0.1, loop=self.loop)) |
---|
1008 | n/a | |
---|
1009 | n/a | def test_async_gen_asyncio_shutdown_02(self): |
---|
1010 | n/a | logged = 0 |
---|
1011 | n/a | |
---|
1012 | n/a | def logger(loop, context): |
---|
1013 | n/a | nonlocal logged |
---|
1014 | n/a | self.assertIn('asyncgen', context) |
---|
1015 | n/a | expected = 'an error occurred during closing of asynchronous' |
---|
1016 | n/a | if expected in context['message']: |
---|
1017 | n/a | logged += 1 |
---|
1018 | n/a | |
---|
1019 | n/a | async def waiter(timeout): |
---|
1020 | n/a | try: |
---|
1021 | n/a | await asyncio.sleep(timeout, loop=self.loop) |
---|
1022 | n/a | yield 1 |
---|
1023 | n/a | finally: |
---|
1024 | n/a | 1 / 0 |
---|
1025 | n/a | |
---|
1026 | n/a | async def wait(): |
---|
1027 | n/a | async for _ in waiter(1): |
---|
1028 | n/a | pass |
---|
1029 | n/a | |
---|
1030 | n/a | t = self.loop.create_task(wait()) |
---|
1031 | n/a | self.loop.run_until_complete(asyncio.sleep(0.1, loop=self.loop)) |
---|
1032 | n/a | |
---|
1033 | n/a | self.loop.set_exception_handler(logger) |
---|
1034 | n/a | self.loop.run_until_complete(self.loop.shutdown_asyncgens()) |
---|
1035 | n/a | |
---|
1036 | n/a | self.assertEqual(logged, 1) |
---|
1037 | n/a | |
---|
1038 | n/a | # Silence warnings |
---|
1039 | n/a | t.cancel() |
---|
1040 | n/a | self.loop.run_until_complete(asyncio.sleep(0.1, loop=self.loop)) |
---|
1041 | n/a | |
---|
1042 | n/a | if __name__ == "__main__": |
---|
1043 | n/a | unittest.main() |
---|