1 | n/a | """Tests for tasks.py.""" |
---|
2 | n/a | |
---|
3 | n/a | import collections |
---|
4 | n/a | import contextlib |
---|
5 | n/a | import functools |
---|
6 | n/a | import io |
---|
7 | n/a | import os |
---|
8 | n/a | import re |
---|
9 | n/a | import sys |
---|
10 | n/a | import types |
---|
11 | n/a | import unittest |
---|
12 | n/a | import weakref |
---|
13 | n/a | from unittest import mock |
---|
14 | n/a | |
---|
15 | n/a | import asyncio |
---|
16 | n/a | from asyncio import coroutines |
---|
17 | n/a | from asyncio import futures |
---|
18 | n/a | from asyncio import tasks |
---|
19 | n/a | from asyncio import test_utils |
---|
20 | n/a | try: |
---|
21 | n/a | from test import support |
---|
22 | n/a | except ImportError: |
---|
23 | n/a | from asyncio import test_support as support |
---|
24 | n/a | try: |
---|
25 | n/a | from test.support.script_helper import assert_python_ok |
---|
26 | n/a | except ImportError: |
---|
27 | n/a | try: |
---|
28 | n/a | from test.script_helper import assert_python_ok |
---|
29 | n/a | except ImportError: |
---|
30 | n/a | from asyncio.test_support import assert_python_ok |
---|
31 | n/a | |
---|
32 | n/a | |
---|
33 | n/a | PY34 = (sys.version_info >= (3, 4)) |
---|
34 | n/a | PY35 = (sys.version_info >= (3, 5)) |
---|
35 | n/a | |
---|
36 | n/a | |
---|
37 | n/a | @asyncio.coroutine |
---|
38 | n/a | def coroutine_function(): |
---|
39 | n/a | pass |
---|
40 | n/a | |
---|
41 | n/a | |
---|
42 | n/a | @contextlib.contextmanager |
---|
43 | n/a | def set_coroutine_debug(enabled): |
---|
44 | n/a | coroutines = asyncio.coroutines |
---|
45 | n/a | |
---|
46 | n/a | old_debug = coroutines._DEBUG |
---|
47 | n/a | try: |
---|
48 | n/a | coroutines._DEBUG = enabled |
---|
49 | n/a | yield |
---|
50 | n/a | finally: |
---|
51 | n/a | coroutines._DEBUG = old_debug |
---|
52 | n/a | |
---|
53 | n/a | |
---|
54 | n/a | |
---|
55 | n/a | def format_coroutine(qualname, state, src, source_traceback, generator=False): |
---|
56 | n/a | if generator: |
---|
57 | n/a | state = '%s' % state |
---|
58 | n/a | else: |
---|
59 | n/a | state = '%s, defined' % state |
---|
60 | n/a | if source_traceback is not None: |
---|
61 | n/a | frame = source_traceback[-1] |
---|
62 | n/a | return ('coro=<%s() %s at %s> created at %s:%s' |
---|
63 | n/a | % (qualname, state, src, frame[0], frame[1])) |
---|
64 | n/a | else: |
---|
65 | n/a | return 'coro=<%s() %s at %s>' % (qualname, state, src) |
---|
66 | n/a | |
---|
67 | n/a | |
---|
68 | n/a | class Dummy: |
---|
69 | n/a | |
---|
70 | n/a | def __repr__(self): |
---|
71 | n/a | return '<Dummy>' |
---|
72 | n/a | |
---|
73 | n/a | def __call__(self, *args): |
---|
74 | n/a | pass |
---|
75 | n/a | |
---|
76 | n/a | |
---|
77 | n/a | class BaseTaskTests: |
---|
78 | n/a | |
---|
79 | n/a | Task = None |
---|
80 | n/a | Future = None |
---|
81 | n/a | |
---|
82 | n/a | def new_task(self, loop, coro): |
---|
83 | n/a | return self.__class__.Task(coro, loop=loop) |
---|
84 | n/a | |
---|
85 | n/a | def new_future(self, loop): |
---|
86 | n/a | return self.__class__.Future(loop=loop) |
---|
87 | n/a | |
---|
88 | n/a | def setUp(self): |
---|
89 | n/a | super().setUp() |
---|
90 | n/a | self.loop = self.new_test_loop() |
---|
91 | n/a | self.loop.set_task_factory(self.new_task) |
---|
92 | n/a | self.loop.create_future = lambda: self.new_future(self.loop) |
---|
93 | n/a | |
---|
94 | n/a | def test_other_loop_future(self): |
---|
95 | n/a | other_loop = asyncio.new_event_loop() |
---|
96 | n/a | fut = self.new_future(other_loop) |
---|
97 | n/a | |
---|
98 | n/a | @asyncio.coroutine |
---|
99 | n/a | def run(fut): |
---|
100 | n/a | yield from fut |
---|
101 | n/a | |
---|
102 | n/a | try: |
---|
103 | n/a | with self.assertRaisesRegex(RuntimeError, |
---|
104 | n/a | r'Task .* got Future .* attached'): |
---|
105 | n/a | self.loop.run_until_complete(run(fut)) |
---|
106 | n/a | finally: |
---|
107 | n/a | other_loop.close() |
---|
108 | n/a | |
---|
109 | n/a | def test_task_awaits_on_itself(self): |
---|
110 | n/a | @asyncio.coroutine |
---|
111 | n/a | def test(): |
---|
112 | n/a | yield from task |
---|
113 | n/a | |
---|
114 | n/a | task = asyncio.ensure_future(test(), loop=self.loop) |
---|
115 | n/a | |
---|
116 | n/a | with self.assertRaisesRegex(RuntimeError, |
---|
117 | n/a | 'Task cannot await on itself'): |
---|
118 | n/a | self.loop.run_until_complete(task) |
---|
119 | n/a | |
---|
120 | n/a | def test_task_class(self): |
---|
121 | n/a | @asyncio.coroutine |
---|
122 | n/a | def notmuch(): |
---|
123 | n/a | return 'ok' |
---|
124 | n/a | t = self.new_task(self.loop, notmuch()) |
---|
125 | n/a | self.loop.run_until_complete(t) |
---|
126 | n/a | self.assertTrue(t.done()) |
---|
127 | n/a | self.assertEqual(t.result(), 'ok') |
---|
128 | n/a | self.assertIs(t._loop, self.loop) |
---|
129 | n/a | |
---|
130 | n/a | loop = asyncio.new_event_loop() |
---|
131 | n/a | self.set_event_loop(loop) |
---|
132 | n/a | t = self.new_task(loop, notmuch()) |
---|
133 | n/a | self.assertIs(t._loop, loop) |
---|
134 | n/a | loop.run_until_complete(t) |
---|
135 | n/a | loop.close() |
---|
136 | n/a | |
---|
137 | n/a | def test_ensure_future_coroutine(self): |
---|
138 | n/a | @asyncio.coroutine |
---|
139 | n/a | def notmuch(): |
---|
140 | n/a | return 'ok' |
---|
141 | n/a | t = asyncio.ensure_future(notmuch(), loop=self.loop) |
---|
142 | n/a | self.loop.run_until_complete(t) |
---|
143 | n/a | self.assertTrue(t.done()) |
---|
144 | n/a | self.assertEqual(t.result(), 'ok') |
---|
145 | n/a | self.assertIs(t._loop, self.loop) |
---|
146 | n/a | |
---|
147 | n/a | loop = asyncio.new_event_loop() |
---|
148 | n/a | self.set_event_loop(loop) |
---|
149 | n/a | t = asyncio.ensure_future(notmuch(), loop=loop) |
---|
150 | n/a | self.assertIs(t._loop, loop) |
---|
151 | n/a | loop.run_until_complete(t) |
---|
152 | n/a | loop.close() |
---|
153 | n/a | |
---|
154 | n/a | def test_ensure_future_future(self): |
---|
155 | n/a | f_orig = self.new_future(self.loop) |
---|
156 | n/a | f_orig.set_result('ko') |
---|
157 | n/a | |
---|
158 | n/a | f = asyncio.ensure_future(f_orig) |
---|
159 | n/a | self.loop.run_until_complete(f) |
---|
160 | n/a | self.assertTrue(f.done()) |
---|
161 | n/a | self.assertEqual(f.result(), 'ko') |
---|
162 | n/a | self.assertIs(f, f_orig) |
---|
163 | n/a | |
---|
164 | n/a | loop = asyncio.new_event_loop() |
---|
165 | n/a | self.set_event_loop(loop) |
---|
166 | n/a | |
---|
167 | n/a | with self.assertRaises(ValueError): |
---|
168 | n/a | f = asyncio.ensure_future(f_orig, loop=loop) |
---|
169 | n/a | |
---|
170 | n/a | loop.close() |
---|
171 | n/a | |
---|
172 | n/a | f = asyncio.ensure_future(f_orig, loop=self.loop) |
---|
173 | n/a | self.assertIs(f, f_orig) |
---|
174 | n/a | |
---|
175 | n/a | def test_ensure_future_task(self): |
---|
176 | n/a | @asyncio.coroutine |
---|
177 | n/a | def notmuch(): |
---|
178 | n/a | return 'ok' |
---|
179 | n/a | t_orig = self.new_task(self.loop, notmuch()) |
---|
180 | n/a | t = asyncio.ensure_future(t_orig) |
---|
181 | n/a | self.loop.run_until_complete(t) |
---|
182 | n/a | self.assertTrue(t.done()) |
---|
183 | n/a | self.assertEqual(t.result(), 'ok') |
---|
184 | n/a | self.assertIs(t, t_orig) |
---|
185 | n/a | |
---|
186 | n/a | loop = asyncio.new_event_loop() |
---|
187 | n/a | self.set_event_loop(loop) |
---|
188 | n/a | |
---|
189 | n/a | with self.assertRaises(ValueError): |
---|
190 | n/a | t = asyncio.ensure_future(t_orig, loop=loop) |
---|
191 | n/a | |
---|
192 | n/a | loop.close() |
---|
193 | n/a | |
---|
194 | n/a | t = asyncio.ensure_future(t_orig, loop=self.loop) |
---|
195 | n/a | self.assertIs(t, t_orig) |
---|
196 | n/a | |
---|
197 | n/a | @unittest.skipUnless(PY35, 'need python 3.5 or later') |
---|
198 | n/a | def test_ensure_future_awaitable(self): |
---|
199 | n/a | class Aw: |
---|
200 | n/a | def __init__(self, coro): |
---|
201 | n/a | self.coro = coro |
---|
202 | n/a | def __await__(self): |
---|
203 | n/a | return (yield from self.coro) |
---|
204 | n/a | |
---|
205 | n/a | @asyncio.coroutine |
---|
206 | n/a | def coro(): |
---|
207 | n/a | return 'ok' |
---|
208 | n/a | |
---|
209 | n/a | loop = asyncio.new_event_loop() |
---|
210 | n/a | self.set_event_loop(loop) |
---|
211 | n/a | fut = asyncio.ensure_future(Aw(coro()), loop=loop) |
---|
212 | n/a | loop.run_until_complete(fut) |
---|
213 | n/a | assert fut.result() == 'ok' |
---|
214 | n/a | |
---|
215 | n/a | def test_ensure_future_neither(self): |
---|
216 | n/a | with self.assertRaises(TypeError): |
---|
217 | n/a | asyncio.ensure_future('ok') |
---|
218 | n/a | |
---|
219 | n/a | def test_async_warning(self): |
---|
220 | n/a | f = self.new_future(self.loop) |
---|
221 | n/a | with self.assertWarnsRegex(DeprecationWarning, |
---|
222 | n/a | 'function is deprecated, use ensure_'): |
---|
223 | n/a | self.assertIs(f, asyncio.async(f)) |
---|
224 | n/a | |
---|
225 | n/a | def test_get_stack(self): |
---|
226 | n/a | T = None |
---|
227 | n/a | |
---|
228 | n/a | @asyncio.coroutine |
---|
229 | n/a | def foo(): |
---|
230 | n/a | yield from bar() |
---|
231 | n/a | |
---|
232 | n/a | @asyncio.coroutine |
---|
233 | n/a | def bar(): |
---|
234 | n/a | # test get_stack() |
---|
235 | n/a | f = T.get_stack(limit=1) |
---|
236 | n/a | try: |
---|
237 | n/a | self.assertEqual(f[0].f_code.co_name, 'foo') |
---|
238 | n/a | finally: |
---|
239 | n/a | f = None |
---|
240 | n/a | |
---|
241 | n/a | # test print_stack() |
---|
242 | n/a | file = io.StringIO() |
---|
243 | n/a | T.print_stack(limit=1, file=file) |
---|
244 | n/a | file.seek(0) |
---|
245 | n/a | tb = file.read() |
---|
246 | n/a | self.assertRegex(tb, r'foo\(\) running') |
---|
247 | n/a | |
---|
248 | n/a | @asyncio.coroutine |
---|
249 | n/a | def runner(): |
---|
250 | n/a | nonlocal T |
---|
251 | n/a | T = asyncio.ensure_future(foo(), loop=self.loop) |
---|
252 | n/a | yield from T |
---|
253 | n/a | |
---|
254 | n/a | self.loop.run_until_complete(runner()) |
---|
255 | n/a | |
---|
256 | n/a | def test_task_repr(self): |
---|
257 | n/a | self.loop.set_debug(False) |
---|
258 | n/a | |
---|
259 | n/a | @asyncio.coroutine |
---|
260 | n/a | def notmuch(): |
---|
261 | n/a | yield from [] |
---|
262 | n/a | return 'abc' |
---|
263 | n/a | |
---|
264 | n/a | # test coroutine function |
---|
265 | n/a | self.assertEqual(notmuch.__name__, 'notmuch') |
---|
266 | n/a | if PY35: |
---|
267 | n/a | self.assertRegex(notmuch.__qualname__, |
---|
268 | n/a | r'\w+.test_task_repr.<locals>.notmuch') |
---|
269 | n/a | self.assertEqual(notmuch.__module__, __name__) |
---|
270 | n/a | |
---|
271 | n/a | filename, lineno = test_utils.get_function_source(notmuch) |
---|
272 | n/a | src = "%s:%s" % (filename, lineno) |
---|
273 | n/a | |
---|
274 | n/a | # test coroutine object |
---|
275 | n/a | gen = notmuch() |
---|
276 | n/a | if coroutines._DEBUG or PY35: |
---|
277 | n/a | coro_qualname = 'BaseTaskTests.test_task_repr.<locals>.notmuch' |
---|
278 | n/a | else: |
---|
279 | n/a | coro_qualname = 'notmuch' |
---|
280 | n/a | self.assertEqual(gen.__name__, 'notmuch') |
---|
281 | n/a | if PY35: |
---|
282 | n/a | self.assertEqual(gen.__qualname__, |
---|
283 | n/a | coro_qualname) |
---|
284 | n/a | |
---|
285 | n/a | # test pending Task |
---|
286 | n/a | t = self.new_task(self.loop, gen) |
---|
287 | n/a | t.add_done_callback(Dummy()) |
---|
288 | n/a | |
---|
289 | n/a | coro = format_coroutine(coro_qualname, 'running', src, |
---|
290 | n/a | t._source_traceback, generator=True) |
---|
291 | n/a | self.assertEqual(repr(t), |
---|
292 | n/a | '<Task pending %s cb=[<Dummy>()]>' % coro) |
---|
293 | n/a | |
---|
294 | n/a | # test cancelling Task |
---|
295 | n/a | t.cancel() # Does not take immediate effect! |
---|
296 | n/a | self.assertEqual(repr(t), |
---|
297 | n/a | '<Task cancelling %s cb=[<Dummy>()]>' % coro) |
---|
298 | n/a | |
---|
299 | n/a | # test cancelled Task |
---|
300 | n/a | self.assertRaises(asyncio.CancelledError, |
---|
301 | n/a | self.loop.run_until_complete, t) |
---|
302 | n/a | coro = format_coroutine(coro_qualname, 'done', src, |
---|
303 | n/a | t._source_traceback) |
---|
304 | n/a | self.assertEqual(repr(t), |
---|
305 | n/a | '<Task cancelled %s>' % coro) |
---|
306 | n/a | |
---|
307 | n/a | # test finished Task |
---|
308 | n/a | t = self.new_task(self.loop, notmuch()) |
---|
309 | n/a | self.loop.run_until_complete(t) |
---|
310 | n/a | coro = format_coroutine(coro_qualname, 'done', src, |
---|
311 | n/a | t._source_traceback) |
---|
312 | n/a | self.assertEqual(repr(t), |
---|
313 | n/a | "<Task finished %s result='abc'>" % coro) |
---|
314 | n/a | |
---|
315 | n/a | def test_task_repr_coro_decorator(self): |
---|
316 | n/a | self.loop.set_debug(False) |
---|
317 | n/a | |
---|
318 | n/a | @asyncio.coroutine |
---|
319 | n/a | def notmuch(): |
---|
320 | n/a | # notmuch() function doesn't use yield from: it will be wrapped by |
---|
321 | n/a | # @coroutine decorator |
---|
322 | n/a | return 123 |
---|
323 | n/a | |
---|
324 | n/a | # test coroutine function |
---|
325 | n/a | self.assertEqual(notmuch.__name__, 'notmuch') |
---|
326 | n/a | if PY35: |
---|
327 | n/a | self.assertRegex(notmuch.__qualname__, |
---|
328 | n/a | r'\w+.test_task_repr_coro_decorator' |
---|
329 | n/a | r'\.<locals>\.notmuch') |
---|
330 | n/a | self.assertEqual(notmuch.__module__, __name__) |
---|
331 | n/a | |
---|
332 | n/a | # test coroutine object |
---|
333 | n/a | gen = notmuch() |
---|
334 | n/a | if coroutines._DEBUG or PY35: |
---|
335 | n/a | # On Python >= 3.5, generators now inherit the name of the |
---|
336 | n/a | # function, as expected, and have a qualified name (__qualname__ |
---|
337 | n/a | # attribute). |
---|
338 | n/a | coro_name = 'notmuch' |
---|
339 | n/a | coro_qualname = ('BaseTaskTests.test_task_repr_coro_decorator' |
---|
340 | n/a | '.<locals>.notmuch') |
---|
341 | n/a | else: |
---|
342 | n/a | # On Python < 3.5, generators inherit the name of the code, not of |
---|
343 | n/a | # the function. See: http://bugs.python.org/issue21205 |
---|
344 | n/a | coro_name = coro_qualname = 'coro' |
---|
345 | n/a | self.assertEqual(gen.__name__, coro_name) |
---|
346 | n/a | if PY35: |
---|
347 | n/a | self.assertEqual(gen.__qualname__, coro_qualname) |
---|
348 | n/a | |
---|
349 | n/a | # test repr(CoroWrapper) |
---|
350 | n/a | if coroutines._DEBUG: |
---|
351 | n/a | # format the coroutine object |
---|
352 | n/a | if coroutines._DEBUG: |
---|
353 | n/a | filename, lineno = test_utils.get_function_source(notmuch) |
---|
354 | n/a | frame = gen._source_traceback[-1] |
---|
355 | n/a | coro = ('%s() running, defined at %s:%s, created at %s:%s' |
---|
356 | n/a | % (coro_qualname, filename, lineno, |
---|
357 | n/a | frame[0], frame[1])) |
---|
358 | n/a | else: |
---|
359 | n/a | code = gen.gi_code |
---|
360 | n/a | coro = ('%s() running at %s:%s' |
---|
361 | n/a | % (coro_qualname, code.co_filename, |
---|
362 | n/a | code.co_firstlineno)) |
---|
363 | n/a | |
---|
364 | n/a | self.assertEqual(repr(gen), '<CoroWrapper %s>' % coro) |
---|
365 | n/a | |
---|
366 | n/a | # test pending Task |
---|
367 | n/a | t = self.new_task(self.loop, gen) |
---|
368 | n/a | t.add_done_callback(Dummy()) |
---|
369 | n/a | |
---|
370 | n/a | # format the coroutine object |
---|
371 | n/a | if coroutines._DEBUG: |
---|
372 | n/a | src = '%s:%s' % test_utils.get_function_source(notmuch) |
---|
373 | n/a | else: |
---|
374 | n/a | code = gen.gi_code |
---|
375 | n/a | src = '%s:%s' % (code.co_filename, code.co_firstlineno) |
---|
376 | n/a | coro = format_coroutine(coro_qualname, 'running', src, |
---|
377 | n/a | t._source_traceback, |
---|
378 | n/a | generator=not coroutines._DEBUG) |
---|
379 | n/a | self.assertEqual(repr(t), |
---|
380 | n/a | '<Task pending %s cb=[<Dummy>()]>' % coro) |
---|
381 | n/a | self.loop.run_until_complete(t) |
---|
382 | n/a | |
---|
383 | n/a | def test_task_repr_wait_for(self): |
---|
384 | n/a | self.loop.set_debug(False) |
---|
385 | n/a | |
---|
386 | n/a | @asyncio.coroutine |
---|
387 | n/a | def wait_for(fut): |
---|
388 | n/a | return (yield from fut) |
---|
389 | n/a | |
---|
390 | n/a | fut = self.new_future(self.loop) |
---|
391 | n/a | task = self.new_task(self.loop, wait_for(fut)) |
---|
392 | n/a | test_utils.run_briefly(self.loop) |
---|
393 | n/a | self.assertRegex(repr(task), |
---|
394 | n/a | '<Task .* wait_for=%s>' % re.escape(repr(fut))) |
---|
395 | n/a | |
---|
396 | n/a | fut.set_result(None) |
---|
397 | n/a | self.loop.run_until_complete(task) |
---|
398 | n/a | |
---|
399 | n/a | def test_task_repr_partial_corowrapper(self): |
---|
400 | n/a | # Issue #222: repr(CoroWrapper) must not fail in debug mode if the |
---|
401 | n/a | # coroutine is a partial function |
---|
402 | n/a | with set_coroutine_debug(True): |
---|
403 | n/a | self.loop.set_debug(True) |
---|
404 | n/a | |
---|
405 | n/a | @asyncio.coroutine |
---|
406 | n/a | def func(x, y): |
---|
407 | n/a | yield from asyncio.sleep(0) |
---|
408 | n/a | |
---|
409 | n/a | partial_func = asyncio.coroutine(functools.partial(func, 1)) |
---|
410 | n/a | task = self.loop.create_task(partial_func(2)) |
---|
411 | n/a | |
---|
412 | n/a | # make warnings quiet |
---|
413 | n/a | task._log_destroy_pending = False |
---|
414 | n/a | self.addCleanup(task._coro.close) |
---|
415 | n/a | |
---|
416 | n/a | coro_repr = repr(task._coro) |
---|
417 | n/a | expected = ( |
---|
418 | n/a | r'<CoroWrapper \w+.test_task_repr_partial_corowrapper' |
---|
419 | n/a | r'\.<locals>\.func\(1\)\(\) running, ' |
---|
420 | n/a | ) |
---|
421 | n/a | self.assertRegex(coro_repr, expected) |
---|
422 | n/a | |
---|
423 | n/a | def test_task_basics(self): |
---|
424 | n/a | @asyncio.coroutine |
---|
425 | n/a | def outer(): |
---|
426 | n/a | a = yield from inner1() |
---|
427 | n/a | b = yield from inner2() |
---|
428 | n/a | return a+b |
---|
429 | n/a | |
---|
430 | n/a | @asyncio.coroutine |
---|
431 | n/a | def inner1(): |
---|
432 | n/a | return 42 |
---|
433 | n/a | |
---|
434 | n/a | @asyncio.coroutine |
---|
435 | n/a | def inner2(): |
---|
436 | n/a | return 1000 |
---|
437 | n/a | |
---|
438 | n/a | t = outer() |
---|
439 | n/a | self.assertEqual(self.loop.run_until_complete(t), 1042) |
---|
440 | n/a | |
---|
441 | n/a | def test_cancel(self): |
---|
442 | n/a | |
---|
443 | n/a | def gen(): |
---|
444 | n/a | when = yield |
---|
445 | n/a | self.assertAlmostEqual(10.0, when) |
---|
446 | n/a | yield 0 |
---|
447 | n/a | |
---|
448 | n/a | loop = self.new_test_loop(gen) |
---|
449 | n/a | |
---|
450 | n/a | @asyncio.coroutine |
---|
451 | n/a | def task(): |
---|
452 | n/a | yield from asyncio.sleep(10.0, loop=loop) |
---|
453 | n/a | return 12 |
---|
454 | n/a | |
---|
455 | n/a | t = self.new_task(loop, task()) |
---|
456 | n/a | loop.call_soon(t.cancel) |
---|
457 | n/a | with self.assertRaises(asyncio.CancelledError): |
---|
458 | n/a | loop.run_until_complete(t) |
---|
459 | n/a | self.assertTrue(t.done()) |
---|
460 | n/a | self.assertTrue(t.cancelled()) |
---|
461 | n/a | self.assertFalse(t.cancel()) |
---|
462 | n/a | |
---|
463 | n/a | def test_cancel_yield(self): |
---|
464 | n/a | @asyncio.coroutine |
---|
465 | n/a | def task(): |
---|
466 | n/a | yield |
---|
467 | n/a | yield |
---|
468 | n/a | return 12 |
---|
469 | n/a | |
---|
470 | n/a | t = self.new_task(self.loop, task()) |
---|
471 | n/a | test_utils.run_briefly(self.loop) # start coro |
---|
472 | n/a | t.cancel() |
---|
473 | n/a | self.assertRaises( |
---|
474 | n/a | asyncio.CancelledError, self.loop.run_until_complete, t) |
---|
475 | n/a | self.assertTrue(t.done()) |
---|
476 | n/a | self.assertTrue(t.cancelled()) |
---|
477 | n/a | self.assertFalse(t.cancel()) |
---|
478 | n/a | |
---|
479 | n/a | def test_cancel_inner_future(self): |
---|
480 | n/a | f = self.new_future(self.loop) |
---|
481 | n/a | |
---|
482 | n/a | @asyncio.coroutine |
---|
483 | n/a | def task(): |
---|
484 | n/a | yield from f |
---|
485 | n/a | return 12 |
---|
486 | n/a | |
---|
487 | n/a | t = self.new_task(self.loop, task()) |
---|
488 | n/a | test_utils.run_briefly(self.loop) # start task |
---|
489 | n/a | f.cancel() |
---|
490 | n/a | with self.assertRaises(asyncio.CancelledError): |
---|
491 | n/a | self.loop.run_until_complete(t) |
---|
492 | n/a | self.assertTrue(f.cancelled()) |
---|
493 | n/a | self.assertTrue(t.cancelled()) |
---|
494 | n/a | |
---|
495 | n/a | def test_cancel_both_task_and_inner_future(self): |
---|
496 | n/a | f = self.new_future(self.loop) |
---|
497 | n/a | |
---|
498 | n/a | @asyncio.coroutine |
---|
499 | n/a | def task(): |
---|
500 | n/a | yield from f |
---|
501 | n/a | return 12 |
---|
502 | n/a | |
---|
503 | n/a | t = self.new_task(self.loop, task()) |
---|
504 | n/a | test_utils.run_briefly(self.loop) |
---|
505 | n/a | |
---|
506 | n/a | f.cancel() |
---|
507 | n/a | t.cancel() |
---|
508 | n/a | |
---|
509 | n/a | with self.assertRaises(asyncio.CancelledError): |
---|
510 | n/a | self.loop.run_until_complete(t) |
---|
511 | n/a | |
---|
512 | n/a | self.assertTrue(t.done()) |
---|
513 | n/a | self.assertTrue(f.cancelled()) |
---|
514 | n/a | self.assertTrue(t.cancelled()) |
---|
515 | n/a | |
---|
516 | n/a | def test_cancel_task_catching(self): |
---|
517 | n/a | fut1 = self.new_future(self.loop) |
---|
518 | n/a | fut2 = self.new_future(self.loop) |
---|
519 | n/a | |
---|
520 | n/a | @asyncio.coroutine |
---|
521 | n/a | def task(): |
---|
522 | n/a | yield from fut1 |
---|
523 | n/a | try: |
---|
524 | n/a | yield from fut2 |
---|
525 | n/a | except asyncio.CancelledError: |
---|
526 | n/a | return 42 |
---|
527 | n/a | |
---|
528 | n/a | t = self.new_task(self.loop, task()) |
---|
529 | n/a | test_utils.run_briefly(self.loop) |
---|
530 | n/a | self.assertIs(t._fut_waiter, fut1) # White-box test. |
---|
531 | n/a | fut1.set_result(None) |
---|
532 | n/a | test_utils.run_briefly(self.loop) |
---|
533 | n/a | self.assertIs(t._fut_waiter, fut2) # White-box test. |
---|
534 | n/a | t.cancel() |
---|
535 | n/a | self.assertTrue(fut2.cancelled()) |
---|
536 | n/a | res = self.loop.run_until_complete(t) |
---|
537 | n/a | self.assertEqual(res, 42) |
---|
538 | n/a | self.assertFalse(t.cancelled()) |
---|
539 | n/a | |
---|
540 | n/a | def test_cancel_task_ignoring(self): |
---|
541 | n/a | fut1 = self.new_future(self.loop) |
---|
542 | n/a | fut2 = self.new_future(self.loop) |
---|
543 | n/a | fut3 = self.new_future(self.loop) |
---|
544 | n/a | |
---|
545 | n/a | @asyncio.coroutine |
---|
546 | n/a | def task(): |
---|
547 | n/a | yield from fut1 |
---|
548 | n/a | try: |
---|
549 | n/a | yield from fut2 |
---|
550 | n/a | except asyncio.CancelledError: |
---|
551 | n/a | pass |
---|
552 | n/a | res = yield from fut3 |
---|
553 | n/a | return res |
---|
554 | n/a | |
---|
555 | n/a | t = self.new_task(self.loop, task()) |
---|
556 | n/a | test_utils.run_briefly(self.loop) |
---|
557 | n/a | self.assertIs(t._fut_waiter, fut1) # White-box test. |
---|
558 | n/a | fut1.set_result(None) |
---|
559 | n/a | test_utils.run_briefly(self.loop) |
---|
560 | n/a | self.assertIs(t._fut_waiter, fut2) # White-box test. |
---|
561 | n/a | t.cancel() |
---|
562 | n/a | self.assertTrue(fut2.cancelled()) |
---|
563 | n/a | test_utils.run_briefly(self.loop) |
---|
564 | n/a | self.assertIs(t._fut_waiter, fut3) # White-box test. |
---|
565 | n/a | fut3.set_result(42) |
---|
566 | n/a | res = self.loop.run_until_complete(t) |
---|
567 | n/a | self.assertEqual(res, 42) |
---|
568 | n/a | self.assertFalse(fut3.cancelled()) |
---|
569 | n/a | self.assertFalse(t.cancelled()) |
---|
570 | n/a | |
---|
571 | n/a | def test_cancel_current_task(self): |
---|
572 | n/a | loop = asyncio.new_event_loop() |
---|
573 | n/a | self.set_event_loop(loop) |
---|
574 | n/a | |
---|
575 | n/a | @asyncio.coroutine |
---|
576 | n/a | def task(): |
---|
577 | n/a | t.cancel() |
---|
578 | n/a | self.assertTrue(t._must_cancel) # White-box test. |
---|
579 | n/a | # The sleep should be cancelled immediately. |
---|
580 | n/a | yield from asyncio.sleep(100, loop=loop) |
---|
581 | n/a | return 12 |
---|
582 | n/a | |
---|
583 | n/a | t = self.new_task(loop, task()) |
---|
584 | n/a | self.assertRaises( |
---|
585 | n/a | asyncio.CancelledError, loop.run_until_complete, t) |
---|
586 | n/a | self.assertTrue(t.done()) |
---|
587 | n/a | self.assertFalse(t._must_cancel) # White-box test. |
---|
588 | n/a | self.assertFalse(t.cancel()) |
---|
589 | n/a | |
---|
590 | n/a | def test_stop_while_run_in_complete(self): |
---|
591 | n/a | |
---|
592 | n/a | def gen(): |
---|
593 | n/a | when = yield |
---|
594 | n/a | self.assertAlmostEqual(0.1, when) |
---|
595 | n/a | when = yield 0.1 |
---|
596 | n/a | self.assertAlmostEqual(0.2, when) |
---|
597 | n/a | when = yield 0.1 |
---|
598 | n/a | self.assertAlmostEqual(0.3, when) |
---|
599 | n/a | yield 0.1 |
---|
600 | n/a | |
---|
601 | n/a | loop = self.new_test_loop(gen) |
---|
602 | n/a | |
---|
603 | n/a | x = 0 |
---|
604 | n/a | waiters = [] |
---|
605 | n/a | |
---|
606 | n/a | @asyncio.coroutine |
---|
607 | n/a | def task(): |
---|
608 | n/a | nonlocal x |
---|
609 | n/a | while x < 10: |
---|
610 | n/a | waiters.append(asyncio.sleep(0.1, loop=loop)) |
---|
611 | n/a | yield from waiters[-1] |
---|
612 | n/a | x += 1 |
---|
613 | n/a | if x == 2: |
---|
614 | n/a | loop.stop() |
---|
615 | n/a | |
---|
616 | n/a | t = self.new_task(loop, task()) |
---|
617 | n/a | with self.assertRaises(RuntimeError) as cm: |
---|
618 | n/a | loop.run_until_complete(t) |
---|
619 | n/a | self.assertEqual(str(cm.exception), |
---|
620 | n/a | 'Event loop stopped before Future completed.') |
---|
621 | n/a | self.assertFalse(t.done()) |
---|
622 | n/a | self.assertEqual(x, 2) |
---|
623 | n/a | self.assertAlmostEqual(0.3, loop.time()) |
---|
624 | n/a | |
---|
625 | n/a | # close generators |
---|
626 | n/a | for w in waiters: |
---|
627 | n/a | w.close() |
---|
628 | n/a | t.cancel() |
---|
629 | n/a | self.assertRaises(asyncio.CancelledError, loop.run_until_complete, t) |
---|
630 | n/a | |
---|
631 | n/a | def test_wait_for(self): |
---|
632 | n/a | |
---|
633 | n/a | def gen(): |
---|
634 | n/a | when = yield |
---|
635 | n/a | self.assertAlmostEqual(0.2, when) |
---|
636 | n/a | when = yield 0 |
---|
637 | n/a | self.assertAlmostEqual(0.1, when) |
---|
638 | n/a | when = yield 0.1 |
---|
639 | n/a | |
---|
640 | n/a | loop = self.new_test_loop(gen) |
---|
641 | n/a | |
---|
642 | n/a | foo_running = None |
---|
643 | n/a | |
---|
644 | n/a | @asyncio.coroutine |
---|
645 | n/a | def foo(): |
---|
646 | n/a | nonlocal foo_running |
---|
647 | n/a | foo_running = True |
---|
648 | n/a | try: |
---|
649 | n/a | yield from asyncio.sleep(0.2, loop=loop) |
---|
650 | n/a | finally: |
---|
651 | n/a | foo_running = False |
---|
652 | n/a | return 'done' |
---|
653 | n/a | |
---|
654 | n/a | fut = self.new_task(loop, foo()) |
---|
655 | n/a | |
---|
656 | n/a | with self.assertRaises(asyncio.TimeoutError): |
---|
657 | n/a | loop.run_until_complete(asyncio.wait_for(fut, 0.1, loop=loop)) |
---|
658 | n/a | self.assertTrue(fut.done()) |
---|
659 | n/a | # it should have been cancelled due to the timeout |
---|
660 | n/a | self.assertTrue(fut.cancelled()) |
---|
661 | n/a | self.assertAlmostEqual(0.1, loop.time()) |
---|
662 | n/a | self.assertEqual(foo_running, False) |
---|
663 | n/a | |
---|
664 | n/a | def test_wait_for_blocking(self): |
---|
665 | n/a | loop = self.new_test_loop() |
---|
666 | n/a | |
---|
667 | n/a | @asyncio.coroutine |
---|
668 | n/a | def coro(): |
---|
669 | n/a | return 'done' |
---|
670 | n/a | |
---|
671 | n/a | res = loop.run_until_complete(asyncio.wait_for(coro(), |
---|
672 | n/a | timeout=None, |
---|
673 | n/a | loop=loop)) |
---|
674 | n/a | self.assertEqual(res, 'done') |
---|
675 | n/a | |
---|
676 | n/a | def test_wait_for_with_global_loop(self): |
---|
677 | n/a | |
---|
678 | n/a | def gen(): |
---|
679 | n/a | when = yield |
---|
680 | n/a | self.assertAlmostEqual(0.2, when) |
---|
681 | n/a | when = yield 0 |
---|
682 | n/a | self.assertAlmostEqual(0.01, when) |
---|
683 | n/a | yield 0.01 |
---|
684 | n/a | |
---|
685 | n/a | loop = self.new_test_loop(gen) |
---|
686 | n/a | |
---|
687 | n/a | @asyncio.coroutine |
---|
688 | n/a | def foo(): |
---|
689 | n/a | yield from asyncio.sleep(0.2, loop=loop) |
---|
690 | n/a | return 'done' |
---|
691 | n/a | |
---|
692 | n/a | asyncio.set_event_loop(loop) |
---|
693 | n/a | try: |
---|
694 | n/a | fut = self.new_task(loop, foo()) |
---|
695 | n/a | with self.assertRaises(asyncio.TimeoutError): |
---|
696 | n/a | loop.run_until_complete(asyncio.wait_for(fut, 0.01)) |
---|
697 | n/a | finally: |
---|
698 | n/a | asyncio.set_event_loop(None) |
---|
699 | n/a | |
---|
700 | n/a | self.assertAlmostEqual(0.01, loop.time()) |
---|
701 | n/a | self.assertTrue(fut.done()) |
---|
702 | n/a | self.assertTrue(fut.cancelled()) |
---|
703 | n/a | |
---|
704 | n/a | def test_wait_for_race_condition(self): |
---|
705 | n/a | |
---|
706 | n/a | def gen(): |
---|
707 | n/a | yield 0.1 |
---|
708 | n/a | yield 0.1 |
---|
709 | n/a | yield 0.1 |
---|
710 | n/a | |
---|
711 | n/a | loop = self.new_test_loop(gen) |
---|
712 | n/a | |
---|
713 | n/a | fut = self.new_future(loop) |
---|
714 | n/a | task = asyncio.wait_for(fut, timeout=0.2, loop=loop) |
---|
715 | n/a | loop.call_later(0.1, fut.set_result, "ok") |
---|
716 | n/a | res = loop.run_until_complete(task) |
---|
717 | n/a | self.assertEqual(res, "ok") |
---|
718 | n/a | |
---|
719 | n/a | def test_wait(self): |
---|
720 | n/a | |
---|
721 | n/a | def gen(): |
---|
722 | n/a | when = yield |
---|
723 | n/a | self.assertAlmostEqual(0.1, when) |
---|
724 | n/a | when = yield 0 |
---|
725 | n/a | self.assertAlmostEqual(0.15, when) |
---|
726 | n/a | yield 0.15 |
---|
727 | n/a | |
---|
728 | n/a | loop = self.new_test_loop(gen) |
---|
729 | n/a | |
---|
730 | n/a | a = self.new_task(loop, asyncio.sleep(0.1, loop=loop)) |
---|
731 | n/a | b = self.new_task(loop, asyncio.sleep(0.15, loop=loop)) |
---|
732 | n/a | |
---|
733 | n/a | @asyncio.coroutine |
---|
734 | n/a | def foo(): |
---|
735 | n/a | done, pending = yield from asyncio.wait([b, a], loop=loop) |
---|
736 | n/a | self.assertEqual(done, set([a, b])) |
---|
737 | n/a | self.assertEqual(pending, set()) |
---|
738 | n/a | return 42 |
---|
739 | n/a | |
---|
740 | n/a | res = loop.run_until_complete(self.new_task(loop, foo())) |
---|
741 | n/a | self.assertEqual(res, 42) |
---|
742 | n/a | self.assertAlmostEqual(0.15, loop.time()) |
---|
743 | n/a | |
---|
744 | n/a | # Doing it again should take no time and exercise a different path. |
---|
745 | n/a | res = loop.run_until_complete(self.new_task(loop, foo())) |
---|
746 | n/a | self.assertAlmostEqual(0.15, loop.time()) |
---|
747 | n/a | self.assertEqual(res, 42) |
---|
748 | n/a | |
---|
749 | n/a | def test_wait_with_global_loop(self): |
---|
750 | n/a | |
---|
751 | n/a | def gen(): |
---|
752 | n/a | when = yield |
---|
753 | n/a | self.assertAlmostEqual(0.01, when) |
---|
754 | n/a | when = yield 0 |
---|
755 | n/a | self.assertAlmostEqual(0.015, when) |
---|
756 | n/a | yield 0.015 |
---|
757 | n/a | |
---|
758 | n/a | loop = self.new_test_loop(gen) |
---|
759 | n/a | |
---|
760 | n/a | a = self.new_task(loop, asyncio.sleep(0.01, loop=loop)) |
---|
761 | n/a | b = self.new_task(loop, asyncio.sleep(0.015, loop=loop)) |
---|
762 | n/a | |
---|
763 | n/a | @asyncio.coroutine |
---|
764 | n/a | def foo(): |
---|
765 | n/a | done, pending = yield from asyncio.wait([b, a]) |
---|
766 | n/a | self.assertEqual(done, set([a, b])) |
---|
767 | n/a | self.assertEqual(pending, set()) |
---|
768 | n/a | return 42 |
---|
769 | n/a | |
---|
770 | n/a | asyncio.set_event_loop(loop) |
---|
771 | n/a | res = loop.run_until_complete( |
---|
772 | n/a | self.new_task(loop, foo())) |
---|
773 | n/a | |
---|
774 | n/a | self.assertEqual(res, 42) |
---|
775 | n/a | |
---|
776 | n/a | def test_wait_duplicate_coroutines(self): |
---|
777 | n/a | @asyncio.coroutine |
---|
778 | n/a | def coro(s): |
---|
779 | n/a | return s |
---|
780 | n/a | c = coro('test') |
---|
781 | n/a | |
---|
782 | n/a | task =self.new_task( |
---|
783 | n/a | self.loop, |
---|
784 | n/a | asyncio.wait([c, c, coro('spam')], loop=self.loop)) |
---|
785 | n/a | |
---|
786 | n/a | done, pending = self.loop.run_until_complete(task) |
---|
787 | n/a | |
---|
788 | n/a | self.assertFalse(pending) |
---|
789 | n/a | self.assertEqual(set(f.result() for f in done), {'test', 'spam'}) |
---|
790 | n/a | |
---|
791 | n/a | def test_wait_errors(self): |
---|
792 | n/a | self.assertRaises( |
---|
793 | n/a | ValueError, self.loop.run_until_complete, |
---|
794 | n/a | asyncio.wait(set(), loop=self.loop)) |
---|
795 | n/a | |
---|
796 | n/a | # -1 is an invalid return_when value |
---|
797 | n/a | sleep_coro = asyncio.sleep(10.0, loop=self.loop) |
---|
798 | n/a | wait_coro = asyncio.wait([sleep_coro], return_when=-1, loop=self.loop) |
---|
799 | n/a | self.assertRaises(ValueError, |
---|
800 | n/a | self.loop.run_until_complete, wait_coro) |
---|
801 | n/a | |
---|
802 | n/a | sleep_coro.close() |
---|
803 | n/a | |
---|
804 | n/a | def test_wait_first_completed(self): |
---|
805 | n/a | |
---|
806 | n/a | def gen(): |
---|
807 | n/a | when = yield |
---|
808 | n/a | self.assertAlmostEqual(10.0, when) |
---|
809 | n/a | when = yield 0 |
---|
810 | n/a | self.assertAlmostEqual(0.1, when) |
---|
811 | n/a | yield 0.1 |
---|
812 | n/a | |
---|
813 | n/a | loop = self.new_test_loop(gen) |
---|
814 | n/a | |
---|
815 | n/a | a = self.new_task(loop, asyncio.sleep(10.0, loop=loop)) |
---|
816 | n/a | b = self.new_task(loop, asyncio.sleep(0.1, loop=loop)) |
---|
817 | n/a | task = self.new_task( |
---|
818 | n/a | loop, |
---|
819 | n/a | asyncio.wait([b, a], return_when=asyncio.FIRST_COMPLETED, |
---|
820 | n/a | loop=loop)) |
---|
821 | n/a | |
---|
822 | n/a | done, pending = loop.run_until_complete(task) |
---|
823 | n/a | self.assertEqual({b}, done) |
---|
824 | n/a | self.assertEqual({a}, pending) |
---|
825 | n/a | self.assertFalse(a.done()) |
---|
826 | n/a | self.assertTrue(b.done()) |
---|
827 | n/a | self.assertIsNone(b.result()) |
---|
828 | n/a | self.assertAlmostEqual(0.1, loop.time()) |
---|
829 | n/a | |
---|
830 | n/a | # move forward to close generator |
---|
831 | n/a | loop.advance_time(10) |
---|
832 | n/a | loop.run_until_complete(asyncio.wait([a, b], loop=loop)) |
---|
833 | n/a | |
---|
834 | n/a | def test_wait_really_done(self): |
---|
835 | n/a | # there is possibility that some tasks in the pending list |
---|
836 | n/a | # became done but their callbacks haven't all been called yet |
---|
837 | n/a | |
---|
838 | n/a | @asyncio.coroutine |
---|
839 | n/a | def coro1(): |
---|
840 | n/a | yield |
---|
841 | n/a | |
---|
842 | n/a | @asyncio.coroutine |
---|
843 | n/a | def coro2(): |
---|
844 | n/a | yield |
---|
845 | n/a | yield |
---|
846 | n/a | |
---|
847 | n/a | a = self.new_task(self.loop, coro1()) |
---|
848 | n/a | b = self.new_task(self.loop, coro2()) |
---|
849 | n/a | task = self.new_task( |
---|
850 | n/a | self.loop, |
---|
851 | n/a | asyncio.wait([b, a], return_when=asyncio.FIRST_COMPLETED, |
---|
852 | n/a | loop=self.loop)) |
---|
853 | n/a | |
---|
854 | n/a | done, pending = self.loop.run_until_complete(task) |
---|
855 | n/a | self.assertEqual({a, b}, done) |
---|
856 | n/a | self.assertTrue(a.done()) |
---|
857 | n/a | self.assertIsNone(a.result()) |
---|
858 | n/a | self.assertTrue(b.done()) |
---|
859 | n/a | self.assertIsNone(b.result()) |
---|
860 | n/a | |
---|
861 | n/a | def test_wait_first_exception(self): |
---|
862 | n/a | |
---|
863 | n/a | def gen(): |
---|
864 | n/a | when = yield |
---|
865 | n/a | self.assertAlmostEqual(10.0, when) |
---|
866 | n/a | yield 0 |
---|
867 | n/a | |
---|
868 | n/a | loop = self.new_test_loop(gen) |
---|
869 | n/a | |
---|
870 | n/a | # first_exception, task already has exception |
---|
871 | n/a | a = self.new_task(loop, asyncio.sleep(10.0, loop=loop)) |
---|
872 | n/a | |
---|
873 | n/a | @asyncio.coroutine |
---|
874 | n/a | def exc(): |
---|
875 | n/a | raise ZeroDivisionError('err') |
---|
876 | n/a | |
---|
877 | n/a | b = self.new_task(loop, exc()) |
---|
878 | n/a | task = self.new_task( |
---|
879 | n/a | loop, |
---|
880 | n/a | asyncio.wait([b, a], return_when=asyncio.FIRST_EXCEPTION, |
---|
881 | n/a | loop=loop)) |
---|
882 | n/a | |
---|
883 | n/a | done, pending = loop.run_until_complete(task) |
---|
884 | n/a | self.assertEqual({b}, done) |
---|
885 | n/a | self.assertEqual({a}, pending) |
---|
886 | n/a | self.assertAlmostEqual(0, loop.time()) |
---|
887 | n/a | |
---|
888 | n/a | # move forward to close generator |
---|
889 | n/a | loop.advance_time(10) |
---|
890 | n/a | loop.run_until_complete(asyncio.wait([a, b], loop=loop)) |
---|
891 | n/a | |
---|
892 | n/a | def test_wait_first_exception_in_wait(self): |
---|
893 | n/a | |
---|
894 | n/a | def gen(): |
---|
895 | n/a | when = yield |
---|
896 | n/a | self.assertAlmostEqual(10.0, when) |
---|
897 | n/a | when = yield 0 |
---|
898 | n/a | self.assertAlmostEqual(0.01, when) |
---|
899 | n/a | yield 0.01 |
---|
900 | n/a | |
---|
901 | n/a | loop = self.new_test_loop(gen) |
---|
902 | n/a | |
---|
903 | n/a | # first_exception, exception during waiting |
---|
904 | n/a | a = self.new_task(loop, asyncio.sleep(10.0, loop=loop)) |
---|
905 | n/a | |
---|
906 | n/a | @asyncio.coroutine |
---|
907 | n/a | def exc(): |
---|
908 | n/a | yield from asyncio.sleep(0.01, loop=loop) |
---|
909 | n/a | raise ZeroDivisionError('err') |
---|
910 | n/a | |
---|
911 | n/a | b = self.new_task(loop, exc()) |
---|
912 | n/a | task = asyncio.wait([b, a], return_when=asyncio.FIRST_EXCEPTION, |
---|
913 | n/a | loop=loop) |
---|
914 | n/a | |
---|
915 | n/a | done, pending = loop.run_until_complete(task) |
---|
916 | n/a | self.assertEqual({b}, done) |
---|
917 | n/a | self.assertEqual({a}, pending) |
---|
918 | n/a | self.assertAlmostEqual(0.01, loop.time()) |
---|
919 | n/a | |
---|
920 | n/a | # move forward to close generator |
---|
921 | n/a | loop.advance_time(10) |
---|
922 | n/a | loop.run_until_complete(asyncio.wait([a, b], loop=loop)) |
---|
923 | n/a | |
---|
924 | n/a | def test_wait_with_exception(self): |
---|
925 | n/a | |
---|
926 | n/a | def gen(): |
---|
927 | n/a | when = yield |
---|
928 | n/a | self.assertAlmostEqual(0.1, when) |
---|
929 | n/a | when = yield 0 |
---|
930 | n/a | self.assertAlmostEqual(0.15, when) |
---|
931 | n/a | yield 0.15 |
---|
932 | n/a | |
---|
933 | n/a | loop = self.new_test_loop(gen) |
---|
934 | n/a | |
---|
935 | n/a | a = self.new_task(loop, asyncio.sleep(0.1, loop=loop)) |
---|
936 | n/a | |
---|
937 | n/a | @asyncio.coroutine |
---|
938 | n/a | def sleeper(): |
---|
939 | n/a | yield from asyncio.sleep(0.15, loop=loop) |
---|
940 | n/a | raise ZeroDivisionError('really') |
---|
941 | n/a | |
---|
942 | n/a | b = self.new_task(loop, sleeper()) |
---|
943 | n/a | |
---|
944 | n/a | @asyncio.coroutine |
---|
945 | n/a | def foo(): |
---|
946 | n/a | done, pending = yield from asyncio.wait([b, a], loop=loop) |
---|
947 | n/a | self.assertEqual(len(done), 2) |
---|
948 | n/a | self.assertEqual(pending, set()) |
---|
949 | n/a | errors = set(f for f in done if f.exception() is not None) |
---|
950 | n/a | self.assertEqual(len(errors), 1) |
---|
951 | n/a | |
---|
952 | n/a | loop.run_until_complete(self.new_task(loop, foo())) |
---|
953 | n/a | self.assertAlmostEqual(0.15, loop.time()) |
---|
954 | n/a | |
---|
955 | n/a | loop.run_until_complete(self.new_task(loop, foo())) |
---|
956 | n/a | self.assertAlmostEqual(0.15, loop.time()) |
---|
957 | n/a | |
---|
958 | n/a | def test_wait_with_timeout(self): |
---|
959 | n/a | |
---|
960 | n/a | def gen(): |
---|
961 | n/a | when = yield |
---|
962 | n/a | self.assertAlmostEqual(0.1, when) |
---|
963 | n/a | when = yield 0 |
---|
964 | n/a | self.assertAlmostEqual(0.15, when) |
---|
965 | n/a | when = yield 0 |
---|
966 | n/a | self.assertAlmostEqual(0.11, when) |
---|
967 | n/a | yield 0.11 |
---|
968 | n/a | |
---|
969 | n/a | loop = self.new_test_loop(gen) |
---|
970 | n/a | |
---|
971 | n/a | a = self.new_task(loop, asyncio.sleep(0.1, loop=loop)) |
---|
972 | n/a | b = self.new_task(loop, asyncio.sleep(0.15, loop=loop)) |
---|
973 | n/a | |
---|
974 | n/a | @asyncio.coroutine |
---|
975 | n/a | def foo(): |
---|
976 | n/a | done, pending = yield from asyncio.wait([b, a], timeout=0.11, |
---|
977 | n/a | loop=loop) |
---|
978 | n/a | self.assertEqual(done, set([a])) |
---|
979 | n/a | self.assertEqual(pending, set([b])) |
---|
980 | n/a | |
---|
981 | n/a | loop.run_until_complete(self.new_task(loop, foo())) |
---|
982 | n/a | self.assertAlmostEqual(0.11, loop.time()) |
---|
983 | n/a | |
---|
984 | n/a | # move forward to close generator |
---|
985 | n/a | loop.advance_time(10) |
---|
986 | n/a | loop.run_until_complete(asyncio.wait([a, b], loop=loop)) |
---|
987 | n/a | |
---|
988 | n/a | def test_wait_concurrent_complete(self): |
---|
989 | n/a | |
---|
990 | n/a | def gen(): |
---|
991 | n/a | when = yield |
---|
992 | n/a | self.assertAlmostEqual(0.1, when) |
---|
993 | n/a | when = yield 0 |
---|
994 | n/a | self.assertAlmostEqual(0.15, when) |
---|
995 | n/a | when = yield 0 |
---|
996 | n/a | self.assertAlmostEqual(0.1, when) |
---|
997 | n/a | yield 0.1 |
---|
998 | n/a | |
---|
999 | n/a | loop = self.new_test_loop(gen) |
---|
1000 | n/a | |
---|
1001 | n/a | a = self.new_task(loop, asyncio.sleep(0.1, loop=loop)) |
---|
1002 | n/a | b = self.new_task(loop, asyncio.sleep(0.15, loop=loop)) |
---|
1003 | n/a | |
---|
1004 | n/a | done, pending = loop.run_until_complete( |
---|
1005 | n/a | asyncio.wait([b, a], timeout=0.1, loop=loop)) |
---|
1006 | n/a | |
---|
1007 | n/a | self.assertEqual(done, set([a])) |
---|
1008 | n/a | self.assertEqual(pending, set([b])) |
---|
1009 | n/a | self.assertAlmostEqual(0.1, loop.time()) |
---|
1010 | n/a | |
---|
1011 | n/a | # move forward to close generator |
---|
1012 | n/a | loop.advance_time(10) |
---|
1013 | n/a | loop.run_until_complete(asyncio.wait([a, b], loop=loop)) |
---|
1014 | n/a | |
---|
1015 | n/a | def test_as_completed(self): |
---|
1016 | n/a | |
---|
1017 | n/a | def gen(): |
---|
1018 | n/a | yield 0 |
---|
1019 | n/a | yield 0 |
---|
1020 | n/a | yield 0.01 |
---|
1021 | n/a | yield 0 |
---|
1022 | n/a | |
---|
1023 | n/a | loop = self.new_test_loop(gen) |
---|
1024 | n/a | # disable "slow callback" warning |
---|
1025 | n/a | loop.slow_callback_duration = 1.0 |
---|
1026 | n/a | completed = set() |
---|
1027 | n/a | time_shifted = False |
---|
1028 | n/a | |
---|
1029 | n/a | @asyncio.coroutine |
---|
1030 | n/a | def sleeper(dt, x): |
---|
1031 | n/a | nonlocal time_shifted |
---|
1032 | n/a | yield from asyncio.sleep(dt, loop=loop) |
---|
1033 | n/a | completed.add(x) |
---|
1034 | n/a | if not time_shifted and 'a' in completed and 'b' in completed: |
---|
1035 | n/a | time_shifted = True |
---|
1036 | n/a | loop.advance_time(0.14) |
---|
1037 | n/a | return x |
---|
1038 | n/a | |
---|
1039 | n/a | a = sleeper(0.01, 'a') |
---|
1040 | n/a | b = sleeper(0.01, 'b') |
---|
1041 | n/a | c = sleeper(0.15, 'c') |
---|
1042 | n/a | |
---|
1043 | n/a | @asyncio.coroutine |
---|
1044 | n/a | def foo(): |
---|
1045 | n/a | values = [] |
---|
1046 | n/a | for f in asyncio.as_completed([b, c, a], loop=loop): |
---|
1047 | n/a | values.append((yield from f)) |
---|
1048 | n/a | return values |
---|
1049 | n/a | |
---|
1050 | n/a | res = loop.run_until_complete(self.new_task(loop, foo())) |
---|
1051 | n/a | self.assertAlmostEqual(0.15, loop.time()) |
---|
1052 | n/a | self.assertTrue('a' in res[:2]) |
---|
1053 | n/a | self.assertTrue('b' in res[:2]) |
---|
1054 | n/a | self.assertEqual(res[2], 'c') |
---|
1055 | n/a | |
---|
1056 | n/a | # Doing it again should take no time and exercise a different path. |
---|
1057 | n/a | res = loop.run_until_complete(self.new_task(loop, foo())) |
---|
1058 | n/a | self.assertAlmostEqual(0.15, loop.time()) |
---|
1059 | n/a | |
---|
1060 | n/a | def test_as_completed_with_timeout(self): |
---|
1061 | n/a | |
---|
1062 | n/a | def gen(): |
---|
1063 | n/a | yield |
---|
1064 | n/a | yield 0 |
---|
1065 | n/a | yield 0 |
---|
1066 | n/a | yield 0.1 |
---|
1067 | n/a | |
---|
1068 | n/a | loop = self.new_test_loop(gen) |
---|
1069 | n/a | |
---|
1070 | n/a | a = asyncio.sleep(0.1, 'a', loop=loop) |
---|
1071 | n/a | b = asyncio.sleep(0.15, 'b', loop=loop) |
---|
1072 | n/a | |
---|
1073 | n/a | @asyncio.coroutine |
---|
1074 | n/a | def foo(): |
---|
1075 | n/a | values = [] |
---|
1076 | n/a | for f in asyncio.as_completed([a, b], timeout=0.12, loop=loop): |
---|
1077 | n/a | if values: |
---|
1078 | n/a | loop.advance_time(0.02) |
---|
1079 | n/a | try: |
---|
1080 | n/a | v = yield from f |
---|
1081 | n/a | values.append((1, v)) |
---|
1082 | n/a | except asyncio.TimeoutError as exc: |
---|
1083 | n/a | values.append((2, exc)) |
---|
1084 | n/a | return values |
---|
1085 | n/a | |
---|
1086 | n/a | res = loop.run_until_complete(self.new_task(loop, foo())) |
---|
1087 | n/a | self.assertEqual(len(res), 2, res) |
---|
1088 | n/a | self.assertEqual(res[0], (1, 'a')) |
---|
1089 | n/a | self.assertEqual(res[1][0], 2) |
---|
1090 | n/a | self.assertIsInstance(res[1][1], asyncio.TimeoutError) |
---|
1091 | n/a | self.assertAlmostEqual(0.12, loop.time()) |
---|
1092 | n/a | |
---|
1093 | n/a | # move forward to close generator |
---|
1094 | n/a | loop.advance_time(10) |
---|
1095 | n/a | loop.run_until_complete(asyncio.wait([a, b], loop=loop)) |
---|
1096 | n/a | |
---|
1097 | n/a | def test_as_completed_with_unused_timeout(self): |
---|
1098 | n/a | |
---|
1099 | n/a | def gen(): |
---|
1100 | n/a | yield |
---|
1101 | n/a | yield 0 |
---|
1102 | n/a | yield 0.01 |
---|
1103 | n/a | |
---|
1104 | n/a | loop = self.new_test_loop(gen) |
---|
1105 | n/a | |
---|
1106 | n/a | a = asyncio.sleep(0.01, 'a', loop=loop) |
---|
1107 | n/a | |
---|
1108 | n/a | @asyncio.coroutine |
---|
1109 | n/a | def foo(): |
---|
1110 | n/a | for f in asyncio.as_completed([a], timeout=1, loop=loop): |
---|
1111 | n/a | v = yield from f |
---|
1112 | n/a | self.assertEqual(v, 'a') |
---|
1113 | n/a | |
---|
1114 | n/a | loop.run_until_complete(self.new_task(loop, foo())) |
---|
1115 | n/a | |
---|
1116 | n/a | def test_as_completed_reverse_wait(self): |
---|
1117 | n/a | |
---|
1118 | n/a | def gen(): |
---|
1119 | n/a | yield 0 |
---|
1120 | n/a | yield 0.05 |
---|
1121 | n/a | yield 0 |
---|
1122 | n/a | |
---|
1123 | n/a | loop = self.new_test_loop(gen) |
---|
1124 | n/a | |
---|
1125 | n/a | a = asyncio.sleep(0.05, 'a', loop=loop) |
---|
1126 | n/a | b = asyncio.sleep(0.10, 'b', loop=loop) |
---|
1127 | n/a | fs = {a, b} |
---|
1128 | n/a | futs = list(asyncio.as_completed(fs, loop=loop)) |
---|
1129 | n/a | self.assertEqual(len(futs), 2) |
---|
1130 | n/a | |
---|
1131 | n/a | x = loop.run_until_complete(futs[1]) |
---|
1132 | n/a | self.assertEqual(x, 'a') |
---|
1133 | n/a | self.assertAlmostEqual(0.05, loop.time()) |
---|
1134 | n/a | loop.advance_time(0.05) |
---|
1135 | n/a | y = loop.run_until_complete(futs[0]) |
---|
1136 | n/a | self.assertEqual(y, 'b') |
---|
1137 | n/a | self.assertAlmostEqual(0.10, loop.time()) |
---|
1138 | n/a | |
---|
1139 | n/a | def test_as_completed_concurrent(self): |
---|
1140 | n/a | |
---|
1141 | n/a | def gen(): |
---|
1142 | n/a | when = yield |
---|
1143 | n/a | self.assertAlmostEqual(0.05, when) |
---|
1144 | n/a | when = yield 0 |
---|
1145 | n/a | self.assertAlmostEqual(0.05, when) |
---|
1146 | n/a | yield 0.05 |
---|
1147 | n/a | |
---|
1148 | n/a | loop = self.new_test_loop(gen) |
---|
1149 | n/a | |
---|
1150 | n/a | a = asyncio.sleep(0.05, 'a', loop=loop) |
---|
1151 | n/a | b = asyncio.sleep(0.05, 'b', loop=loop) |
---|
1152 | n/a | fs = {a, b} |
---|
1153 | n/a | futs = list(asyncio.as_completed(fs, loop=loop)) |
---|
1154 | n/a | self.assertEqual(len(futs), 2) |
---|
1155 | n/a | waiter = asyncio.wait(futs, loop=loop) |
---|
1156 | n/a | done, pending = loop.run_until_complete(waiter) |
---|
1157 | n/a | self.assertEqual(set(f.result() for f in done), {'a', 'b'}) |
---|
1158 | n/a | |
---|
1159 | n/a | def test_as_completed_duplicate_coroutines(self): |
---|
1160 | n/a | |
---|
1161 | n/a | @asyncio.coroutine |
---|
1162 | n/a | def coro(s): |
---|
1163 | n/a | return s |
---|
1164 | n/a | |
---|
1165 | n/a | @asyncio.coroutine |
---|
1166 | n/a | def runner(): |
---|
1167 | n/a | result = [] |
---|
1168 | n/a | c = coro('ham') |
---|
1169 | n/a | for f in asyncio.as_completed([c, c, coro('spam')], |
---|
1170 | n/a | loop=self.loop): |
---|
1171 | n/a | result.append((yield from f)) |
---|
1172 | n/a | return result |
---|
1173 | n/a | |
---|
1174 | n/a | fut = self.new_task(self.loop, runner()) |
---|
1175 | n/a | self.loop.run_until_complete(fut) |
---|
1176 | n/a | result = fut.result() |
---|
1177 | n/a | self.assertEqual(set(result), {'ham', 'spam'}) |
---|
1178 | n/a | self.assertEqual(len(result), 2) |
---|
1179 | n/a | |
---|
1180 | n/a | def test_sleep(self): |
---|
1181 | n/a | |
---|
1182 | n/a | def gen(): |
---|
1183 | n/a | when = yield |
---|
1184 | n/a | self.assertAlmostEqual(0.05, when) |
---|
1185 | n/a | when = yield 0.05 |
---|
1186 | n/a | self.assertAlmostEqual(0.1, when) |
---|
1187 | n/a | yield 0.05 |
---|
1188 | n/a | |
---|
1189 | n/a | loop = self.new_test_loop(gen) |
---|
1190 | n/a | |
---|
1191 | n/a | @asyncio.coroutine |
---|
1192 | n/a | def sleeper(dt, arg): |
---|
1193 | n/a | yield from asyncio.sleep(dt/2, loop=loop) |
---|
1194 | n/a | res = yield from asyncio.sleep(dt/2, arg, loop=loop) |
---|
1195 | n/a | return res |
---|
1196 | n/a | |
---|
1197 | n/a | t = self.new_task(loop, sleeper(0.1, 'yeah')) |
---|
1198 | n/a | loop.run_until_complete(t) |
---|
1199 | n/a | self.assertTrue(t.done()) |
---|
1200 | n/a | self.assertEqual(t.result(), 'yeah') |
---|
1201 | n/a | self.assertAlmostEqual(0.1, loop.time()) |
---|
1202 | n/a | |
---|
1203 | n/a | def test_sleep_cancel(self): |
---|
1204 | n/a | |
---|
1205 | n/a | def gen(): |
---|
1206 | n/a | when = yield |
---|
1207 | n/a | self.assertAlmostEqual(10.0, when) |
---|
1208 | n/a | yield 0 |
---|
1209 | n/a | |
---|
1210 | n/a | loop = self.new_test_loop(gen) |
---|
1211 | n/a | |
---|
1212 | n/a | t = self.new_task(loop, asyncio.sleep(10.0, 'yeah', loop=loop)) |
---|
1213 | n/a | |
---|
1214 | n/a | handle = None |
---|
1215 | n/a | orig_call_later = loop.call_later |
---|
1216 | n/a | |
---|
1217 | n/a | def call_later(delay, callback, *args): |
---|
1218 | n/a | nonlocal handle |
---|
1219 | n/a | handle = orig_call_later(delay, callback, *args) |
---|
1220 | n/a | return handle |
---|
1221 | n/a | |
---|
1222 | n/a | loop.call_later = call_later |
---|
1223 | n/a | test_utils.run_briefly(loop) |
---|
1224 | n/a | |
---|
1225 | n/a | self.assertFalse(handle._cancelled) |
---|
1226 | n/a | |
---|
1227 | n/a | t.cancel() |
---|
1228 | n/a | test_utils.run_briefly(loop) |
---|
1229 | n/a | self.assertTrue(handle._cancelled) |
---|
1230 | n/a | |
---|
1231 | n/a | def test_task_cancel_sleeping_task(self): |
---|
1232 | n/a | |
---|
1233 | n/a | def gen(): |
---|
1234 | n/a | when = yield |
---|
1235 | n/a | self.assertAlmostEqual(0.1, when) |
---|
1236 | n/a | when = yield 0 |
---|
1237 | n/a | self.assertAlmostEqual(5000, when) |
---|
1238 | n/a | yield 0.1 |
---|
1239 | n/a | |
---|
1240 | n/a | loop = self.new_test_loop(gen) |
---|
1241 | n/a | |
---|
1242 | n/a | @asyncio.coroutine |
---|
1243 | n/a | def sleep(dt): |
---|
1244 | n/a | yield from asyncio.sleep(dt, loop=loop) |
---|
1245 | n/a | |
---|
1246 | n/a | @asyncio.coroutine |
---|
1247 | n/a | def doit(): |
---|
1248 | n/a | sleeper = self.new_task(loop, sleep(5000)) |
---|
1249 | n/a | loop.call_later(0.1, sleeper.cancel) |
---|
1250 | n/a | try: |
---|
1251 | n/a | yield from sleeper |
---|
1252 | n/a | except asyncio.CancelledError: |
---|
1253 | n/a | return 'cancelled' |
---|
1254 | n/a | else: |
---|
1255 | n/a | return 'slept in' |
---|
1256 | n/a | |
---|
1257 | n/a | doer = doit() |
---|
1258 | n/a | self.assertEqual(loop.run_until_complete(doer), 'cancelled') |
---|
1259 | n/a | self.assertAlmostEqual(0.1, loop.time()) |
---|
1260 | n/a | |
---|
1261 | n/a | def test_task_cancel_waiter_future(self): |
---|
1262 | n/a | fut = self.new_future(self.loop) |
---|
1263 | n/a | |
---|
1264 | n/a | @asyncio.coroutine |
---|
1265 | n/a | def coro(): |
---|
1266 | n/a | yield from fut |
---|
1267 | n/a | |
---|
1268 | n/a | task = self.new_task(self.loop, coro()) |
---|
1269 | n/a | test_utils.run_briefly(self.loop) |
---|
1270 | n/a | self.assertIs(task._fut_waiter, fut) |
---|
1271 | n/a | |
---|
1272 | n/a | task.cancel() |
---|
1273 | n/a | test_utils.run_briefly(self.loop) |
---|
1274 | n/a | self.assertRaises( |
---|
1275 | n/a | asyncio.CancelledError, self.loop.run_until_complete, task) |
---|
1276 | n/a | self.assertIsNone(task._fut_waiter) |
---|
1277 | n/a | self.assertTrue(fut.cancelled()) |
---|
1278 | n/a | |
---|
1279 | n/a | def test_step_in_completed_task(self): |
---|
1280 | n/a | @asyncio.coroutine |
---|
1281 | n/a | def notmuch(): |
---|
1282 | n/a | return 'ko' |
---|
1283 | n/a | |
---|
1284 | n/a | gen = notmuch() |
---|
1285 | n/a | task = self.new_task(self.loop, gen) |
---|
1286 | n/a | task.set_result('ok') |
---|
1287 | n/a | |
---|
1288 | n/a | self.assertRaises(AssertionError, task._step) |
---|
1289 | n/a | gen.close() |
---|
1290 | n/a | |
---|
1291 | n/a | def test_step_result(self): |
---|
1292 | n/a | @asyncio.coroutine |
---|
1293 | n/a | def notmuch(): |
---|
1294 | n/a | yield None |
---|
1295 | n/a | yield 1 |
---|
1296 | n/a | return 'ko' |
---|
1297 | n/a | |
---|
1298 | n/a | self.assertRaises( |
---|
1299 | n/a | RuntimeError, self.loop.run_until_complete, notmuch()) |
---|
1300 | n/a | |
---|
1301 | n/a | def test_step_result_future(self): |
---|
1302 | n/a | # If coroutine returns future, task waits on this future. |
---|
1303 | n/a | |
---|
1304 | n/a | class Fut(asyncio.Future): |
---|
1305 | n/a | def __init__(self, *args, **kwds): |
---|
1306 | n/a | self.cb_added = False |
---|
1307 | n/a | super().__init__(*args, **kwds) |
---|
1308 | n/a | |
---|
1309 | n/a | def add_done_callback(self, fn): |
---|
1310 | n/a | self.cb_added = True |
---|
1311 | n/a | super().add_done_callback(fn) |
---|
1312 | n/a | |
---|
1313 | n/a | fut = Fut(loop=self.loop) |
---|
1314 | n/a | result = None |
---|
1315 | n/a | |
---|
1316 | n/a | @asyncio.coroutine |
---|
1317 | n/a | def wait_for_future(): |
---|
1318 | n/a | nonlocal result |
---|
1319 | n/a | result = yield from fut |
---|
1320 | n/a | |
---|
1321 | n/a | t = self.new_task(self.loop, wait_for_future()) |
---|
1322 | n/a | test_utils.run_briefly(self.loop) |
---|
1323 | n/a | self.assertTrue(fut.cb_added) |
---|
1324 | n/a | |
---|
1325 | n/a | res = object() |
---|
1326 | n/a | fut.set_result(res) |
---|
1327 | n/a | test_utils.run_briefly(self.loop) |
---|
1328 | n/a | self.assertIs(res, result) |
---|
1329 | n/a | self.assertTrue(t.done()) |
---|
1330 | n/a | self.assertIsNone(t.result()) |
---|
1331 | n/a | |
---|
1332 | n/a | def test_step_with_baseexception(self): |
---|
1333 | n/a | @asyncio.coroutine |
---|
1334 | n/a | def notmutch(): |
---|
1335 | n/a | raise BaseException() |
---|
1336 | n/a | |
---|
1337 | n/a | task = self.new_task(self.loop, notmutch()) |
---|
1338 | n/a | self.assertRaises(BaseException, task._step) |
---|
1339 | n/a | |
---|
1340 | n/a | self.assertTrue(task.done()) |
---|
1341 | n/a | self.assertIsInstance(task.exception(), BaseException) |
---|
1342 | n/a | |
---|
1343 | n/a | def test_baseexception_during_cancel(self): |
---|
1344 | n/a | |
---|
1345 | n/a | def gen(): |
---|
1346 | n/a | when = yield |
---|
1347 | n/a | self.assertAlmostEqual(10.0, when) |
---|
1348 | n/a | yield 0 |
---|
1349 | n/a | |
---|
1350 | n/a | loop = self.new_test_loop(gen) |
---|
1351 | n/a | |
---|
1352 | n/a | @asyncio.coroutine |
---|
1353 | n/a | def sleeper(): |
---|
1354 | n/a | yield from asyncio.sleep(10, loop=loop) |
---|
1355 | n/a | |
---|
1356 | n/a | base_exc = BaseException() |
---|
1357 | n/a | |
---|
1358 | n/a | @asyncio.coroutine |
---|
1359 | n/a | def notmutch(): |
---|
1360 | n/a | try: |
---|
1361 | n/a | yield from sleeper() |
---|
1362 | n/a | except asyncio.CancelledError: |
---|
1363 | n/a | raise base_exc |
---|
1364 | n/a | |
---|
1365 | n/a | task = self.new_task(loop, notmutch()) |
---|
1366 | n/a | test_utils.run_briefly(loop) |
---|
1367 | n/a | |
---|
1368 | n/a | task.cancel() |
---|
1369 | n/a | self.assertFalse(task.done()) |
---|
1370 | n/a | |
---|
1371 | n/a | self.assertRaises(BaseException, test_utils.run_briefly, loop) |
---|
1372 | n/a | |
---|
1373 | n/a | self.assertTrue(task.done()) |
---|
1374 | n/a | self.assertFalse(task.cancelled()) |
---|
1375 | n/a | self.assertIs(task.exception(), base_exc) |
---|
1376 | n/a | |
---|
1377 | n/a | def test_iscoroutinefunction(self): |
---|
1378 | n/a | def fn(): |
---|
1379 | n/a | pass |
---|
1380 | n/a | |
---|
1381 | n/a | self.assertFalse(asyncio.iscoroutinefunction(fn)) |
---|
1382 | n/a | |
---|
1383 | n/a | def fn1(): |
---|
1384 | n/a | yield |
---|
1385 | n/a | self.assertFalse(asyncio.iscoroutinefunction(fn1)) |
---|
1386 | n/a | |
---|
1387 | n/a | @asyncio.coroutine |
---|
1388 | n/a | def fn2(): |
---|
1389 | n/a | yield |
---|
1390 | n/a | self.assertTrue(asyncio.iscoroutinefunction(fn2)) |
---|
1391 | n/a | |
---|
1392 | n/a | self.assertFalse(asyncio.iscoroutinefunction(mock.Mock())) |
---|
1393 | n/a | |
---|
1394 | n/a | def test_yield_vs_yield_from(self): |
---|
1395 | n/a | fut = self.new_future(self.loop) |
---|
1396 | n/a | |
---|
1397 | n/a | @asyncio.coroutine |
---|
1398 | n/a | def wait_for_future(): |
---|
1399 | n/a | yield fut |
---|
1400 | n/a | |
---|
1401 | n/a | task = wait_for_future() |
---|
1402 | n/a | with self.assertRaises(RuntimeError): |
---|
1403 | n/a | self.loop.run_until_complete(task) |
---|
1404 | n/a | |
---|
1405 | n/a | self.assertFalse(fut.done()) |
---|
1406 | n/a | |
---|
1407 | n/a | def test_yield_vs_yield_from_generator(self): |
---|
1408 | n/a | @asyncio.coroutine |
---|
1409 | n/a | def coro(): |
---|
1410 | n/a | yield |
---|
1411 | n/a | |
---|
1412 | n/a | @asyncio.coroutine |
---|
1413 | n/a | def wait_for_future(): |
---|
1414 | n/a | gen = coro() |
---|
1415 | n/a | try: |
---|
1416 | n/a | yield gen |
---|
1417 | n/a | finally: |
---|
1418 | n/a | gen.close() |
---|
1419 | n/a | |
---|
1420 | n/a | task = wait_for_future() |
---|
1421 | n/a | self.assertRaises( |
---|
1422 | n/a | RuntimeError, |
---|
1423 | n/a | self.loop.run_until_complete, task) |
---|
1424 | n/a | |
---|
1425 | n/a | def test_coroutine_non_gen_function(self): |
---|
1426 | n/a | @asyncio.coroutine |
---|
1427 | n/a | def func(): |
---|
1428 | n/a | return 'test' |
---|
1429 | n/a | |
---|
1430 | n/a | self.assertTrue(asyncio.iscoroutinefunction(func)) |
---|
1431 | n/a | |
---|
1432 | n/a | coro = func() |
---|
1433 | n/a | self.assertTrue(asyncio.iscoroutine(coro)) |
---|
1434 | n/a | |
---|
1435 | n/a | res = self.loop.run_until_complete(coro) |
---|
1436 | n/a | self.assertEqual(res, 'test') |
---|
1437 | n/a | |
---|
1438 | n/a | def test_coroutine_non_gen_function_return_future(self): |
---|
1439 | n/a | fut = self.new_future(self.loop) |
---|
1440 | n/a | |
---|
1441 | n/a | @asyncio.coroutine |
---|
1442 | n/a | def func(): |
---|
1443 | n/a | return fut |
---|
1444 | n/a | |
---|
1445 | n/a | @asyncio.coroutine |
---|
1446 | n/a | def coro(): |
---|
1447 | n/a | fut.set_result('test') |
---|
1448 | n/a | |
---|
1449 | n/a | t1 = self.new_task(self.loop, func()) |
---|
1450 | n/a | t2 = self.new_task(self.loop, coro()) |
---|
1451 | n/a | res = self.loop.run_until_complete(t1) |
---|
1452 | n/a | self.assertEqual(res, 'test') |
---|
1453 | n/a | self.assertIsNone(t2.result()) |
---|
1454 | n/a | |
---|
1455 | n/a | def test_current_task(self): |
---|
1456 | n/a | Task = self.__class__.Task |
---|
1457 | n/a | |
---|
1458 | n/a | self.assertIsNone(Task.current_task(loop=self.loop)) |
---|
1459 | n/a | |
---|
1460 | n/a | @asyncio.coroutine |
---|
1461 | n/a | def coro(loop): |
---|
1462 | n/a | self.assertTrue(Task.current_task(loop=loop) is task) |
---|
1463 | n/a | |
---|
1464 | n/a | task = self.new_task(self.loop, coro(self.loop)) |
---|
1465 | n/a | self.loop.run_until_complete(task) |
---|
1466 | n/a | self.assertIsNone(Task.current_task(loop=self.loop)) |
---|
1467 | n/a | |
---|
1468 | n/a | def test_current_task_with_interleaving_tasks(self): |
---|
1469 | n/a | Task = self.__class__.Task |
---|
1470 | n/a | |
---|
1471 | n/a | self.assertIsNone(Task.current_task(loop=self.loop)) |
---|
1472 | n/a | |
---|
1473 | n/a | fut1 = self.new_future(self.loop) |
---|
1474 | n/a | fut2 = self.new_future(self.loop) |
---|
1475 | n/a | |
---|
1476 | n/a | @asyncio.coroutine |
---|
1477 | n/a | def coro1(loop): |
---|
1478 | n/a | self.assertTrue(Task.current_task(loop=loop) is task1) |
---|
1479 | n/a | yield from fut1 |
---|
1480 | n/a | self.assertTrue(Task.current_task(loop=loop) is task1) |
---|
1481 | n/a | fut2.set_result(True) |
---|
1482 | n/a | |
---|
1483 | n/a | @asyncio.coroutine |
---|
1484 | n/a | def coro2(loop): |
---|
1485 | n/a | self.assertTrue(Task.current_task(loop=loop) is task2) |
---|
1486 | n/a | fut1.set_result(True) |
---|
1487 | n/a | yield from fut2 |
---|
1488 | n/a | self.assertTrue(Task.current_task(loop=loop) is task2) |
---|
1489 | n/a | |
---|
1490 | n/a | task1 = self.new_task(self.loop, coro1(self.loop)) |
---|
1491 | n/a | task2 = self.new_task(self.loop, coro2(self.loop)) |
---|
1492 | n/a | |
---|
1493 | n/a | self.loop.run_until_complete(asyncio.wait((task1, task2), |
---|
1494 | n/a | loop=self.loop)) |
---|
1495 | n/a | self.assertIsNone(Task.current_task(loop=self.loop)) |
---|
1496 | n/a | |
---|
1497 | n/a | # Some thorough tests for cancellation propagation through |
---|
1498 | n/a | # coroutines, tasks and wait(). |
---|
1499 | n/a | |
---|
1500 | n/a | def test_yield_future_passes_cancel(self): |
---|
1501 | n/a | # Cancelling outer() cancels inner() cancels waiter. |
---|
1502 | n/a | proof = 0 |
---|
1503 | n/a | waiter = self.new_future(self.loop) |
---|
1504 | n/a | |
---|
1505 | n/a | @asyncio.coroutine |
---|
1506 | n/a | def inner(): |
---|
1507 | n/a | nonlocal proof |
---|
1508 | n/a | try: |
---|
1509 | n/a | yield from waiter |
---|
1510 | n/a | except asyncio.CancelledError: |
---|
1511 | n/a | proof += 1 |
---|
1512 | n/a | raise |
---|
1513 | n/a | else: |
---|
1514 | n/a | self.fail('got past sleep() in inner()') |
---|
1515 | n/a | |
---|
1516 | n/a | @asyncio.coroutine |
---|
1517 | n/a | def outer(): |
---|
1518 | n/a | nonlocal proof |
---|
1519 | n/a | try: |
---|
1520 | n/a | yield from inner() |
---|
1521 | n/a | except asyncio.CancelledError: |
---|
1522 | n/a | proof += 100 # Expect this path. |
---|
1523 | n/a | else: |
---|
1524 | n/a | proof += 10 |
---|
1525 | n/a | |
---|
1526 | n/a | f = asyncio.ensure_future(outer(), loop=self.loop) |
---|
1527 | n/a | test_utils.run_briefly(self.loop) |
---|
1528 | n/a | f.cancel() |
---|
1529 | n/a | self.loop.run_until_complete(f) |
---|
1530 | n/a | self.assertEqual(proof, 101) |
---|
1531 | n/a | self.assertTrue(waiter.cancelled()) |
---|
1532 | n/a | |
---|
1533 | n/a | def test_yield_wait_does_not_shield_cancel(self): |
---|
1534 | n/a | # Cancelling outer() makes wait() return early, leaves inner() |
---|
1535 | n/a | # running. |
---|
1536 | n/a | proof = 0 |
---|
1537 | n/a | waiter = self.new_future(self.loop) |
---|
1538 | n/a | |
---|
1539 | n/a | @asyncio.coroutine |
---|
1540 | n/a | def inner(): |
---|
1541 | n/a | nonlocal proof |
---|
1542 | n/a | yield from waiter |
---|
1543 | n/a | proof += 1 |
---|
1544 | n/a | |
---|
1545 | n/a | @asyncio.coroutine |
---|
1546 | n/a | def outer(): |
---|
1547 | n/a | nonlocal proof |
---|
1548 | n/a | d, p = yield from asyncio.wait([inner()], loop=self.loop) |
---|
1549 | n/a | proof += 100 |
---|
1550 | n/a | |
---|
1551 | n/a | f = asyncio.ensure_future(outer(), loop=self.loop) |
---|
1552 | n/a | test_utils.run_briefly(self.loop) |
---|
1553 | n/a | f.cancel() |
---|
1554 | n/a | self.assertRaises( |
---|
1555 | n/a | asyncio.CancelledError, self.loop.run_until_complete, f) |
---|
1556 | n/a | waiter.set_result(None) |
---|
1557 | n/a | test_utils.run_briefly(self.loop) |
---|
1558 | n/a | self.assertEqual(proof, 1) |
---|
1559 | n/a | |
---|
1560 | n/a | def test_shield_result(self): |
---|
1561 | n/a | inner = self.new_future(self.loop) |
---|
1562 | n/a | outer = asyncio.shield(inner) |
---|
1563 | n/a | inner.set_result(42) |
---|
1564 | n/a | res = self.loop.run_until_complete(outer) |
---|
1565 | n/a | self.assertEqual(res, 42) |
---|
1566 | n/a | |
---|
1567 | n/a | def test_shield_exception(self): |
---|
1568 | n/a | inner = self.new_future(self.loop) |
---|
1569 | n/a | outer = asyncio.shield(inner) |
---|
1570 | n/a | test_utils.run_briefly(self.loop) |
---|
1571 | n/a | exc = RuntimeError('expected') |
---|
1572 | n/a | inner.set_exception(exc) |
---|
1573 | n/a | test_utils.run_briefly(self.loop) |
---|
1574 | n/a | self.assertIs(outer.exception(), exc) |
---|
1575 | n/a | |
---|
1576 | n/a | def test_shield_cancel(self): |
---|
1577 | n/a | inner = self.new_future(self.loop) |
---|
1578 | n/a | outer = asyncio.shield(inner) |
---|
1579 | n/a | test_utils.run_briefly(self.loop) |
---|
1580 | n/a | inner.cancel() |
---|
1581 | n/a | test_utils.run_briefly(self.loop) |
---|
1582 | n/a | self.assertTrue(outer.cancelled()) |
---|
1583 | n/a | |
---|
1584 | n/a | def test_shield_shortcut(self): |
---|
1585 | n/a | fut = self.new_future(self.loop) |
---|
1586 | n/a | fut.set_result(42) |
---|
1587 | n/a | res = self.loop.run_until_complete(asyncio.shield(fut)) |
---|
1588 | n/a | self.assertEqual(res, 42) |
---|
1589 | n/a | |
---|
1590 | n/a | def test_shield_effect(self): |
---|
1591 | n/a | # Cancelling outer() does not affect inner(). |
---|
1592 | n/a | proof = 0 |
---|
1593 | n/a | waiter = self.new_future(self.loop) |
---|
1594 | n/a | |
---|
1595 | n/a | @asyncio.coroutine |
---|
1596 | n/a | def inner(): |
---|
1597 | n/a | nonlocal proof |
---|
1598 | n/a | yield from waiter |
---|
1599 | n/a | proof += 1 |
---|
1600 | n/a | |
---|
1601 | n/a | @asyncio.coroutine |
---|
1602 | n/a | def outer(): |
---|
1603 | n/a | nonlocal proof |
---|
1604 | n/a | yield from asyncio.shield(inner(), loop=self.loop) |
---|
1605 | n/a | proof += 100 |
---|
1606 | n/a | |
---|
1607 | n/a | f = asyncio.ensure_future(outer(), loop=self.loop) |
---|
1608 | n/a | test_utils.run_briefly(self.loop) |
---|
1609 | n/a | f.cancel() |
---|
1610 | n/a | with self.assertRaises(asyncio.CancelledError): |
---|
1611 | n/a | self.loop.run_until_complete(f) |
---|
1612 | n/a | waiter.set_result(None) |
---|
1613 | n/a | test_utils.run_briefly(self.loop) |
---|
1614 | n/a | self.assertEqual(proof, 1) |
---|
1615 | n/a | |
---|
1616 | n/a | def test_shield_gather(self): |
---|
1617 | n/a | child1 = self.new_future(self.loop) |
---|
1618 | n/a | child2 = self.new_future(self.loop) |
---|
1619 | n/a | parent = asyncio.gather(child1, child2, loop=self.loop) |
---|
1620 | n/a | outer = asyncio.shield(parent, loop=self.loop) |
---|
1621 | n/a | test_utils.run_briefly(self.loop) |
---|
1622 | n/a | outer.cancel() |
---|
1623 | n/a | test_utils.run_briefly(self.loop) |
---|
1624 | n/a | self.assertTrue(outer.cancelled()) |
---|
1625 | n/a | child1.set_result(1) |
---|
1626 | n/a | child2.set_result(2) |
---|
1627 | n/a | test_utils.run_briefly(self.loop) |
---|
1628 | n/a | self.assertEqual(parent.result(), [1, 2]) |
---|
1629 | n/a | |
---|
1630 | n/a | def test_gather_shield(self): |
---|
1631 | n/a | child1 = self.new_future(self.loop) |
---|
1632 | n/a | child2 = self.new_future(self.loop) |
---|
1633 | n/a | inner1 = asyncio.shield(child1, loop=self.loop) |
---|
1634 | n/a | inner2 = asyncio.shield(child2, loop=self.loop) |
---|
1635 | n/a | parent = asyncio.gather(inner1, inner2, loop=self.loop) |
---|
1636 | n/a | test_utils.run_briefly(self.loop) |
---|
1637 | n/a | parent.cancel() |
---|
1638 | n/a | # This should cancel inner1 and inner2 but bot child1 and child2. |
---|
1639 | n/a | test_utils.run_briefly(self.loop) |
---|
1640 | n/a | self.assertIsInstance(parent.exception(), asyncio.CancelledError) |
---|
1641 | n/a | self.assertTrue(inner1.cancelled()) |
---|
1642 | n/a | self.assertTrue(inner2.cancelled()) |
---|
1643 | n/a | child1.set_result(1) |
---|
1644 | n/a | child2.set_result(2) |
---|
1645 | n/a | test_utils.run_briefly(self.loop) |
---|
1646 | n/a | |
---|
1647 | n/a | def test_as_completed_invalid_args(self): |
---|
1648 | n/a | fut = self.new_future(self.loop) |
---|
1649 | n/a | |
---|
1650 | n/a | # as_completed() expects a list of futures, not a future instance |
---|
1651 | n/a | self.assertRaises(TypeError, self.loop.run_until_complete, |
---|
1652 | n/a | asyncio.as_completed(fut, loop=self.loop)) |
---|
1653 | n/a | coro = coroutine_function() |
---|
1654 | n/a | self.assertRaises(TypeError, self.loop.run_until_complete, |
---|
1655 | n/a | asyncio.as_completed(coro, loop=self.loop)) |
---|
1656 | n/a | coro.close() |
---|
1657 | n/a | |
---|
1658 | n/a | def test_wait_invalid_args(self): |
---|
1659 | n/a | fut = self.new_future(self.loop) |
---|
1660 | n/a | |
---|
1661 | n/a | # wait() expects a list of futures, not a future instance |
---|
1662 | n/a | self.assertRaises(TypeError, self.loop.run_until_complete, |
---|
1663 | n/a | asyncio.wait(fut, loop=self.loop)) |
---|
1664 | n/a | coro = coroutine_function() |
---|
1665 | n/a | self.assertRaises(TypeError, self.loop.run_until_complete, |
---|
1666 | n/a | asyncio.wait(coro, loop=self.loop)) |
---|
1667 | n/a | coro.close() |
---|
1668 | n/a | |
---|
1669 | n/a | # wait() expects at least a future |
---|
1670 | n/a | self.assertRaises(ValueError, self.loop.run_until_complete, |
---|
1671 | n/a | asyncio.wait([], loop=self.loop)) |
---|
1672 | n/a | |
---|
1673 | n/a | def test_corowrapper_mocks_generator(self): |
---|
1674 | n/a | |
---|
1675 | n/a | def check(): |
---|
1676 | n/a | # A function that asserts various things. |
---|
1677 | n/a | # Called twice, with different debug flag values. |
---|
1678 | n/a | |
---|
1679 | n/a | @asyncio.coroutine |
---|
1680 | n/a | def coro(): |
---|
1681 | n/a | # The actual coroutine. |
---|
1682 | n/a | self.assertTrue(gen.gi_running) |
---|
1683 | n/a | yield from fut |
---|
1684 | n/a | |
---|
1685 | n/a | # A completed Future used to run the coroutine. |
---|
1686 | n/a | fut = self.new_future(self.loop) |
---|
1687 | n/a | fut.set_result(None) |
---|
1688 | n/a | |
---|
1689 | n/a | # Call the coroutine. |
---|
1690 | n/a | gen = coro() |
---|
1691 | n/a | |
---|
1692 | n/a | # Check some properties. |
---|
1693 | n/a | self.assertTrue(asyncio.iscoroutine(gen)) |
---|
1694 | n/a | self.assertIsInstance(gen.gi_frame, types.FrameType) |
---|
1695 | n/a | self.assertFalse(gen.gi_running) |
---|
1696 | n/a | self.assertIsInstance(gen.gi_code, types.CodeType) |
---|
1697 | n/a | |
---|
1698 | n/a | # Run it. |
---|
1699 | n/a | self.loop.run_until_complete(gen) |
---|
1700 | n/a | |
---|
1701 | n/a | # The frame should have changed. |
---|
1702 | n/a | self.assertIsNone(gen.gi_frame) |
---|
1703 | n/a | |
---|
1704 | n/a | # Test with debug flag cleared. |
---|
1705 | n/a | with set_coroutine_debug(False): |
---|
1706 | n/a | check() |
---|
1707 | n/a | |
---|
1708 | n/a | # Test with debug flag set. |
---|
1709 | n/a | with set_coroutine_debug(True): |
---|
1710 | n/a | check() |
---|
1711 | n/a | |
---|
1712 | n/a | def test_yield_from_corowrapper(self): |
---|
1713 | n/a | with set_coroutine_debug(True): |
---|
1714 | n/a | @asyncio.coroutine |
---|
1715 | n/a | def t1(): |
---|
1716 | n/a | return (yield from t2()) |
---|
1717 | n/a | |
---|
1718 | n/a | @asyncio.coroutine |
---|
1719 | n/a | def t2(): |
---|
1720 | n/a | f = self.new_future(self.loop) |
---|
1721 | n/a | self.new_task(self.loop, t3(f)) |
---|
1722 | n/a | return (yield from f) |
---|
1723 | n/a | |
---|
1724 | n/a | @asyncio.coroutine |
---|
1725 | n/a | def t3(f): |
---|
1726 | n/a | f.set_result((1, 2, 3)) |
---|
1727 | n/a | |
---|
1728 | n/a | task = self.new_task(self.loop, t1()) |
---|
1729 | n/a | val = self.loop.run_until_complete(task) |
---|
1730 | n/a | self.assertEqual(val, (1, 2, 3)) |
---|
1731 | n/a | |
---|
1732 | n/a | def test_yield_from_corowrapper_send(self): |
---|
1733 | n/a | def foo(): |
---|
1734 | n/a | a = yield |
---|
1735 | n/a | return a |
---|
1736 | n/a | |
---|
1737 | n/a | def call(arg): |
---|
1738 | n/a | cw = asyncio.coroutines.CoroWrapper(foo()) |
---|
1739 | n/a | cw.send(None) |
---|
1740 | n/a | try: |
---|
1741 | n/a | cw.send(arg) |
---|
1742 | n/a | except StopIteration as ex: |
---|
1743 | n/a | return ex.args[0] |
---|
1744 | n/a | else: |
---|
1745 | n/a | raise AssertionError('StopIteration was expected') |
---|
1746 | n/a | |
---|
1747 | n/a | self.assertEqual(call((1, 2)), (1, 2)) |
---|
1748 | n/a | self.assertEqual(call('spam'), 'spam') |
---|
1749 | n/a | |
---|
1750 | n/a | def test_corowrapper_weakref(self): |
---|
1751 | n/a | wd = weakref.WeakValueDictionary() |
---|
1752 | n/a | def foo(): yield from [] |
---|
1753 | n/a | cw = asyncio.coroutines.CoroWrapper(foo()) |
---|
1754 | n/a | wd['cw'] = cw # Would fail without __weakref__ slot. |
---|
1755 | n/a | cw.gen = None # Suppress warning from __del__. |
---|
1756 | n/a | |
---|
1757 | n/a | def test_corowrapper_throw(self): |
---|
1758 | n/a | # Issue 429: CoroWrapper.throw must be compatible with gen.throw |
---|
1759 | n/a | def foo(): |
---|
1760 | n/a | value = None |
---|
1761 | n/a | while True: |
---|
1762 | n/a | try: |
---|
1763 | n/a | value = yield value |
---|
1764 | n/a | except Exception as e: |
---|
1765 | n/a | value = e |
---|
1766 | n/a | |
---|
1767 | n/a | exception = Exception("foo") |
---|
1768 | n/a | cw = asyncio.coroutines.CoroWrapper(foo()) |
---|
1769 | n/a | cw.send(None) |
---|
1770 | n/a | self.assertIs(exception, cw.throw(exception)) |
---|
1771 | n/a | |
---|
1772 | n/a | cw = asyncio.coroutines.CoroWrapper(foo()) |
---|
1773 | n/a | cw.send(None) |
---|
1774 | n/a | self.assertIs(exception, cw.throw(Exception, exception)) |
---|
1775 | n/a | |
---|
1776 | n/a | cw = asyncio.coroutines.CoroWrapper(foo()) |
---|
1777 | n/a | cw.send(None) |
---|
1778 | n/a | exception = cw.throw(Exception, "foo") |
---|
1779 | n/a | self.assertIsInstance(exception, Exception) |
---|
1780 | n/a | self.assertEqual(exception.args, ("foo", )) |
---|
1781 | n/a | |
---|
1782 | n/a | cw = asyncio.coroutines.CoroWrapper(foo()) |
---|
1783 | n/a | cw.send(None) |
---|
1784 | n/a | exception = cw.throw(Exception, "foo", None) |
---|
1785 | n/a | self.assertIsInstance(exception, Exception) |
---|
1786 | n/a | self.assertEqual(exception.args, ("foo", )) |
---|
1787 | n/a | |
---|
1788 | n/a | @unittest.skipUnless(PY34, |
---|
1789 | n/a | 'need python 3.4 or later') |
---|
1790 | n/a | def test_log_destroyed_pending_task(self): |
---|
1791 | n/a | Task = self.__class__.Task |
---|
1792 | n/a | |
---|
1793 | n/a | @asyncio.coroutine |
---|
1794 | n/a | def kill_me(loop): |
---|
1795 | n/a | future = self.new_future(loop) |
---|
1796 | n/a | yield from future |
---|
1797 | n/a | # at this point, the only reference to kill_me() task is |
---|
1798 | n/a | # the Task._wakeup() method in future._callbacks |
---|
1799 | n/a | raise Exception("code never reached") |
---|
1800 | n/a | |
---|
1801 | n/a | mock_handler = mock.Mock() |
---|
1802 | n/a | self.loop.set_debug(True) |
---|
1803 | n/a | self.loop.set_exception_handler(mock_handler) |
---|
1804 | n/a | |
---|
1805 | n/a | # schedule the task |
---|
1806 | n/a | coro = kill_me(self.loop) |
---|
1807 | n/a | task = asyncio.ensure_future(coro, loop=self.loop) |
---|
1808 | n/a | self.assertEqual(Task.all_tasks(loop=self.loop), {task}) |
---|
1809 | n/a | |
---|
1810 | n/a | # execute the task so it waits for future |
---|
1811 | n/a | self.loop._run_once() |
---|
1812 | n/a | self.assertEqual(len(self.loop._ready), 0) |
---|
1813 | n/a | |
---|
1814 | n/a | # remove the future used in kill_me(), and references to the task |
---|
1815 | n/a | del coro.gi_frame.f_locals['future'] |
---|
1816 | n/a | coro = None |
---|
1817 | n/a | source_traceback = task._source_traceback |
---|
1818 | n/a | task = None |
---|
1819 | n/a | |
---|
1820 | n/a | # no more reference to kill_me() task: the task is destroyed by the GC |
---|
1821 | n/a | support.gc_collect() |
---|
1822 | n/a | |
---|
1823 | n/a | self.assertEqual(Task.all_tasks(loop=self.loop), set()) |
---|
1824 | n/a | |
---|
1825 | n/a | mock_handler.assert_called_with(self.loop, { |
---|
1826 | n/a | 'message': 'Task was destroyed but it is pending!', |
---|
1827 | n/a | 'task': mock.ANY, |
---|
1828 | n/a | 'source_traceback': source_traceback, |
---|
1829 | n/a | }) |
---|
1830 | n/a | mock_handler.reset_mock() |
---|
1831 | n/a | |
---|
1832 | n/a | @mock.patch('asyncio.coroutines.logger') |
---|
1833 | n/a | def test_coroutine_never_yielded(self, m_log): |
---|
1834 | n/a | with set_coroutine_debug(True): |
---|
1835 | n/a | @asyncio.coroutine |
---|
1836 | n/a | def coro_noop(): |
---|
1837 | n/a | pass |
---|
1838 | n/a | |
---|
1839 | n/a | tb_filename = __file__ |
---|
1840 | n/a | tb_lineno = sys._getframe().f_lineno + 2 |
---|
1841 | n/a | # create a coroutine object but don't use it |
---|
1842 | n/a | coro_noop() |
---|
1843 | n/a | support.gc_collect() |
---|
1844 | n/a | |
---|
1845 | n/a | self.assertTrue(m_log.error.called) |
---|
1846 | n/a | message = m_log.error.call_args[0][0] |
---|
1847 | n/a | func_filename, func_lineno = test_utils.get_function_source(coro_noop) |
---|
1848 | n/a | |
---|
1849 | n/a | regex = (r'^<CoroWrapper %s\(?\)? .* at %s:%s, .*> ' |
---|
1850 | n/a | r'was never yielded from\n' |
---|
1851 | n/a | r'Coroutine object created at \(most recent call last\):\n' |
---|
1852 | n/a | r'.*\n' |
---|
1853 | n/a | r' File "%s", line %s, in test_coroutine_never_yielded\n' |
---|
1854 | n/a | r' coro_noop\(\)$' |
---|
1855 | n/a | % (re.escape(coro_noop.__qualname__), |
---|
1856 | n/a | re.escape(func_filename), func_lineno, |
---|
1857 | n/a | re.escape(tb_filename), tb_lineno)) |
---|
1858 | n/a | |
---|
1859 | n/a | self.assertRegex(message, re.compile(regex, re.DOTALL)) |
---|
1860 | n/a | |
---|
1861 | n/a | def test_return_coroutine_from_coroutine(self): |
---|
1862 | n/a | """Return of @asyncio.coroutine()-wrapped function generator object |
---|
1863 | n/a | from @asyncio.coroutine()-wrapped function should have same effect as |
---|
1864 | n/a | returning generator object or Future.""" |
---|
1865 | n/a | def check(): |
---|
1866 | n/a | @asyncio.coroutine |
---|
1867 | n/a | def outer_coro(): |
---|
1868 | n/a | @asyncio.coroutine |
---|
1869 | n/a | def inner_coro(): |
---|
1870 | n/a | return 1 |
---|
1871 | n/a | |
---|
1872 | n/a | return inner_coro() |
---|
1873 | n/a | |
---|
1874 | n/a | result = self.loop.run_until_complete(outer_coro()) |
---|
1875 | n/a | self.assertEqual(result, 1) |
---|
1876 | n/a | |
---|
1877 | n/a | # Test with debug flag cleared. |
---|
1878 | n/a | with set_coroutine_debug(False): |
---|
1879 | n/a | check() |
---|
1880 | n/a | |
---|
1881 | n/a | # Test with debug flag set. |
---|
1882 | n/a | with set_coroutine_debug(True): |
---|
1883 | n/a | check() |
---|
1884 | n/a | |
---|
1885 | n/a | def test_task_source_traceback(self): |
---|
1886 | n/a | self.loop.set_debug(True) |
---|
1887 | n/a | |
---|
1888 | n/a | task = self.new_task(self.loop, coroutine_function()) |
---|
1889 | n/a | lineno = sys._getframe().f_lineno - 1 |
---|
1890 | n/a | self.assertIsInstance(task._source_traceback, list) |
---|
1891 | n/a | self.assertEqual(task._source_traceback[-2][:3], |
---|
1892 | n/a | (__file__, |
---|
1893 | n/a | lineno, |
---|
1894 | n/a | 'test_task_source_traceback')) |
---|
1895 | n/a | self.loop.run_until_complete(task) |
---|
1896 | n/a | |
---|
1897 | n/a | def _test_cancel_wait_for(self, timeout): |
---|
1898 | n/a | loop = asyncio.new_event_loop() |
---|
1899 | n/a | self.addCleanup(loop.close) |
---|
1900 | n/a | |
---|
1901 | n/a | @asyncio.coroutine |
---|
1902 | n/a | def blocking_coroutine(): |
---|
1903 | n/a | fut = self.new_future(loop) |
---|
1904 | n/a | # Block: fut result is never set |
---|
1905 | n/a | yield from fut |
---|
1906 | n/a | |
---|
1907 | n/a | task = loop.create_task(blocking_coroutine()) |
---|
1908 | n/a | |
---|
1909 | n/a | wait = loop.create_task(asyncio.wait_for(task, timeout, loop=loop)) |
---|
1910 | n/a | loop.call_soon(wait.cancel) |
---|
1911 | n/a | |
---|
1912 | n/a | self.assertRaises(asyncio.CancelledError, |
---|
1913 | n/a | loop.run_until_complete, wait) |
---|
1914 | n/a | |
---|
1915 | n/a | # Python issue #23219: cancelling the wait must also cancel the task |
---|
1916 | n/a | self.assertTrue(task.cancelled()) |
---|
1917 | n/a | |
---|
1918 | n/a | def test_cancel_blocking_wait_for(self): |
---|
1919 | n/a | self._test_cancel_wait_for(None) |
---|
1920 | n/a | |
---|
1921 | n/a | def test_cancel_wait_for(self): |
---|
1922 | n/a | self._test_cancel_wait_for(60.0) |
---|
1923 | n/a | |
---|
1924 | n/a | def test_cancel_gather(self): |
---|
1925 | n/a | """Ensure that a gathering future refuses to be cancelled once all |
---|
1926 | n/a | children are done""" |
---|
1927 | n/a | loop = asyncio.new_event_loop() |
---|
1928 | n/a | self.addCleanup(loop.close) |
---|
1929 | n/a | |
---|
1930 | n/a | fut = self.new_future(loop) |
---|
1931 | n/a | # The indirection fut->child_coro is needed since otherwise the |
---|
1932 | n/a | # gathering task is done at the same time as the child future |
---|
1933 | n/a | def child_coro(): |
---|
1934 | n/a | return (yield from fut) |
---|
1935 | n/a | gather_future = asyncio.gather(child_coro(), loop=loop) |
---|
1936 | n/a | gather_task = asyncio.ensure_future(gather_future, loop=loop) |
---|
1937 | n/a | |
---|
1938 | n/a | cancel_result = None |
---|
1939 | n/a | def cancelling_callback(_): |
---|
1940 | n/a | nonlocal cancel_result |
---|
1941 | n/a | cancel_result = gather_task.cancel() |
---|
1942 | n/a | fut.add_done_callback(cancelling_callback) |
---|
1943 | n/a | |
---|
1944 | n/a | fut.set_result(42) # calls the cancelling_callback after fut is done() |
---|
1945 | n/a | |
---|
1946 | n/a | # At this point the task should complete. |
---|
1947 | n/a | loop.run_until_complete(gather_task) |
---|
1948 | n/a | |
---|
1949 | n/a | # Python issue #26923: asyncio.gather drops cancellation |
---|
1950 | n/a | self.assertEqual(cancel_result, False) |
---|
1951 | n/a | self.assertFalse(gather_task.cancelled()) |
---|
1952 | n/a | self.assertEqual(gather_task.result(), [42]) |
---|
1953 | n/a | |
---|
1954 | n/a | def test_exception_traceback(self): |
---|
1955 | n/a | # See http://bugs.python.org/issue28843 |
---|
1956 | n/a | |
---|
1957 | n/a | @asyncio.coroutine |
---|
1958 | n/a | def foo(): |
---|
1959 | n/a | 1 / 0 |
---|
1960 | n/a | |
---|
1961 | n/a | @asyncio.coroutine |
---|
1962 | n/a | def main(): |
---|
1963 | n/a | task = self.new_task(self.loop, foo()) |
---|
1964 | n/a | yield # skip one loop iteration |
---|
1965 | n/a | self.assertIsNotNone(task.exception().__traceback__) |
---|
1966 | n/a | |
---|
1967 | n/a | self.loop.run_until_complete(main()) |
---|
1968 | n/a | |
---|
1969 | n/a | @mock.patch('asyncio.base_events.logger') |
---|
1970 | n/a | def test_error_in_call_soon(self, m_log): |
---|
1971 | n/a | def call_soon(callback, *args): |
---|
1972 | n/a | raise ValueError |
---|
1973 | n/a | self.loop.call_soon = call_soon |
---|
1974 | n/a | |
---|
1975 | n/a | @asyncio.coroutine |
---|
1976 | n/a | def coro(): |
---|
1977 | n/a | pass |
---|
1978 | n/a | |
---|
1979 | n/a | self.assertFalse(m_log.error.called) |
---|
1980 | n/a | |
---|
1981 | n/a | with self.assertRaises(ValueError): |
---|
1982 | n/a | self.new_task(self.loop, coro()) |
---|
1983 | n/a | |
---|
1984 | n/a | self.assertTrue(m_log.error.called) |
---|
1985 | n/a | message = m_log.error.call_args[0][0] |
---|
1986 | n/a | self.assertIn('Task was destroyed but it is pending', message) |
---|
1987 | n/a | |
---|
1988 | n/a | self.assertEqual(self.Task.all_tasks(self.loop), set()) |
---|
1989 | n/a | |
---|
1990 | n/a | |
---|
1991 | n/a | def add_subclass_tests(cls): |
---|
1992 | n/a | BaseTask = cls.Task |
---|
1993 | n/a | BaseFuture = cls.Future |
---|
1994 | n/a | |
---|
1995 | n/a | if BaseTask is None or BaseFuture is None: |
---|
1996 | n/a | return cls |
---|
1997 | n/a | |
---|
1998 | n/a | class CommonFuture: |
---|
1999 | n/a | def __init__(self, *args, **kwargs): |
---|
2000 | n/a | self.calls = collections.defaultdict(lambda: 0) |
---|
2001 | n/a | super().__init__(*args, **kwargs) |
---|
2002 | n/a | |
---|
2003 | n/a | def _schedule_callbacks(self): |
---|
2004 | n/a | self.calls['_schedule_callbacks'] += 1 |
---|
2005 | n/a | return super()._schedule_callbacks() |
---|
2006 | n/a | |
---|
2007 | n/a | def add_done_callback(self, *args): |
---|
2008 | n/a | self.calls['add_done_callback'] += 1 |
---|
2009 | n/a | return super().add_done_callback(*args) |
---|
2010 | n/a | |
---|
2011 | n/a | class Task(CommonFuture, BaseTask): |
---|
2012 | n/a | def _step(self, *args): |
---|
2013 | n/a | self.calls['_step'] += 1 |
---|
2014 | n/a | return super()._step(*args) |
---|
2015 | n/a | |
---|
2016 | n/a | def _wakeup(self, *args): |
---|
2017 | n/a | self.calls['_wakeup'] += 1 |
---|
2018 | n/a | return super()._wakeup(*args) |
---|
2019 | n/a | |
---|
2020 | n/a | class Future(CommonFuture, BaseFuture): |
---|
2021 | n/a | pass |
---|
2022 | n/a | |
---|
2023 | n/a | def test_subclasses_ctask_cfuture(self): |
---|
2024 | n/a | fut = self.Future(loop=self.loop) |
---|
2025 | n/a | |
---|
2026 | n/a | async def func(): |
---|
2027 | n/a | self.loop.call_soon(lambda: fut.set_result('spam')) |
---|
2028 | n/a | return await fut |
---|
2029 | n/a | |
---|
2030 | n/a | task = self.Task(func(), loop=self.loop) |
---|
2031 | n/a | |
---|
2032 | n/a | result = self.loop.run_until_complete(task) |
---|
2033 | n/a | |
---|
2034 | n/a | self.assertEqual(result, 'spam') |
---|
2035 | n/a | |
---|
2036 | n/a | self.assertEqual( |
---|
2037 | n/a | dict(task.calls), |
---|
2038 | n/a | {'_step': 2, '_wakeup': 1, 'add_done_callback': 1, |
---|
2039 | n/a | '_schedule_callbacks': 1}) |
---|
2040 | n/a | |
---|
2041 | n/a | self.assertEqual( |
---|
2042 | n/a | dict(fut.calls), |
---|
2043 | n/a | {'add_done_callback': 1, '_schedule_callbacks': 1}) |
---|
2044 | n/a | |
---|
2045 | n/a | # Add patched Task & Future back to the test case |
---|
2046 | n/a | cls.Task = Task |
---|
2047 | n/a | cls.Future = Future |
---|
2048 | n/a | |
---|
2049 | n/a | # Add an extra unit-test |
---|
2050 | n/a | cls.test_subclasses_ctask_cfuture = test_subclasses_ctask_cfuture |
---|
2051 | n/a | |
---|
2052 | n/a | # Disable the "test_task_source_traceback" test |
---|
2053 | n/a | # (the test is hardcoded for a particular call stack, which |
---|
2054 | n/a | # is slightly different for Task subclasses) |
---|
2055 | n/a | cls.test_task_source_traceback = None |
---|
2056 | n/a | |
---|
2057 | n/a | return cls |
---|
2058 | n/a | |
---|
2059 | n/a | |
---|
2060 | n/a | @unittest.skipUnless(hasattr(futures, '_CFuture'), |
---|
2061 | n/a | 'requires the C _asyncio module') |
---|
2062 | n/a | class CTask_CFuture_Tests(BaseTaskTests, test_utils.TestCase): |
---|
2063 | n/a | Task = getattr(tasks, '_CTask', None) |
---|
2064 | n/a | Future = getattr(futures, '_CFuture', None) |
---|
2065 | n/a | |
---|
2066 | n/a | |
---|
2067 | n/a | @unittest.skipUnless(hasattr(futures, '_CFuture'), |
---|
2068 | n/a | 'requires the C _asyncio module') |
---|
2069 | n/a | @add_subclass_tests |
---|
2070 | n/a | class CTask_CFuture_SubclassTests(BaseTaskTests, test_utils.TestCase): |
---|
2071 | n/a | Task = getattr(tasks, '_CTask', None) |
---|
2072 | n/a | Future = getattr(futures, '_CFuture', None) |
---|
2073 | n/a | |
---|
2074 | n/a | |
---|
2075 | n/a | @unittest.skipUnless(hasattr(futures, '_CFuture'), |
---|
2076 | n/a | 'requires the C _asyncio module') |
---|
2077 | n/a | class CTask_PyFuture_Tests(BaseTaskTests, test_utils.TestCase): |
---|
2078 | n/a | Task = getattr(tasks, '_CTask', None) |
---|
2079 | n/a | Future = futures._PyFuture |
---|
2080 | n/a | |
---|
2081 | n/a | |
---|
2082 | n/a | @unittest.skipUnless(hasattr(futures, '_CFuture'), |
---|
2083 | n/a | 'requires the C _asyncio module') |
---|
2084 | n/a | class PyTask_CFuture_Tests(BaseTaskTests, test_utils.TestCase): |
---|
2085 | n/a | Task = tasks._PyTask |
---|
2086 | n/a | Future = getattr(futures, '_CFuture', None) |
---|
2087 | n/a | |
---|
2088 | n/a | |
---|
2089 | n/a | class PyTask_PyFuture_Tests(BaseTaskTests, test_utils.TestCase): |
---|
2090 | n/a | Task = tasks._PyTask |
---|
2091 | n/a | Future = futures._PyFuture |
---|
2092 | n/a | |
---|
2093 | n/a | |
---|
2094 | n/a | @add_subclass_tests |
---|
2095 | n/a | class PyTask_PyFuture_SubclassTests(BaseTaskTests, test_utils.TestCase): |
---|
2096 | n/a | Task = tasks._PyTask |
---|
2097 | n/a | Future = futures._PyFuture |
---|
2098 | n/a | |
---|
2099 | n/a | |
---|
2100 | n/a | class GenericTaskTests(test_utils.TestCase): |
---|
2101 | n/a | |
---|
2102 | n/a | def test_future_subclass(self): |
---|
2103 | n/a | self.assertTrue(issubclass(asyncio.Task, asyncio.Future)) |
---|
2104 | n/a | |
---|
2105 | n/a | def test_asyncio_module_compiled(self): |
---|
2106 | n/a | # Because of circular imports it's easy to make _asyncio |
---|
2107 | n/a | # module non-importable. This is a simple test that will |
---|
2108 | n/a | # fail on systems where C modules were successfully compiled |
---|
2109 | n/a | # (hence the test for _functools), but _asyncio somehow didn't. |
---|
2110 | n/a | try: |
---|
2111 | n/a | import _functools |
---|
2112 | n/a | except ImportError: |
---|
2113 | n/a | pass |
---|
2114 | n/a | else: |
---|
2115 | n/a | try: |
---|
2116 | n/a | import _asyncio |
---|
2117 | n/a | except ImportError: |
---|
2118 | n/a | self.fail('_asyncio module is missing') |
---|
2119 | n/a | |
---|
2120 | n/a | |
---|
2121 | n/a | class GatherTestsBase: |
---|
2122 | n/a | |
---|
2123 | n/a | def setUp(self): |
---|
2124 | n/a | super().setUp() |
---|
2125 | n/a | self.one_loop = self.new_test_loop() |
---|
2126 | n/a | self.other_loop = self.new_test_loop() |
---|
2127 | n/a | self.set_event_loop(self.one_loop, cleanup=False) |
---|
2128 | n/a | |
---|
2129 | n/a | def _run_loop(self, loop): |
---|
2130 | n/a | while loop._ready: |
---|
2131 | n/a | test_utils.run_briefly(loop) |
---|
2132 | n/a | |
---|
2133 | n/a | def _check_success(self, **kwargs): |
---|
2134 | n/a | a, b, c = [asyncio.Future(loop=self.one_loop) for i in range(3)] |
---|
2135 | n/a | fut = asyncio.gather(*self.wrap_futures(a, b, c), **kwargs) |
---|
2136 | n/a | cb = test_utils.MockCallback() |
---|
2137 | n/a | fut.add_done_callback(cb) |
---|
2138 | n/a | b.set_result(1) |
---|
2139 | n/a | a.set_result(2) |
---|
2140 | n/a | self._run_loop(self.one_loop) |
---|
2141 | n/a | self.assertEqual(cb.called, False) |
---|
2142 | n/a | self.assertFalse(fut.done()) |
---|
2143 | n/a | c.set_result(3) |
---|
2144 | n/a | self._run_loop(self.one_loop) |
---|
2145 | n/a | cb.assert_called_once_with(fut) |
---|
2146 | n/a | self.assertEqual(fut.result(), [2, 1, 3]) |
---|
2147 | n/a | |
---|
2148 | n/a | def test_success(self): |
---|
2149 | n/a | self._check_success() |
---|
2150 | n/a | self._check_success(return_exceptions=False) |
---|
2151 | n/a | |
---|
2152 | n/a | def test_result_exception_success(self): |
---|
2153 | n/a | self._check_success(return_exceptions=True) |
---|
2154 | n/a | |
---|
2155 | n/a | def test_one_exception(self): |
---|
2156 | n/a | a, b, c, d, e = [asyncio.Future(loop=self.one_loop) for i in range(5)] |
---|
2157 | n/a | fut = asyncio.gather(*self.wrap_futures(a, b, c, d, e)) |
---|
2158 | n/a | cb = test_utils.MockCallback() |
---|
2159 | n/a | fut.add_done_callback(cb) |
---|
2160 | n/a | exc = ZeroDivisionError() |
---|
2161 | n/a | a.set_result(1) |
---|
2162 | n/a | b.set_exception(exc) |
---|
2163 | n/a | self._run_loop(self.one_loop) |
---|
2164 | n/a | self.assertTrue(fut.done()) |
---|
2165 | n/a | cb.assert_called_once_with(fut) |
---|
2166 | n/a | self.assertIs(fut.exception(), exc) |
---|
2167 | n/a | # Does nothing |
---|
2168 | n/a | c.set_result(3) |
---|
2169 | n/a | d.cancel() |
---|
2170 | n/a | e.set_exception(RuntimeError()) |
---|
2171 | n/a | e.exception() |
---|
2172 | n/a | |
---|
2173 | n/a | def test_return_exceptions(self): |
---|
2174 | n/a | a, b, c, d = [asyncio.Future(loop=self.one_loop) for i in range(4)] |
---|
2175 | n/a | fut = asyncio.gather(*self.wrap_futures(a, b, c, d), |
---|
2176 | n/a | return_exceptions=True) |
---|
2177 | n/a | cb = test_utils.MockCallback() |
---|
2178 | n/a | fut.add_done_callback(cb) |
---|
2179 | n/a | exc = ZeroDivisionError() |
---|
2180 | n/a | exc2 = RuntimeError() |
---|
2181 | n/a | b.set_result(1) |
---|
2182 | n/a | c.set_exception(exc) |
---|
2183 | n/a | a.set_result(3) |
---|
2184 | n/a | self._run_loop(self.one_loop) |
---|
2185 | n/a | self.assertFalse(fut.done()) |
---|
2186 | n/a | d.set_exception(exc2) |
---|
2187 | n/a | self._run_loop(self.one_loop) |
---|
2188 | n/a | self.assertTrue(fut.done()) |
---|
2189 | n/a | cb.assert_called_once_with(fut) |
---|
2190 | n/a | self.assertEqual(fut.result(), [3, 1, exc, exc2]) |
---|
2191 | n/a | |
---|
2192 | n/a | def test_env_var_debug(self): |
---|
2193 | n/a | aio_path = os.path.dirname(os.path.dirname(asyncio.__file__)) |
---|
2194 | n/a | |
---|
2195 | n/a | code = '\n'.join(( |
---|
2196 | n/a | 'import asyncio.coroutines', |
---|
2197 | n/a | 'print(asyncio.coroutines._DEBUG)')) |
---|
2198 | n/a | |
---|
2199 | n/a | # Test with -E to not fail if the unit test was run with |
---|
2200 | n/a | # PYTHONASYNCIODEBUG set to a non-empty string |
---|
2201 | n/a | sts, stdout, stderr = assert_python_ok('-E', '-c', code, |
---|
2202 | n/a | PYTHONPATH=aio_path) |
---|
2203 | n/a | self.assertEqual(stdout.rstrip(), b'False') |
---|
2204 | n/a | |
---|
2205 | n/a | sts, stdout, stderr = assert_python_ok('-c', code, |
---|
2206 | n/a | PYTHONASYNCIODEBUG='', |
---|
2207 | n/a | PYTHONPATH=aio_path) |
---|
2208 | n/a | self.assertEqual(stdout.rstrip(), b'False') |
---|
2209 | n/a | |
---|
2210 | n/a | sts, stdout, stderr = assert_python_ok('-c', code, |
---|
2211 | n/a | PYTHONASYNCIODEBUG='1', |
---|
2212 | n/a | PYTHONPATH=aio_path) |
---|
2213 | n/a | self.assertEqual(stdout.rstrip(), b'True') |
---|
2214 | n/a | |
---|
2215 | n/a | sts, stdout, stderr = assert_python_ok('-E', '-c', code, |
---|
2216 | n/a | PYTHONASYNCIODEBUG='1', |
---|
2217 | n/a | PYTHONPATH=aio_path) |
---|
2218 | n/a | self.assertEqual(stdout.rstrip(), b'False') |
---|
2219 | n/a | |
---|
2220 | n/a | |
---|
2221 | n/a | class FutureGatherTests(GatherTestsBase, test_utils.TestCase): |
---|
2222 | n/a | |
---|
2223 | n/a | def wrap_futures(self, *futures): |
---|
2224 | n/a | return futures |
---|
2225 | n/a | |
---|
2226 | n/a | def _check_empty_sequence(self, seq_or_iter): |
---|
2227 | n/a | asyncio.set_event_loop(self.one_loop) |
---|
2228 | n/a | self.addCleanup(asyncio.set_event_loop, None) |
---|
2229 | n/a | fut = asyncio.gather(*seq_or_iter) |
---|
2230 | n/a | self.assertIsInstance(fut, asyncio.Future) |
---|
2231 | n/a | self.assertIs(fut._loop, self.one_loop) |
---|
2232 | n/a | self._run_loop(self.one_loop) |
---|
2233 | n/a | self.assertTrue(fut.done()) |
---|
2234 | n/a | self.assertEqual(fut.result(), []) |
---|
2235 | n/a | fut = asyncio.gather(*seq_or_iter, loop=self.other_loop) |
---|
2236 | n/a | self.assertIs(fut._loop, self.other_loop) |
---|
2237 | n/a | |
---|
2238 | n/a | def test_constructor_empty_sequence(self): |
---|
2239 | n/a | self._check_empty_sequence([]) |
---|
2240 | n/a | self._check_empty_sequence(()) |
---|
2241 | n/a | self._check_empty_sequence(set()) |
---|
2242 | n/a | self._check_empty_sequence(iter("")) |
---|
2243 | n/a | |
---|
2244 | n/a | def test_constructor_heterogenous_futures(self): |
---|
2245 | n/a | fut1 = asyncio.Future(loop=self.one_loop) |
---|
2246 | n/a | fut2 = asyncio.Future(loop=self.other_loop) |
---|
2247 | n/a | with self.assertRaises(ValueError): |
---|
2248 | n/a | asyncio.gather(fut1, fut2) |
---|
2249 | n/a | with self.assertRaises(ValueError): |
---|
2250 | n/a | asyncio.gather(fut1, loop=self.other_loop) |
---|
2251 | n/a | |
---|
2252 | n/a | def test_constructor_homogenous_futures(self): |
---|
2253 | n/a | children = [asyncio.Future(loop=self.other_loop) for i in range(3)] |
---|
2254 | n/a | fut = asyncio.gather(*children) |
---|
2255 | n/a | self.assertIs(fut._loop, self.other_loop) |
---|
2256 | n/a | self._run_loop(self.other_loop) |
---|
2257 | n/a | self.assertFalse(fut.done()) |
---|
2258 | n/a | fut = asyncio.gather(*children, loop=self.other_loop) |
---|
2259 | n/a | self.assertIs(fut._loop, self.other_loop) |
---|
2260 | n/a | self._run_loop(self.other_loop) |
---|
2261 | n/a | self.assertFalse(fut.done()) |
---|
2262 | n/a | |
---|
2263 | n/a | def test_one_cancellation(self): |
---|
2264 | n/a | a, b, c, d, e = [asyncio.Future(loop=self.one_loop) for i in range(5)] |
---|
2265 | n/a | fut = asyncio.gather(a, b, c, d, e) |
---|
2266 | n/a | cb = test_utils.MockCallback() |
---|
2267 | n/a | fut.add_done_callback(cb) |
---|
2268 | n/a | a.set_result(1) |
---|
2269 | n/a | b.cancel() |
---|
2270 | n/a | self._run_loop(self.one_loop) |
---|
2271 | n/a | self.assertTrue(fut.done()) |
---|
2272 | n/a | cb.assert_called_once_with(fut) |
---|
2273 | n/a | self.assertFalse(fut.cancelled()) |
---|
2274 | n/a | self.assertIsInstance(fut.exception(), asyncio.CancelledError) |
---|
2275 | n/a | # Does nothing |
---|
2276 | n/a | c.set_result(3) |
---|
2277 | n/a | d.cancel() |
---|
2278 | n/a | e.set_exception(RuntimeError()) |
---|
2279 | n/a | e.exception() |
---|
2280 | n/a | |
---|
2281 | n/a | def test_result_exception_one_cancellation(self): |
---|
2282 | n/a | a, b, c, d, e, f = [asyncio.Future(loop=self.one_loop) |
---|
2283 | n/a | for i in range(6)] |
---|
2284 | n/a | fut = asyncio.gather(a, b, c, d, e, f, return_exceptions=True) |
---|
2285 | n/a | cb = test_utils.MockCallback() |
---|
2286 | n/a | fut.add_done_callback(cb) |
---|
2287 | n/a | a.set_result(1) |
---|
2288 | n/a | zde = ZeroDivisionError() |
---|
2289 | n/a | b.set_exception(zde) |
---|
2290 | n/a | c.cancel() |
---|
2291 | n/a | self._run_loop(self.one_loop) |
---|
2292 | n/a | self.assertFalse(fut.done()) |
---|
2293 | n/a | d.set_result(3) |
---|
2294 | n/a | e.cancel() |
---|
2295 | n/a | rte = RuntimeError() |
---|
2296 | n/a | f.set_exception(rte) |
---|
2297 | n/a | res = self.one_loop.run_until_complete(fut) |
---|
2298 | n/a | self.assertIsInstance(res[2], asyncio.CancelledError) |
---|
2299 | n/a | self.assertIsInstance(res[4], asyncio.CancelledError) |
---|
2300 | n/a | res[2] = res[4] = None |
---|
2301 | n/a | self.assertEqual(res, [1, zde, None, 3, None, rte]) |
---|
2302 | n/a | cb.assert_called_once_with(fut) |
---|
2303 | n/a | |
---|
2304 | n/a | |
---|
2305 | n/a | class CoroutineGatherTests(GatherTestsBase, test_utils.TestCase): |
---|
2306 | n/a | |
---|
2307 | n/a | def setUp(self): |
---|
2308 | n/a | super().setUp() |
---|
2309 | n/a | asyncio.set_event_loop(self.one_loop) |
---|
2310 | n/a | |
---|
2311 | n/a | def wrap_futures(self, *futures): |
---|
2312 | n/a | coros = [] |
---|
2313 | n/a | for fut in futures: |
---|
2314 | n/a | @asyncio.coroutine |
---|
2315 | n/a | def coro(fut=fut): |
---|
2316 | n/a | return (yield from fut) |
---|
2317 | n/a | coros.append(coro()) |
---|
2318 | n/a | return coros |
---|
2319 | n/a | |
---|
2320 | n/a | def test_constructor_loop_selection(self): |
---|
2321 | n/a | @asyncio.coroutine |
---|
2322 | n/a | def coro(): |
---|
2323 | n/a | return 'abc' |
---|
2324 | n/a | gen1 = coro() |
---|
2325 | n/a | gen2 = coro() |
---|
2326 | n/a | fut = asyncio.gather(gen1, gen2) |
---|
2327 | n/a | self.assertIs(fut._loop, self.one_loop) |
---|
2328 | n/a | self.one_loop.run_until_complete(fut) |
---|
2329 | n/a | |
---|
2330 | n/a | self.set_event_loop(self.other_loop, cleanup=False) |
---|
2331 | n/a | gen3 = coro() |
---|
2332 | n/a | gen4 = coro() |
---|
2333 | n/a | fut2 = asyncio.gather(gen3, gen4, loop=self.other_loop) |
---|
2334 | n/a | self.assertIs(fut2._loop, self.other_loop) |
---|
2335 | n/a | self.other_loop.run_until_complete(fut2) |
---|
2336 | n/a | |
---|
2337 | n/a | def test_duplicate_coroutines(self): |
---|
2338 | n/a | @asyncio.coroutine |
---|
2339 | n/a | def coro(s): |
---|
2340 | n/a | return s |
---|
2341 | n/a | c = coro('abc') |
---|
2342 | n/a | fut = asyncio.gather(c, c, coro('def'), c, loop=self.one_loop) |
---|
2343 | n/a | self._run_loop(self.one_loop) |
---|
2344 | n/a | self.assertEqual(fut.result(), ['abc', 'abc', 'def', 'abc']) |
---|
2345 | n/a | |
---|
2346 | n/a | def test_cancellation_broadcast(self): |
---|
2347 | n/a | # Cancelling outer() cancels all children. |
---|
2348 | n/a | proof = 0 |
---|
2349 | n/a | waiter = asyncio.Future(loop=self.one_loop) |
---|
2350 | n/a | |
---|
2351 | n/a | @asyncio.coroutine |
---|
2352 | n/a | def inner(): |
---|
2353 | n/a | nonlocal proof |
---|
2354 | n/a | yield from waiter |
---|
2355 | n/a | proof += 1 |
---|
2356 | n/a | |
---|
2357 | n/a | child1 = asyncio.ensure_future(inner(), loop=self.one_loop) |
---|
2358 | n/a | child2 = asyncio.ensure_future(inner(), loop=self.one_loop) |
---|
2359 | n/a | gatherer = None |
---|
2360 | n/a | |
---|
2361 | n/a | @asyncio.coroutine |
---|
2362 | n/a | def outer(): |
---|
2363 | n/a | nonlocal proof, gatherer |
---|
2364 | n/a | gatherer = asyncio.gather(child1, child2, loop=self.one_loop) |
---|
2365 | n/a | yield from gatherer |
---|
2366 | n/a | proof += 100 |
---|
2367 | n/a | |
---|
2368 | n/a | f = asyncio.ensure_future(outer(), loop=self.one_loop) |
---|
2369 | n/a | test_utils.run_briefly(self.one_loop) |
---|
2370 | n/a | self.assertTrue(f.cancel()) |
---|
2371 | n/a | with self.assertRaises(asyncio.CancelledError): |
---|
2372 | n/a | self.one_loop.run_until_complete(f) |
---|
2373 | n/a | self.assertFalse(gatherer.cancel()) |
---|
2374 | n/a | self.assertTrue(waiter.cancelled()) |
---|
2375 | n/a | self.assertTrue(child1.cancelled()) |
---|
2376 | n/a | self.assertTrue(child2.cancelled()) |
---|
2377 | n/a | test_utils.run_briefly(self.one_loop) |
---|
2378 | n/a | self.assertEqual(proof, 0) |
---|
2379 | n/a | |
---|
2380 | n/a | def test_exception_marking(self): |
---|
2381 | n/a | # Test for the first line marked "Mark exception retrieved." |
---|
2382 | n/a | |
---|
2383 | n/a | @asyncio.coroutine |
---|
2384 | n/a | def inner(f): |
---|
2385 | n/a | yield from f |
---|
2386 | n/a | raise RuntimeError('should not be ignored') |
---|
2387 | n/a | |
---|
2388 | n/a | a = asyncio.Future(loop=self.one_loop) |
---|
2389 | n/a | b = asyncio.Future(loop=self.one_loop) |
---|
2390 | n/a | |
---|
2391 | n/a | @asyncio.coroutine |
---|
2392 | n/a | def outer(): |
---|
2393 | n/a | yield from asyncio.gather(inner(a), inner(b), loop=self.one_loop) |
---|
2394 | n/a | |
---|
2395 | n/a | f = asyncio.ensure_future(outer(), loop=self.one_loop) |
---|
2396 | n/a | test_utils.run_briefly(self.one_loop) |
---|
2397 | n/a | a.set_result(None) |
---|
2398 | n/a | test_utils.run_briefly(self.one_loop) |
---|
2399 | n/a | b.set_result(None) |
---|
2400 | n/a | test_utils.run_briefly(self.one_loop) |
---|
2401 | n/a | self.assertIsInstance(f.exception(), RuntimeError) |
---|
2402 | n/a | |
---|
2403 | n/a | |
---|
2404 | n/a | class RunCoroutineThreadsafeTests(test_utils.TestCase): |
---|
2405 | n/a | """Test case for asyncio.run_coroutine_threadsafe.""" |
---|
2406 | n/a | |
---|
2407 | n/a | def setUp(self): |
---|
2408 | n/a | super().setUp() |
---|
2409 | n/a | self.loop = asyncio.new_event_loop() |
---|
2410 | n/a | self.set_event_loop(self.loop) # Will cleanup properly |
---|
2411 | n/a | |
---|
2412 | n/a | @asyncio.coroutine |
---|
2413 | n/a | def add(self, a, b, fail=False, cancel=False): |
---|
2414 | n/a | """Wait 0.05 second and return a + b.""" |
---|
2415 | n/a | yield from asyncio.sleep(0.05, loop=self.loop) |
---|
2416 | n/a | if fail: |
---|
2417 | n/a | raise RuntimeError("Fail!") |
---|
2418 | n/a | if cancel: |
---|
2419 | n/a | asyncio.tasks.Task.current_task(self.loop).cancel() |
---|
2420 | n/a | yield |
---|
2421 | n/a | return a + b |
---|
2422 | n/a | |
---|
2423 | n/a | def target(self, fail=False, cancel=False, timeout=None, |
---|
2424 | n/a | advance_coro=False): |
---|
2425 | n/a | """Run add coroutine in the event loop.""" |
---|
2426 | n/a | coro = self.add(1, 2, fail=fail, cancel=cancel) |
---|
2427 | n/a | future = asyncio.run_coroutine_threadsafe(coro, self.loop) |
---|
2428 | n/a | if advance_coro: |
---|
2429 | n/a | # this is for test_run_coroutine_threadsafe_task_factory_exception; |
---|
2430 | n/a | # otherwise it spills errors and breaks **other** unittests, since |
---|
2431 | n/a | # 'target' is interacting with threads. |
---|
2432 | n/a | |
---|
2433 | n/a | # With this call, `coro` will be advanced, so that |
---|
2434 | n/a | # CoroWrapper.__del__ won't do anything when asyncio tests run |
---|
2435 | n/a | # in debug mode. |
---|
2436 | n/a | self.loop.call_soon_threadsafe(coro.send, None) |
---|
2437 | n/a | try: |
---|
2438 | n/a | return future.result(timeout) |
---|
2439 | n/a | finally: |
---|
2440 | n/a | future.done() or future.cancel() |
---|
2441 | n/a | |
---|
2442 | n/a | def test_run_coroutine_threadsafe(self): |
---|
2443 | n/a | """Test coroutine submission from a thread to an event loop.""" |
---|
2444 | n/a | future = self.loop.run_in_executor(None, self.target) |
---|
2445 | n/a | result = self.loop.run_until_complete(future) |
---|
2446 | n/a | self.assertEqual(result, 3) |
---|
2447 | n/a | |
---|
2448 | n/a | def test_run_coroutine_threadsafe_with_exception(self): |
---|
2449 | n/a | """Test coroutine submission from a thread to an event loop |
---|
2450 | n/a | when an exception is raised.""" |
---|
2451 | n/a | future = self.loop.run_in_executor(None, self.target, True) |
---|
2452 | n/a | with self.assertRaises(RuntimeError) as exc_context: |
---|
2453 | n/a | self.loop.run_until_complete(future) |
---|
2454 | n/a | self.assertIn("Fail!", exc_context.exception.args) |
---|
2455 | n/a | |
---|
2456 | n/a | def test_run_coroutine_threadsafe_with_timeout(self): |
---|
2457 | n/a | """Test coroutine submission from a thread to an event loop |
---|
2458 | n/a | when a timeout is raised.""" |
---|
2459 | n/a | callback = lambda: self.target(timeout=0) |
---|
2460 | n/a | future = self.loop.run_in_executor(None, callback) |
---|
2461 | n/a | with self.assertRaises(asyncio.TimeoutError): |
---|
2462 | n/a | self.loop.run_until_complete(future) |
---|
2463 | n/a | test_utils.run_briefly(self.loop) |
---|
2464 | n/a | # Check that there's no pending task (add has been cancelled) |
---|
2465 | n/a | for task in asyncio.Task.all_tasks(self.loop): |
---|
2466 | n/a | self.assertTrue(task.done()) |
---|
2467 | n/a | |
---|
2468 | n/a | def test_run_coroutine_threadsafe_task_cancelled(self): |
---|
2469 | n/a | """Test coroutine submission from a tread to an event loop |
---|
2470 | n/a | when the task is cancelled.""" |
---|
2471 | n/a | callback = lambda: self.target(cancel=True) |
---|
2472 | n/a | future = self.loop.run_in_executor(None, callback) |
---|
2473 | n/a | with self.assertRaises(asyncio.CancelledError): |
---|
2474 | n/a | self.loop.run_until_complete(future) |
---|
2475 | n/a | |
---|
2476 | n/a | def test_run_coroutine_threadsafe_task_factory_exception(self): |
---|
2477 | n/a | """Test coroutine submission from a tread to an event loop |
---|
2478 | n/a | when the task factory raise an exception.""" |
---|
2479 | n/a | # Schedule the target |
---|
2480 | n/a | future = self.loop.run_in_executor( |
---|
2481 | n/a | None, lambda: self.target(advance_coro=True)) |
---|
2482 | n/a | # Set corrupted task factory |
---|
2483 | n/a | self.loop.set_task_factory(lambda loop, coro: wrong_name) |
---|
2484 | n/a | # Set exception handler |
---|
2485 | n/a | callback = test_utils.MockCallback() |
---|
2486 | n/a | self.loop.set_exception_handler(callback) |
---|
2487 | n/a | # Run event loop |
---|
2488 | n/a | with self.assertRaises(NameError) as exc_context: |
---|
2489 | n/a | self.loop.run_until_complete(future) |
---|
2490 | n/a | # Check exceptions |
---|
2491 | n/a | self.assertIn('wrong_name', exc_context.exception.args[0]) |
---|
2492 | n/a | self.assertEqual(len(callback.call_args_list), 1) |
---|
2493 | n/a | (loop, context), kwargs = callback.call_args |
---|
2494 | n/a | self.assertEqual(context['exception'], exc_context.exception) |
---|
2495 | n/a | |
---|
2496 | n/a | |
---|
2497 | n/a | class SleepTests(test_utils.TestCase): |
---|
2498 | n/a | def setUp(self): |
---|
2499 | n/a | super().setUp() |
---|
2500 | n/a | self.loop = asyncio.new_event_loop() |
---|
2501 | n/a | asyncio.set_event_loop(None) |
---|
2502 | n/a | |
---|
2503 | n/a | def tearDown(self): |
---|
2504 | n/a | self.loop.close() |
---|
2505 | n/a | self.loop = None |
---|
2506 | n/a | super().tearDown() |
---|
2507 | n/a | |
---|
2508 | n/a | def test_sleep_zero(self): |
---|
2509 | n/a | result = 0 |
---|
2510 | n/a | |
---|
2511 | n/a | def inc_result(num): |
---|
2512 | n/a | nonlocal result |
---|
2513 | n/a | result += num |
---|
2514 | n/a | |
---|
2515 | n/a | @asyncio.coroutine |
---|
2516 | n/a | def coro(): |
---|
2517 | n/a | self.loop.call_soon(inc_result, 1) |
---|
2518 | n/a | self.assertEqual(result, 0) |
---|
2519 | n/a | num = yield from asyncio.sleep(0, loop=self.loop, result=10) |
---|
2520 | n/a | self.assertEqual(result, 1) # inc'ed by call_soon |
---|
2521 | n/a | inc_result(num) # num should be 11 |
---|
2522 | n/a | |
---|
2523 | n/a | self.loop.run_until_complete(coro()) |
---|
2524 | n/a | self.assertEqual(result, 11) |
---|
2525 | n/a | |
---|
2526 | n/a | |
---|
2527 | n/a | if __name__ == '__main__': |
---|
2528 | n/a | unittest.main() |
---|