1 | n/a | """Tests for futures.py.""" |
---|
2 | n/a | |
---|
3 | n/a | import concurrent.futures |
---|
4 | n/a | import re |
---|
5 | n/a | import sys |
---|
6 | n/a | import threading |
---|
7 | n/a | import unittest |
---|
8 | n/a | from unittest import mock |
---|
9 | n/a | |
---|
10 | n/a | import asyncio |
---|
11 | n/a | from asyncio import test_utils |
---|
12 | n/a | from asyncio import futures |
---|
13 | n/a | try: |
---|
14 | n/a | from test import support |
---|
15 | n/a | except ImportError: |
---|
16 | n/a | from asyncio import test_support as support |
---|
17 | n/a | |
---|
18 | n/a | |
---|
19 | n/a | def _fakefunc(f): |
---|
20 | n/a | return f |
---|
21 | n/a | |
---|
22 | n/a | def first_cb(): |
---|
23 | n/a | pass |
---|
24 | n/a | |
---|
25 | n/a | def last_cb(): |
---|
26 | n/a | pass |
---|
27 | n/a | |
---|
28 | n/a | |
---|
29 | n/a | class DuckFuture: |
---|
30 | n/a | # Class that does not inherit from Future but aims to be duck-type |
---|
31 | n/a | # compatible with it. |
---|
32 | n/a | |
---|
33 | n/a | _asyncio_future_blocking = False |
---|
34 | n/a | __cancelled = False |
---|
35 | n/a | __result = None |
---|
36 | n/a | __exception = None |
---|
37 | n/a | |
---|
38 | n/a | def cancel(self): |
---|
39 | n/a | if self.done(): |
---|
40 | n/a | return False |
---|
41 | n/a | self.__cancelled = True |
---|
42 | n/a | return True |
---|
43 | n/a | |
---|
44 | n/a | def cancelled(self): |
---|
45 | n/a | return self.__cancelled |
---|
46 | n/a | |
---|
47 | n/a | def done(self): |
---|
48 | n/a | return (self.__cancelled |
---|
49 | n/a | or self.__result is not None |
---|
50 | n/a | or self.__exception is not None) |
---|
51 | n/a | |
---|
52 | n/a | def result(self): |
---|
53 | n/a | assert not self.cancelled() |
---|
54 | n/a | if self.__exception is not None: |
---|
55 | n/a | raise self.__exception |
---|
56 | n/a | return self.__result |
---|
57 | n/a | |
---|
58 | n/a | def exception(self): |
---|
59 | n/a | assert not self.cancelled() |
---|
60 | n/a | return self.__exception |
---|
61 | n/a | |
---|
62 | n/a | def set_result(self, result): |
---|
63 | n/a | assert not self.done() |
---|
64 | n/a | assert result is not None |
---|
65 | n/a | self.__result = result |
---|
66 | n/a | |
---|
67 | n/a | def set_exception(self, exception): |
---|
68 | n/a | assert not self.done() |
---|
69 | n/a | assert exception is not None |
---|
70 | n/a | self.__exception = exception |
---|
71 | n/a | |
---|
72 | n/a | def __iter__(self): |
---|
73 | n/a | if not self.done(): |
---|
74 | n/a | self._asyncio_future_blocking = True |
---|
75 | n/a | yield self |
---|
76 | n/a | assert self.done() |
---|
77 | n/a | return self.result() |
---|
78 | n/a | |
---|
79 | n/a | |
---|
80 | n/a | class DuckTests(test_utils.TestCase): |
---|
81 | n/a | |
---|
82 | n/a | def setUp(self): |
---|
83 | n/a | super().setUp() |
---|
84 | n/a | self.loop = self.new_test_loop() |
---|
85 | n/a | self.addCleanup(self.loop.close) |
---|
86 | n/a | |
---|
87 | n/a | def test_wrap_future(self): |
---|
88 | n/a | f = DuckFuture() |
---|
89 | n/a | g = asyncio.wrap_future(f) |
---|
90 | n/a | assert g is f |
---|
91 | n/a | |
---|
92 | n/a | def test_ensure_future(self): |
---|
93 | n/a | f = DuckFuture() |
---|
94 | n/a | g = asyncio.ensure_future(f) |
---|
95 | n/a | assert g is f |
---|
96 | n/a | |
---|
97 | n/a | |
---|
98 | n/a | class BaseFutureTests: |
---|
99 | n/a | |
---|
100 | n/a | def _new_future(self, loop=None): |
---|
101 | n/a | raise NotImplementedError |
---|
102 | n/a | |
---|
103 | n/a | def setUp(self): |
---|
104 | n/a | super().setUp() |
---|
105 | n/a | self.loop = self.new_test_loop() |
---|
106 | n/a | self.addCleanup(self.loop.close) |
---|
107 | n/a | |
---|
108 | n/a | def test_isfuture(self): |
---|
109 | n/a | class MyFuture: |
---|
110 | n/a | _asyncio_future_blocking = None |
---|
111 | n/a | |
---|
112 | n/a | def __init__(self): |
---|
113 | n/a | self._asyncio_future_blocking = False |
---|
114 | n/a | |
---|
115 | n/a | self.assertFalse(asyncio.isfuture(MyFuture)) |
---|
116 | n/a | self.assertTrue(asyncio.isfuture(MyFuture())) |
---|
117 | n/a | self.assertFalse(asyncio.isfuture(1)) |
---|
118 | n/a | |
---|
119 | n/a | # As `isinstance(Mock(), Future)` returns `False` |
---|
120 | n/a | self.assertFalse(asyncio.isfuture(mock.Mock())) |
---|
121 | n/a | |
---|
122 | n/a | f = self._new_future(loop=self.loop) |
---|
123 | n/a | self.assertTrue(asyncio.isfuture(f)) |
---|
124 | n/a | self.assertFalse(asyncio.isfuture(type(f))) |
---|
125 | n/a | |
---|
126 | n/a | # As `isinstance(Mock(Future), Future)` returns `True` |
---|
127 | n/a | self.assertTrue(asyncio.isfuture(mock.Mock(type(f)))) |
---|
128 | n/a | |
---|
129 | n/a | f.cancel() |
---|
130 | n/a | |
---|
131 | n/a | def test_initial_state(self): |
---|
132 | n/a | f = self._new_future(loop=self.loop) |
---|
133 | n/a | self.assertFalse(f.cancelled()) |
---|
134 | n/a | self.assertFalse(f.done()) |
---|
135 | n/a | f.cancel() |
---|
136 | n/a | self.assertTrue(f.cancelled()) |
---|
137 | n/a | |
---|
138 | n/a | def test_init_constructor_default_loop(self): |
---|
139 | n/a | asyncio.set_event_loop(self.loop) |
---|
140 | n/a | f = self._new_future() |
---|
141 | n/a | self.assertIs(f._loop, self.loop) |
---|
142 | n/a | |
---|
143 | n/a | def test_constructor_positional(self): |
---|
144 | n/a | # Make sure Future doesn't accept a positional argument |
---|
145 | n/a | self.assertRaises(TypeError, self._new_future, 42) |
---|
146 | n/a | |
---|
147 | n/a | def test_cancel(self): |
---|
148 | n/a | f = self._new_future(loop=self.loop) |
---|
149 | n/a | self.assertTrue(f.cancel()) |
---|
150 | n/a | self.assertTrue(f.cancelled()) |
---|
151 | n/a | self.assertTrue(f.done()) |
---|
152 | n/a | self.assertRaises(asyncio.CancelledError, f.result) |
---|
153 | n/a | self.assertRaises(asyncio.CancelledError, f.exception) |
---|
154 | n/a | self.assertRaises(asyncio.InvalidStateError, f.set_result, None) |
---|
155 | n/a | self.assertRaises(asyncio.InvalidStateError, f.set_exception, None) |
---|
156 | n/a | self.assertFalse(f.cancel()) |
---|
157 | n/a | |
---|
158 | n/a | def test_result(self): |
---|
159 | n/a | f = self._new_future(loop=self.loop) |
---|
160 | n/a | self.assertRaises(asyncio.InvalidStateError, f.result) |
---|
161 | n/a | |
---|
162 | n/a | f.set_result(42) |
---|
163 | n/a | self.assertFalse(f.cancelled()) |
---|
164 | n/a | self.assertTrue(f.done()) |
---|
165 | n/a | self.assertEqual(f.result(), 42) |
---|
166 | n/a | self.assertEqual(f.exception(), None) |
---|
167 | n/a | self.assertRaises(asyncio.InvalidStateError, f.set_result, None) |
---|
168 | n/a | self.assertRaises(asyncio.InvalidStateError, f.set_exception, None) |
---|
169 | n/a | self.assertFalse(f.cancel()) |
---|
170 | n/a | |
---|
171 | n/a | def test_exception(self): |
---|
172 | n/a | exc = RuntimeError() |
---|
173 | n/a | f = self._new_future(loop=self.loop) |
---|
174 | n/a | self.assertRaises(asyncio.InvalidStateError, f.exception) |
---|
175 | n/a | |
---|
176 | n/a | # StopIteration cannot be raised into a Future - CPython issue26221 |
---|
177 | n/a | self.assertRaisesRegex(TypeError, "StopIteration .* cannot be raised", |
---|
178 | n/a | f.set_exception, StopIteration) |
---|
179 | n/a | |
---|
180 | n/a | f.set_exception(exc) |
---|
181 | n/a | self.assertFalse(f.cancelled()) |
---|
182 | n/a | self.assertTrue(f.done()) |
---|
183 | n/a | self.assertRaises(RuntimeError, f.result) |
---|
184 | n/a | self.assertEqual(f.exception(), exc) |
---|
185 | n/a | self.assertRaises(asyncio.InvalidStateError, f.set_result, None) |
---|
186 | n/a | self.assertRaises(asyncio.InvalidStateError, f.set_exception, None) |
---|
187 | n/a | self.assertFalse(f.cancel()) |
---|
188 | n/a | |
---|
189 | n/a | def test_exception_class(self): |
---|
190 | n/a | f = self._new_future(loop=self.loop) |
---|
191 | n/a | f.set_exception(RuntimeError) |
---|
192 | n/a | self.assertIsInstance(f.exception(), RuntimeError) |
---|
193 | n/a | |
---|
194 | n/a | def test_yield_from_twice(self): |
---|
195 | n/a | f = self._new_future(loop=self.loop) |
---|
196 | n/a | |
---|
197 | n/a | def fixture(): |
---|
198 | n/a | yield 'A' |
---|
199 | n/a | x = yield from f |
---|
200 | n/a | yield 'B', x |
---|
201 | n/a | y = yield from f |
---|
202 | n/a | yield 'C', y |
---|
203 | n/a | |
---|
204 | n/a | g = fixture() |
---|
205 | n/a | self.assertEqual(next(g), 'A') # yield 'A'. |
---|
206 | n/a | self.assertEqual(next(g), f) # First yield from f. |
---|
207 | n/a | f.set_result(42) |
---|
208 | n/a | self.assertEqual(next(g), ('B', 42)) # yield 'B', x. |
---|
209 | n/a | # The second "yield from f" does not yield f. |
---|
210 | n/a | self.assertEqual(next(g), ('C', 42)) # yield 'C', y. |
---|
211 | n/a | |
---|
212 | n/a | def test_future_repr(self): |
---|
213 | n/a | self.loop.set_debug(True) |
---|
214 | n/a | f_pending_debug = self._new_future(loop=self.loop) |
---|
215 | n/a | frame = f_pending_debug._source_traceback[-1] |
---|
216 | n/a | self.assertEqual(repr(f_pending_debug), |
---|
217 | n/a | '<Future pending created at %s:%s>' |
---|
218 | n/a | % (frame[0], frame[1])) |
---|
219 | n/a | f_pending_debug.cancel() |
---|
220 | n/a | |
---|
221 | n/a | self.loop.set_debug(False) |
---|
222 | n/a | f_pending = self._new_future(loop=self.loop) |
---|
223 | n/a | self.assertEqual(repr(f_pending), '<Future pending>') |
---|
224 | n/a | f_pending.cancel() |
---|
225 | n/a | |
---|
226 | n/a | f_cancelled = self._new_future(loop=self.loop) |
---|
227 | n/a | f_cancelled.cancel() |
---|
228 | n/a | self.assertEqual(repr(f_cancelled), '<Future cancelled>') |
---|
229 | n/a | |
---|
230 | n/a | f_result = self._new_future(loop=self.loop) |
---|
231 | n/a | f_result.set_result(4) |
---|
232 | n/a | self.assertEqual(repr(f_result), '<Future finished result=4>') |
---|
233 | n/a | self.assertEqual(f_result.result(), 4) |
---|
234 | n/a | |
---|
235 | n/a | exc = RuntimeError() |
---|
236 | n/a | f_exception = self._new_future(loop=self.loop) |
---|
237 | n/a | f_exception.set_exception(exc) |
---|
238 | n/a | self.assertEqual(repr(f_exception), |
---|
239 | n/a | '<Future finished exception=RuntimeError()>') |
---|
240 | n/a | self.assertIs(f_exception.exception(), exc) |
---|
241 | n/a | |
---|
242 | n/a | def func_repr(func): |
---|
243 | n/a | filename, lineno = test_utils.get_function_source(func) |
---|
244 | n/a | text = '%s() at %s:%s' % (func.__qualname__, filename, lineno) |
---|
245 | n/a | return re.escape(text) |
---|
246 | n/a | |
---|
247 | n/a | f_one_callbacks = self._new_future(loop=self.loop) |
---|
248 | n/a | f_one_callbacks.add_done_callback(_fakefunc) |
---|
249 | n/a | fake_repr = func_repr(_fakefunc) |
---|
250 | n/a | self.assertRegex(repr(f_one_callbacks), |
---|
251 | n/a | r'<Future pending cb=\[%s\]>' % fake_repr) |
---|
252 | n/a | f_one_callbacks.cancel() |
---|
253 | n/a | self.assertEqual(repr(f_one_callbacks), |
---|
254 | n/a | '<Future cancelled>') |
---|
255 | n/a | |
---|
256 | n/a | f_two_callbacks = self._new_future(loop=self.loop) |
---|
257 | n/a | f_two_callbacks.add_done_callback(first_cb) |
---|
258 | n/a | f_two_callbacks.add_done_callback(last_cb) |
---|
259 | n/a | first_repr = func_repr(first_cb) |
---|
260 | n/a | last_repr = func_repr(last_cb) |
---|
261 | n/a | self.assertRegex(repr(f_two_callbacks), |
---|
262 | n/a | r'<Future pending cb=\[%s, %s\]>' |
---|
263 | n/a | % (first_repr, last_repr)) |
---|
264 | n/a | |
---|
265 | n/a | f_many_callbacks = self._new_future(loop=self.loop) |
---|
266 | n/a | f_many_callbacks.add_done_callback(first_cb) |
---|
267 | n/a | for i in range(8): |
---|
268 | n/a | f_many_callbacks.add_done_callback(_fakefunc) |
---|
269 | n/a | f_many_callbacks.add_done_callback(last_cb) |
---|
270 | n/a | cb_regex = r'%s, <8 more>, %s' % (first_repr, last_repr) |
---|
271 | n/a | self.assertRegex(repr(f_many_callbacks), |
---|
272 | n/a | r'<Future pending cb=\[%s\]>' % cb_regex) |
---|
273 | n/a | f_many_callbacks.cancel() |
---|
274 | n/a | self.assertEqual(repr(f_many_callbacks), |
---|
275 | n/a | '<Future cancelled>') |
---|
276 | n/a | |
---|
277 | n/a | def test_copy_state(self): |
---|
278 | n/a | from asyncio.futures import _copy_future_state |
---|
279 | n/a | |
---|
280 | n/a | f = self._new_future(loop=self.loop) |
---|
281 | n/a | f.set_result(10) |
---|
282 | n/a | |
---|
283 | n/a | newf = self._new_future(loop=self.loop) |
---|
284 | n/a | _copy_future_state(f, newf) |
---|
285 | n/a | self.assertTrue(newf.done()) |
---|
286 | n/a | self.assertEqual(newf.result(), 10) |
---|
287 | n/a | |
---|
288 | n/a | f_exception = self._new_future(loop=self.loop) |
---|
289 | n/a | f_exception.set_exception(RuntimeError()) |
---|
290 | n/a | |
---|
291 | n/a | newf_exception = self._new_future(loop=self.loop) |
---|
292 | n/a | _copy_future_state(f_exception, newf_exception) |
---|
293 | n/a | self.assertTrue(newf_exception.done()) |
---|
294 | n/a | self.assertRaises(RuntimeError, newf_exception.result) |
---|
295 | n/a | |
---|
296 | n/a | f_cancelled = self._new_future(loop=self.loop) |
---|
297 | n/a | f_cancelled.cancel() |
---|
298 | n/a | |
---|
299 | n/a | newf_cancelled = self._new_future(loop=self.loop) |
---|
300 | n/a | _copy_future_state(f_cancelled, newf_cancelled) |
---|
301 | n/a | self.assertTrue(newf_cancelled.cancelled()) |
---|
302 | n/a | |
---|
303 | n/a | def test_iter(self): |
---|
304 | n/a | fut = self._new_future(loop=self.loop) |
---|
305 | n/a | |
---|
306 | n/a | def coro(): |
---|
307 | n/a | yield from fut |
---|
308 | n/a | |
---|
309 | n/a | def test(): |
---|
310 | n/a | arg1, arg2 = coro() |
---|
311 | n/a | |
---|
312 | n/a | self.assertRaises(AssertionError, test) |
---|
313 | n/a | fut.cancel() |
---|
314 | n/a | |
---|
315 | n/a | @mock.patch('asyncio.base_events.logger') |
---|
316 | n/a | def test_tb_logger_abandoned(self, m_log): |
---|
317 | n/a | fut = self._new_future(loop=self.loop) |
---|
318 | n/a | del fut |
---|
319 | n/a | self.assertFalse(m_log.error.called) |
---|
320 | n/a | |
---|
321 | n/a | @mock.patch('asyncio.base_events.logger') |
---|
322 | n/a | def test_tb_logger_result_unretrieved(self, m_log): |
---|
323 | n/a | fut = self._new_future(loop=self.loop) |
---|
324 | n/a | fut.set_result(42) |
---|
325 | n/a | del fut |
---|
326 | n/a | self.assertFalse(m_log.error.called) |
---|
327 | n/a | |
---|
328 | n/a | @mock.patch('asyncio.base_events.logger') |
---|
329 | n/a | def test_tb_logger_result_retrieved(self, m_log): |
---|
330 | n/a | fut = self._new_future(loop=self.loop) |
---|
331 | n/a | fut.set_result(42) |
---|
332 | n/a | fut.result() |
---|
333 | n/a | del fut |
---|
334 | n/a | self.assertFalse(m_log.error.called) |
---|
335 | n/a | |
---|
336 | n/a | @mock.patch('asyncio.base_events.logger') |
---|
337 | n/a | def test_tb_logger_exception_unretrieved(self, m_log): |
---|
338 | n/a | fut = self._new_future(loop=self.loop) |
---|
339 | n/a | fut.set_exception(RuntimeError('boom')) |
---|
340 | n/a | del fut |
---|
341 | n/a | test_utils.run_briefly(self.loop) |
---|
342 | n/a | support.gc_collect() |
---|
343 | n/a | self.assertTrue(m_log.error.called) |
---|
344 | n/a | |
---|
345 | n/a | @mock.patch('asyncio.base_events.logger') |
---|
346 | n/a | def test_tb_logger_exception_retrieved(self, m_log): |
---|
347 | n/a | fut = self._new_future(loop=self.loop) |
---|
348 | n/a | fut.set_exception(RuntimeError('boom')) |
---|
349 | n/a | fut.exception() |
---|
350 | n/a | del fut |
---|
351 | n/a | self.assertFalse(m_log.error.called) |
---|
352 | n/a | |
---|
353 | n/a | @mock.patch('asyncio.base_events.logger') |
---|
354 | n/a | def test_tb_logger_exception_result_retrieved(self, m_log): |
---|
355 | n/a | fut = self._new_future(loop=self.loop) |
---|
356 | n/a | fut.set_exception(RuntimeError('boom')) |
---|
357 | n/a | self.assertRaises(RuntimeError, fut.result) |
---|
358 | n/a | del fut |
---|
359 | n/a | self.assertFalse(m_log.error.called) |
---|
360 | n/a | |
---|
361 | n/a | def test_wrap_future(self): |
---|
362 | n/a | |
---|
363 | n/a | def run(arg): |
---|
364 | n/a | return (arg, threading.get_ident()) |
---|
365 | n/a | ex = concurrent.futures.ThreadPoolExecutor(1) |
---|
366 | n/a | f1 = ex.submit(run, 'oi') |
---|
367 | n/a | f2 = asyncio.wrap_future(f1, loop=self.loop) |
---|
368 | n/a | res, ident = self.loop.run_until_complete(f2) |
---|
369 | n/a | self.assertTrue(asyncio.isfuture(f2)) |
---|
370 | n/a | self.assertEqual(res, 'oi') |
---|
371 | n/a | self.assertNotEqual(ident, threading.get_ident()) |
---|
372 | n/a | |
---|
373 | n/a | def test_wrap_future_future(self): |
---|
374 | n/a | f1 = self._new_future(loop=self.loop) |
---|
375 | n/a | f2 = asyncio.wrap_future(f1) |
---|
376 | n/a | self.assertIs(f1, f2) |
---|
377 | n/a | |
---|
378 | n/a | def test_wrap_future_use_global_loop(self): |
---|
379 | n/a | with mock.patch('asyncio.futures.events') as events: |
---|
380 | n/a | events.get_event_loop = lambda: self.loop |
---|
381 | n/a | def run(arg): |
---|
382 | n/a | return (arg, threading.get_ident()) |
---|
383 | n/a | ex = concurrent.futures.ThreadPoolExecutor(1) |
---|
384 | n/a | f1 = ex.submit(run, 'oi') |
---|
385 | n/a | f2 = asyncio.wrap_future(f1) |
---|
386 | n/a | self.assertIs(self.loop, f2._loop) |
---|
387 | n/a | |
---|
388 | n/a | def test_wrap_future_cancel(self): |
---|
389 | n/a | f1 = concurrent.futures.Future() |
---|
390 | n/a | f2 = asyncio.wrap_future(f1, loop=self.loop) |
---|
391 | n/a | f2.cancel() |
---|
392 | n/a | test_utils.run_briefly(self.loop) |
---|
393 | n/a | self.assertTrue(f1.cancelled()) |
---|
394 | n/a | self.assertTrue(f2.cancelled()) |
---|
395 | n/a | |
---|
396 | n/a | def test_wrap_future_cancel2(self): |
---|
397 | n/a | f1 = concurrent.futures.Future() |
---|
398 | n/a | f2 = asyncio.wrap_future(f1, loop=self.loop) |
---|
399 | n/a | f1.set_result(42) |
---|
400 | n/a | f2.cancel() |
---|
401 | n/a | test_utils.run_briefly(self.loop) |
---|
402 | n/a | self.assertFalse(f1.cancelled()) |
---|
403 | n/a | self.assertEqual(f1.result(), 42) |
---|
404 | n/a | self.assertTrue(f2.cancelled()) |
---|
405 | n/a | |
---|
406 | n/a | def test_future_source_traceback(self): |
---|
407 | n/a | self.loop.set_debug(True) |
---|
408 | n/a | |
---|
409 | n/a | future = self._new_future(loop=self.loop) |
---|
410 | n/a | lineno = sys._getframe().f_lineno - 1 |
---|
411 | n/a | self.assertIsInstance(future._source_traceback, list) |
---|
412 | n/a | self.assertEqual(future._source_traceback[-2][:3], |
---|
413 | n/a | (__file__, |
---|
414 | n/a | lineno, |
---|
415 | n/a | 'test_future_source_traceback')) |
---|
416 | n/a | |
---|
417 | n/a | @mock.patch('asyncio.base_events.logger') |
---|
418 | n/a | def check_future_exception_never_retrieved(self, debug, m_log): |
---|
419 | n/a | self.loop.set_debug(debug) |
---|
420 | n/a | |
---|
421 | n/a | def memory_error(): |
---|
422 | n/a | try: |
---|
423 | n/a | raise MemoryError() |
---|
424 | n/a | except BaseException as exc: |
---|
425 | n/a | return exc |
---|
426 | n/a | exc = memory_error() |
---|
427 | n/a | |
---|
428 | n/a | future = self._new_future(loop=self.loop) |
---|
429 | n/a | future.set_exception(exc) |
---|
430 | n/a | future = None |
---|
431 | n/a | test_utils.run_briefly(self.loop) |
---|
432 | n/a | support.gc_collect() |
---|
433 | n/a | |
---|
434 | n/a | if sys.version_info >= (3, 4): |
---|
435 | n/a | regex = r'^Future exception was never retrieved\n' |
---|
436 | n/a | exc_info = (type(exc), exc, exc.__traceback__) |
---|
437 | n/a | m_log.error.assert_called_once_with(mock.ANY, exc_info=exc_info) |
---|
438 | n/a | else: |
---|
439 | n/a | regex = r'^Future/Task exception was never retrieved\n' |
---|
440 | n/a | m_log.error.assert_called_once_with(mock.ANY, exc_info=False) |
---|
441 | n/a | message = m_log.error.call_args[0][0] |
---|
442 | n/a | self.assertRegex(message, re.compile(regex, re.DOTALL)) |
---|
443 | n/a | |
---|
444 | n/a | def test_future_exception_never_retrieved(self): |
---|
445 | n/a | self.check_future_exception_never_retrieved(False) |
---|
446 | n/a | |
---|
447 | n/a | def test_future_exception_never_retrieved_debug(self): |
---|
448 | n/a | self.check_future_exception_never_retrieved(True) |
---|
449 | n/a | |
---|
450 | n/a | def test_set_result_unless_cancelled(self): |
---|
451 | n/a | fut = self._new_future(loop=self.loop) |
---|
452 | n/a | fut.cancel() |
---|
453 | n/a | futures._set_result_unless_cancelled(fut, 2) |
---|
454 | n/a | self.assertTrue(fut.cancelled()) |
---|
455 | n/a | |
---|
456 | n/a | def test_future_stop_iteration_args(self): |
---|
457 | n/a | fut = self._new_future(loop=self.loop) |
---|
458 | n/a | fut.set_result((1, 2)) |
---|
459 | n/a | fi = fut.__iter__() |
---|
460 | n/a | result = None |
---|
461 | n/a | try: |
---|
462 | n/a | fi.send(None) |
---|
463 | n/a | except StopIteration as ex: |
---|
464 | n/a | result = ex.args[0] |
---|
465 | n/a | else: |
---|
466 | n/a | self.fail('StopIteration was expected') |
---|
467 | n/a | self.assertEqual(result, (1, 2)) |
---|
468 | n/a | |
---|
469 | n/a | def test_future_iter_throw(self): |
---|
470 | n/a | fut = self._new_future(loop=self.loop) |
---|
471 | n/a | fi = iter(fut) |
---|
472 | n/a | self.assertRaises(TypeError, fi.throw, |
---|
473 | n/a | Exception, Exception("elephant"), 32) |
---|
474 | n/a | self.assertRaises(TypeError, fi.throw, |
---|
475 | n/a | Exception("elephant"), Exception("elephant")) |
---|
476 | n/a | self.assertRaises(TypeError, fi.throw, list) |
---|
477 | n/a | |
---|
478 | n/a | |
---|
479 | n/a | @unittest.skipUnless(hasattr(futures, '_CFuture'), |
---|
480 | n/a | 'requires the C _asyncio module') |
---|
481 | n/a | class CFutureTests(BaseFutureTests, test_utils.TestCase): |
---|
482 | n/a | |
---|
483 | n/a | def _new_future(self, *args, **kwargs): |
---|
484 | n/a | return futures._CFuture(*args, **kwargs) |
---|
485 | n/a | |
---|
486 | n/a | |
---|
487 | n/a | class PyFutureTests(BaseFutureTests, test_utils.TestCase): |
---|
488 | n/a | |
---|
489 | n/a | def _new_future(self, *args, **kwargs): |
---|
490 | n/a | return futures._PyFuture(*args, **kwargs) |
---|
491 | n/a | |
---|
492 | n/a | |
---|
493 | n/a | class BaseFutureDoneCallbackTests(): |
---|
494 | n/a | |
---|
495 | n/a | def setUp(self): |
---|
496 | n/a | super().setUp() |
---|
497 | n/a | self.loop = self.new_test_loop() |
---|
498 | n/a | |
---|
499 | n/a | def run_briefly(self): |
---|
500 | n/a | test_utils.run_briefly(self.loop) |
---|
501 | n/a | |
---|
502 | n/a | def _make_callback(self, bag, thing): |
---|
503 | n/a | # Create a callback function that appends thing to bag. |
---|
504 | n/a | def bag_appender(future): |
---|
505 | n/a | bag.append(thing) |
---|
506 | n/a | return bag_appender |
---|
507 | n/a | |
---|
508 | n/a | def _new_future(self): |
---|
509 | n/a | raise NotImplementedError |
---|
510 | n/a | |
---|
511 | n/a | def test_callbacks_invoked_on_set_result(self): |
---|
512 | n/a | bag = [] |
---|
513 | n/a | f = self._new_future() |
---|
514 | n/a | f.add_done_callback(self._make_callback(bag, 42)) |
---|
515 | n/a | f.add_done_callback(self._make_callback(bag, 17)) |
---|
516 | n/a | |
---|
517 | n/a | self.assertEqual(bag, []) |
---|
518 | n/a | f.set_result('foo') |
---|
519 | n/a | |
---|
520 | n/a | self.run_briefly() |
---|
521 | n/a | |
---|
522 | n/a | self.assertEqual(bag, [42, 17]) |
---|
523 | n/a | self.assertEqual(f.result(), 'foo') |
---|
524 | n/a | |
---|
525 | n/a | def test_callbacks_invoked_on_set_exception(self): |
---|
526 | n/a | bag = [] |
---|
527 | n/a | f = self._new_future() |
---|
528 | n/a | f.add_done_callback(self._make_callback(bag, 100)) |
---|
529 | n/a | |
---|
530 | n/a | self.assertEqual(bag, []) |
---|
531 | n/a | exc = RuntimeError() |
---|
532 | n/a | f.set_exception(exc) |
---|
533 | n/a | |
---|
534 | n/a | self.run_briefly() |
---|
535 | n/a | |
---|
536 | n/a | self.assertEqual(bag, [100]) |
---|
537 | n/a | self.assertEqual(f.exception(), exc) |
---|
538 | n/a | |
---|
539 | n/a | def test_remove_done_callback(self): |
---|
540 | n/a | bag = [] |
---|
541 | n/a | f = self._new_future() |
---|
542 | n/a | cb1 = self._make_callback(bag, 1) |
---|
543 | n/a | cb2 = self._make_callback(bag, 2) |
---|
544 | n/a | cb3 = self._make_callback(bag, 3) |
---|
545 | n/a | |
---|
546 | n/a | # Add one cb1 and one cb2. |
---|
547 | n/a | f.add_done_callback(cb1) |
---|
548 | n/a | f.add_done_callback(cb2) |
---|
549 | n/a | |
---|
550 | n/a | # One instance of cb2 removed. Now there's only one cb1. |
---|
551 | n/a | self.assertEqual(f.remove_done_callback(cb2), 1) |
---|
552 | n/a | |
---|
553 | n/a | # Never had any cb3 in there. |
---|
554 | n/a | self.assertEqual(f.remove_done_callback(cb3), 0) |
---|
555 | n/a | |
---|
556 | n/a | # After this there will be 6 instances of cb1 and one of cb2. |
---|
557 | n/a | f.add_done_callback(cb2) |
---|
558 | n/a | for i in range(5): |
---|
559 | n/a | f.add_done_callback(cb1) |
---|
560 | n/a | |
---|
561 | n/a | # Remove all instances of cb1. One cb2 remains. |
---|
562 | n/a | self.assertEqual(f.remove_done_callback(cb1), 6) |
---|
563 | n/a | |
---|
564 | n/a | self.assertEqual(bag, []) |
---|
565 | n/a | f.set_result('foo') |
---|
566 | n/a | |
---|
567 | n/a | self.run_briefly() |
---|
568 | n/a | |
---|
569 | n/a | self.assertEqual(bag, [2]) |
---|
570 | n/a | self.assertEqual(f.result(), 'foo') |
---|
571 | n/a | |
---|
572 | n/a | |
---|
573 | n/a | @unittest.skipUnless(hasattr(futures, '_CFuture'), |
---|
574 | n/a | 'requires the C _asyncio module') |
---|
575 | n/a | class CFutureDoneCallbackTests(BaseFutureDoneCallbackTests, |
---|
576 | n/a | test_utils.TestCase): |
---|
577 | n/a | |
---|
578 | n/a | def _new_future(self): |
---|
579 | n/a | return futures._CFuture(loop=self.loop) |
---|
580 | n/a | |
---|
581 | n/a | |
---|
582 | n/a | class PyFutureDoneCallbackTests(BaseFutureDoneCallbackTests, |
---|
583 | n/a | test_utils.TestCase): |
---|
584 | n/a | |
---|
585 | n/a | def _new_future(self): |
---|
586 | n/a | return futures._PyFuture(loop=self.loop) |
---|
587 | n/a | |
---|
588 | n/a | |
---|
589 | n/a | if __name__ == '__main__': |
---|
590 | n/a | unittest.main() |
---|