2013-10-17 17:40:50 -03:00
|
|
|
"""Tests for futures.py."""
|
|
|
|
|
|
|
|
import concurrent.futures
|
2014-06-25 16:41:58 -03:00
|
|
|
import re
|
2014-06-27 08:52:20 -03:00
|
|
|
import sys
|
2013-10-17 17:40:50 -03:00
|
|
|
import threading
|
|
|
|
import unittest
|
2014-02-26 05:25:02 -04:00
|
|
|
from unittest import mock
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
import asyncio
|
2013-10-17 17:40:50 -03:00
|
|
|
from asyncio import test_utils
|
2014-12-18 07:29:53 -04:00
|
|
|
try:
|
|
|
|
from test import support # gc_collect
|
|
|
|
except ImportError:
|
|
|
|
from asyncio import test_support as support
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
|
|
|
|
def _fakefunc(f):
|
|
|
|
return f
|
|
|
|
|
2014-06-25 16:41:58 -03:00
|
|
|
def first_cb():
|
|
|
|
pass
|
|
|
|
|
|
|
|
def last_cb():
|
|
|
|
pass
|
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-06-17 20:36:32 -03:00
|
|
|
class FutureTests(test_utils.TestCase):
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def setUp(self):
|
2014-06-17 20:36:32 -03:00
|
|
|
self.loop = self.new_test_loop()
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_initial_state(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
f = asyncio.Future(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertFalse(f.cancelled())
|
|
|
|
self.assertFalse(f.done())
|
|
|
|
f.cancel()
|
|
|
|
self.assertTrue(f.cancelled())
|
|
|
|
|
|
|
|
def test_init_constructor_default_loop(self):
|
2014-06-17 20:36:32 -03:00
|
|
|
asyncio.set_event_loop(self.loop)
|
|
|
|
f = asyncio.Future()
|
|
|
|
self.assertIs(f._loop, self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_constructor_positional(self):
|
2014-02-18 23:27:48 -04:00
|
|
|
# Make sure Future doesn't accept a positional argument
|
2014-01-25 10:32:06 -04:00
|
|
|
self.assertRaises(TypeError, asyncio.Future, 42)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_cancel(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
f = asyncio.Future(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertTrue(f.cancel())
|
|
|
|
self.assertTrue(f.cancelled())
|
|
|
|
self.assertTrue(f.done())
|
2014-01-25 10:32:06 -04:00
|
|
|
self.assertRaises(asyncio.CancelledError, f.result)
|
|
|
|
self.assertRaises(asyncio.CancelledError, f.exception)
|
|
|
|
self.assertRaises(asyncio.InvalidStateError, f.set_result, None)
|
|
|
|
self.assertRaises(asyncio.InvalidStateError, f.set_exception, None)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertFalse(f.cancel())
|
|
|
|
|
|
|
|
def test_result(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
f = asyncio.Future(loop=self.loop)
|
|
|
|
self.assertRaises(asyncio.InvalidStateError, f.result)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
f.set_result(42)
|
|
|
|
self.assertFalse(f.cancelled())
|
|
|
|
self.assertTrue(f.done())
|
|
|
|
self.assertEqual(f.result(), 42)
|
|
|
|
self.assertEqual(f.exception(), None)
|
2014-01-25 10:32:06 -04:00
|
|
|
self.assertRaises(asyncio.InvalidStateError, f.set_result, None)
|
|
|
|
self.assertRaises(asyncio.InvalidStateError, f.set_exception, None)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertFalse(f.cancel())
|
|
|
|
|
|
|
|
def test_exception(self):
|
|
|
|
exc = RuntimeError()
|
2014-01-25 10:32:06 -04:00
|
|
|
f = asyncio.Future(loop=self.loop)
|
|
|
|
self.assertRaises(asyncio.InvalidStateError, f.exception)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
f.set_exception(exc)
|
|
|
|
self.assertFalse(f.cancelled())
|
|
|
|
self.assertTrue(f.done())
|
|
|
|
self.assertRaises(RuntimeError, f.result)
|
|
|
|
self.assertEqual(f.exception(), exc)
|
2014-01-25 10:32:06 -04:00
|
|
|
self.assertRaises(asyncio.InvalidStateError, f.set_result, None)
|
|
|
|
self.assertRaises(asyncio.InvalidStateError, f.set_exception, None)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertFalse(f.cancel())
|
|
|
|
|
2014-01-30 20:01:54 -04:00
|
|
|
def test_exception_class(self):
|
|
|
|
f = asyncio.Future(loop=self.loop)
|
|
|
|
f.set_exception(RuntimeError)
|
|
|
|
self.assertIsInstance(f.exception(), RuntimeError)
|
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
def test_yield_from_twice(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
f = asyncio.Future(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def fixture():
|
|
|
|
yield 'A'
|
|
|
|
x = yield from f
|
|
|
|
yield 'B', x
|
|
|
|
y = yield from f
|
|
|
|
yield 'C', y
|
|
|
|
|
|
|
|
g = fixture()
|
|
|
|
self.assertEqual(next(g), 'A') # yield 'A'.
|
|
|
|
self.assertEqual(next(g), f) # First yield from f.
|
|
|
|
f.set_result(42)
|
|
|
|
self.assertEqual(next(g), ('B', 42)) # yield 'B', x.
|
|
|
|
# The second "yield from f" does not yield f.
|
|
|
|
self.assertEqual(next(g), ('C', 42)) # yield 'C', y.
|
|
|
|
|
2014-06-25 16:41:58 -03:00
|
|
|
def test_future_repr(self):
|
2014-07-29 07:58:23 -03:00
|
|
|
self.loop.set_debug(True)
|
|
|
|
f_pending_debug = asyncio.Future(loop=self.loop)
|
|
|
|
frame = f_pending_debug._source_traceback[-1]
|
|
|
|
self.assertEqual(repr(f_pending_debug),
|
|
|
|
'<Future pending created at %s:%s>'
|
|
|
|
% (frame[0], frame[1]))
|
|
|
|
f_pending_debug.cancel()
|
|
|
|
|
|
|
|
self.loop.set_debug(False)
|
2014-01-25 10:32:06 -04:00
|
|
|
f_pending = asyncio.Future(loop=self.loop)
|
2014-06-25 16:41:58 -03:00
|
|
|
self.assertEqual(repr(f_pending), '<Future pending>')
|
2013-10-17 17:40:50 -03:00
|
|
|
f_pending.cancel()
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
f_cancelled = asyncio.Future(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
f_cancelled.cancel()
|
2014-06-25 16:41:58 -03:00
|
|
|
self.assertEqual(repr(f_cancelled), '<Future cancelled>')
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
f_result = asyncio.Future(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
f_result.set_result(4)
|
2014-06-25 16:41:58 -03:00
|
|
|
self.assertEqual(repr(f_result), '<Future finished result=4>')
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertEqual(f_result.result(), 4)
|
|
|
|
|
|
|
|
exc = RuntimeError()
|
2014-01-25 10:32:06 -04:00
|
|
|
f_exception = asyncio.Future(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
f_exception.set_exception(exc)
|
2014-06-25 16:41:58 -03:00
|
|
|
self.assertEqual(repr(f_exception), '<Future finished exception=RuntimeError()>')
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertIs(f_exception.exception(), exc)
|
|
|
|
|
2014-06-25 16:41:58 -03:00
|
|
|
def func_repr(func):
|
|
|
|
filename, lineno = test_utils.get_function_source(func)
|
|
|
|
text = '%s() at %s:%s' % (func.__qualname__, filename, lineno)
|
|
|
|
return re.escape(text)
|
|
|
|
|
|
|
|
f_one_callbacks = asyncio.Future(loop=self.loop)
|
|
|
|
f_one_callbacks.add_done_callback(_fakefunc)
|
|
|
|
fake_repr = func_repr(_fakefunc)
|
|
|
|
self.assertRegex(repr(f_one_callbacks),
|
|
|
|
r'<Future pending cb=\[%s\]>' % fake_repr)
|
|
|
|
f_one_callbacks.cancel()
|
|
|
|
self.assertEqual(repr(f_one_callbacks),
|
|
|
|
'<Future cancelled>')
|
|
|
|
|
|
|
|
f_two_callbacks = asyncio.Future(loop=self.loop)
|
|
|
|
f_two_callbacks.add_done_callback(first_cb)
|
|
|
|
f_two_callbacks.add_done_callback(last_cb)
|
|
|
|
first_repr = func_repr(first_cb)
|
|
|
|
last_repr = func_repr(last_cb)
|
|
|
|
self.assertRegex(repr(f_two_callbacks),
|
|
|
|
r'<Future pending cb=\[%s, %s\]>'
|
|
|
|
% (first_repr, last_repr))
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
f_many_callbacks = asyncio.Future(loop=self.loop)
|
2014-06-25 16:41:58 -03:00
|
|
|
f_many_callbacks.add_done_callback(first_cb)
|
|
|
|
for i in range(8):
|
2013-10-17 17:40:50 -03:00
|
|
|
f_many_callbacks.add_done_callback(_fakefunc)
|
2014-06-25 16:41:58 -03:00
|
|
|
f_many_callbacks.add_done_callback(last_cb)
|
|
|
|
cb_regex = r'%s, <8 more>, %s' % (first_repr, last_repr)
|
|
|
|
self.assertRegex(repr(f_many_callbacks),
|
|
|
|
r'<Future pending cb=\[%s\]>' % cb_regex)
|
2013-10-17 17:40:50 -03:00
|
|
|
f_many_callbacks.cancel()
|
2014-06-25 16:41:58 -03:00
|
|
|
self.assertEqual(repr(f_many_callbacks),
|
|
|
|
'<Future cancelled>')
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_copy_state(self):
|
|
|
|
# Test the internal _copy_state method since it's being directly
|
|
|
|
# invoked in other modules.
|
2014-01-25 10:32:06 -04:00
|
|
|
f = asyncio.Future(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
f.set_result(10)
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
newf = asyncio.Future(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
newf._copy_state(f)
|
|
|
|
self.assertTrue(newf.done())
|
|
|
|
self.assertEqual(newf.result(), 10)
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
f_exception = asyncio.Future(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
f_exception.set_exception(RuntimeError())
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
newf_exception = asyncio.Future(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
newf_exception._copy_state(f_exception)
|
|
|
|
self.assertTrue(newf_exception.done())
|
|
|
|
self.assertRaises(RuntimeError, newf_exception.result)
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
f_cancelled = asyncio.Future(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
f_cancelled.cancel()
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
newf_cancelled = asyncio.Future(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
newf_cancelled._copy_state(f_cancelled)
|
|
|
|
self.assertTrue(newf_cancelled.cancelled())
|
|
|
|
|
|
|
|
def test_iter(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
fut = asyncio.Future(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def coro():
|
|
|
|
yield from fut
|
|
|
|
|
|
|
|
def test():
|
|
|
|
arg1, arg2 = coro()
|
|
|
|
|
|
|
|
self.assertRaises(AssertionError, test)
|
|
|
|
fut.cancel()
|
|
|
|
|
2014-02-26 05:25:02 -04:00
|
|
|
@mock.patch('asyncio.base_events.logger')
|
2013-10-17 17:40:50 -03:00
|
|
|
def test_tb_logger_abandoned(self, m_log):
|
2014-01-25 10:32:06 -04:00
|
|
|
fut = asyncio.Future(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
del fut
|
|
|
|
self.assertFalse(m_log.error.called)
|
|
|
|
|
2014-02-26 05:25:02 -04:00
|
|
|
@mock.patch('asyncio.base_events.logger')
|
2013-10-17 17:40:50 -03:00
|
|
|
def test_tb_logger_result_unretrieved(self, m_log):
|
2014-01-25 10:32:06 -04:00
|
|
|
fut = asyncio.Future(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
fut.set_result(42)
|
|
|
|
del fut
|
|
|
|
self.assertFalse(m_log.error.called)
|
|
|
|
|
2014-02-26 05:25:02 -04:00
|
|
|
@mock.patch('asyncio.base_events.logger')
|
2013-10-17 17:40:50 -03:00
|
|
|
def test_tb_logger_result_retrieved(self, m_log):
|
2014-01-25 10:32:06 -04:00
|
|
|
fut = asyncio.Future(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
fut.set_result(42)
|
|
|
|
fut.result()
|
|
|
|
del fut
|
|
|
|
self.assertFalse(m_log.error.called)
|
|
|
|
|
2014-02-26 05:25:02 -04:00
|
|
|
@mock.patch('asyncio.base_events.logger')
|
2013-10-17 17:40:50 -03:00
|
|
|
def test_tb_logger_exception_unretrieved(self, m_log):
|
2014-01-25 10:32:06 -04:00
|
|
|
fut = asyncio.Future(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
fut.set_exception(RuntimeError('boom'))
|
|
|
|
del fut
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.assertTrue(m_log.error.called)
|
|
|
|
|
2014-02-26 05:25:02 -04:00
|
|
|
@mock.patch('asyncio.base_events.logger')
|
2013-10-17 17:40:50 -03:00
|
|
|
def test_tb_logger_exception_retrieved(self, m_log):
|
2014-01-25 10:32:06 -04:00
|
|
|
fut = asyncio.Future(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
fut.set_exception(RuntimeError('boom'))
|
|
|
|
fut.exception()
|
|
|
|
del fut
|
|
|
|
self.assertFalse(m_log.error.called)
|
|
|
|
|
2014-02-26 05:25:02 -04:00
|
|
|
@mock.patch('asyncio.base_events.logger')
|
2013-10-17 17:40:50 -03:00
|
|
|
def test_tb_logger_exception_result_retrieved(self, m_log):
|
2014-01-25 10:32:06 -04:00
|
|
|
fut = asyncio.Future(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
fut.set_exception(RuntimeError('boom'))
|
|
|
|
self.assertRaises(RuntimeError, fut.result)
|
|
|
|
del fut
|
|
|
|
self.assertFalse(m_log.error.called)
|
|
|
|
|
|
|
|
def test_wrap_future(self):
|
|
|
|
|
|
|
|
def run(arg):
|
|
|
|
return (arg, threading.get_ident())
|
|
|
|
ex = concurrent.futures.ThreadPoolExecutor(1)
|
|
|
|
f1 = ex.submit(run, 'oi')
|
2014-01-25 10:32:06 -04:00
|
|
|
f2 = asyncio.wrap_future(f1, loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
res, ident = self.loop.run_until_complete(f2)
|
2014-01-25 10:32:06 -04:00
|
|
|
self.assertIsInstance(f2, asyncio.Future)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertEqual(res, 'oi')
|
|
|
|
self.assertNotEqual(ident, threading.get_ident())
|
|
|
|
|
|
|
|
def test_wrap_future_future(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
f1 = asyncio.Future(loop=self.loop)
|
|
|
|
f2 = asyncio.wrap_future(f1)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertIs(f1, f2)
|
|
|
|
|
2014-02-26 05:25:02 -04:00
|
|
|
@mock.patch('asyncio.futures.events')
|
2013-10-17 17:40:50 -03:00
|
|
|
def test_wrap_future_use_global_loop(self, m_events):
|
|
|
|
def run(arg):
|
|
|
|
return (arg, threading.get_ident())
|
|
|
|
ex = concurrent.futures.ThreadPoolExecutor(1)
|
|
|
|
f1 = ex.submit(run, 'oi')
|
2014-01-25 10:32:06 -04:00
|
|
|
f2 = asyncio.wrap_future(f1)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertIs(m_events.get_event_loop.return_value, f2._loop)
|
|
|
|
|
2013-11-22 15:47:22 -04:00
|
|
|
def test_wrap_future_cancel(self):
|
|
|
|
f1 = concurrent.futures.Future()
|
2014-01-25 10:32:06 -04:00
|
|
|
f2 = asyncio.wrap_future(f1, loop=self.loop)
|
2013-11-22 15:47:22 -04:00
|
|
|
f2.cancel()
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.assertTrue(f1.cancelled())
|
|
|
|
self.assertTrue(f2.cancelled())
|
|
|
|
|
|
|
|
def test_wrap_future_cancel2(self):
|
|
|
|
f1 = concurrent.futures.Future()
|
2014-01-25 10:32:06 -04:00
|
|
|
f2 = asyncio.wrap_future(f1, loop=self.loop)
|
2013-11-22 15:47:22 -04:00
|
|
|
f1.set_result(42)
|
|
|
|
f2.cancel()
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.assertFalse(f1.cancelled())
|
|
|
|
self.assertEqual(f1.result(), 42)
|
|
|
|
self.assertTrue(f2.cancelled())
|
|
|
|
|
2014-06-27 08:52:20 -03:00
|
|
|
def test_future_source_traceback(self):
|
|
|
|
self.loop.set_debug(True)
|
|
|
|
|
|
|
|
future = asyncio.Future(loop=self.loop)
|
|
|
|
lineno = sys._getframe().f_lineno - 1
|
|
|
|
self.assertIsInstance(future._source_traceback, list)
|
|
|
|
self.assertEqual(future._source_traceback[-1][:3],
|
|
|
|
(__file__,
|
|
|
|
lineno,
|
|
|
|
'test_future_source_traceback'))
|
|
|
|
|
|
|
|
@mock.patch('asyncio.base_events.logger')
|
2014-11-20 09:16:31 -04:00
|
|
|
def check_future_exception_never_retrieved(self, debug, m_log):
|
|
|
|
self.loop.set_debug(debug)
|
2014-06-27 08:52:20 -03:00
|
|
|
|
2014-07-08 06:29:25 -03:00
|
|
|
def memory_error():
|
2014-06-27 08:52:20 -03:00
|
|
|
try:
|
|
|
|
raise MemoryError()
|
|
|
|
except BaseException as exc:
|
|
|
|
return exc
|
2014-07-08 06:29:25 -03:00
|
|
|
exc = memory_error()
|
2014-06-27 08:52:20 -03:00
|
|
|
|
|
|
|
future = asyncio.Future(loop=self.loop)
|
2014-11-20 09:16:31 -04:00
|
|
|
if debug:
|
|
|
|
source_traceback = future._source_traceback
|
2014-06-27 08:52:20 -03:00
|
|
|
future.set_exception(exc)
|
|
|
|
future = None
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
support.gc_collect()
|
|
|
|
|
|
|
|
if sys.version_info >= (3, 4):
|
2014-11-20 09:16:31 -04:00
|
|
|
if debug:
|
|
|
|
frame = source_traceback[-1]
|
|
|
|
regex = (r'^Future exception was never retrieved\n'
|
|
|
|
r'future: <Future finished exception=MemoryError\(\) created at {filename}:{lineno}>\n'
|
|
|
|
r'source_traceback: Object created at \(most recent call last\):\n'
|
|
|
|
r' File'
|
|
|
|
r'.*\n'
|
|
|
|
r' File "{filename}", line {lineno}, in check_future_exception_never_retrieved\n'
|
|
|
|
r' future = asyncio\.Future\(loop=self\.loop\)$'
|
|
|
|
).format(filename=re.escape(frame[0]), lineno=frame[1])
|
|
|
|
else:
|
|
|
|
regex = (r'^Future exception was never retrieved\n'
|
|
|
|
r'future: <Future finished exception=MemoryError\(\)>$'
|
|
|
|
)
|
2014-06-27 08:52:20 -03:00
|
|
|
exc_info = (type(exc), exc, exc.__traceback__)
|
|
|
|
m_log.error.assert_called_once_with(mock.ANY, exc_info=exc_info)
|
|
|
|
else:
|
2014-11-20 09:16:31 -04:00
|
|
|
if debug:
|
|
|
|
frame = source_traceback[-1]
|
|
|
|
regex = (r'^Future/Task exception was never retrieved\n'
|
|
|
|
r'Future/Task created at \(most recent call last\):\n'
|
|
|
|
r' File'
|
|
|
|
r'.*\n'
|
|
|
|
r' File "{filename}", line {lineno}, in check_future_exception_never_retrieved\n'
|
|
|
|
r' future = asyncio\.Future\(loop=self\.loop\)\n'
|
|
|
|
r'Traceback \(most recent call last\):\n'
|
|
|
|
r'.*\n'
|
|
|
|
r'MemoryError$'
|
|
|
|
).format(filename=re.escape(frame[0]), lineno=frame[1])
|
|
|
|
else:
|
|
|
|
regex = (r'^Future/Task exception was never retrieved\n'
|
|
|
|
r'Traceback \(most recent call last\):\n'
|
|
|
|
r'.*\n'
|
|
|
|
r'MemoryError$'
|
|
|
|
)
|
2014-06-27 08:52:20 -03:00
|
|
|
m_log.error.assert_called_once_with(mock.ANY, exc_info=False)
|
|
|
|
message = m_log.error.call_args[0][0]
|
|
|
|
self.assertRegex(message, re.compile(regex, re.DOTALL))
|
|
|
|
|
2014-11-20 09:16:31 -04:00
|
|
|
def test_future_exception_never_retrieved(self):
|
|
|
|
self.check_future_exception_never_retrieved(False)
|
|
|
|
|
|
|
|
def test_future_exception_never_retrieved_debug(self):
|
|
|
|
self.check_future_exception_never_retrieved(True)
|
|
|
|
|
2014-07-05 10:29:41 -03:00
|
|
|
def test_set_result_unless_cancelled(self):
|
|
|
|
fut = asyncio.Future(loop=self.loop)
|
|
|
|
fut.cancel()
|
|
|
|
fut._set_result_unless_cancelled(2)
|
|
|
|
self.assertTrue(fut.cancelled())
|
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-06-17 20:36:32 -03:00
|
|
|
class FutureDoneCallbackTests(test_utils.TestCase):
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def setUp(self):
|
2014-06-17 20:36:32 -03:00
|
|
|
self.loop = self.new_test_loop()
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def run_briefly(self):
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
|
|
|
|
def _make_callback(self, bag, thing):
|
|
|
|
# Create a callback function that appends thing to bag.
|
|
|
|
def bag_appender(future):
|
|
|
|
bag.append(thing)
|
|
|
|
return bag_appender
|
|
|
|
|
|
|
|
def _new_future(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
return asyncio.Future(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_callbacks_invoked_on_set_result(self):
|
|
|
|
bag = []
|
|
|
|
f = self._new_future()
|
|
|
|
f.add_done_callback(self._make_callback(bag, 42))
|
|
|
|
f.add_done_callback(self._make_callback(bag, 17))
|
|
|
|
|
|
|
|
self.assertEqual(bag, [])
|
|
|
|
f.set_result('foo')
|
|
|
|
|
|
|
|
self.run_briefly()
|
|
|
|
|
|
|
|
self.assertEqual(bag, [42, 17])
|
|
|
|
self.assertEqual(f.result(), 'foo')
|
|
|
|
|
|
|
|
def test_callbacks_invoked_on_set_exception(self):
|
|
|
|
bag = []
|
|
|
|
f = self._new_future()
|
|
|
|
f.add_done_callback(self._make_callback(bag, 100))
|
|
|
|
|
|
|
|
self.assertEqual(bag, [])
|
|
|
|
exc = RuntimeError()
|
|
|
|
f.set_exception(exc)
|
|
|
|
|
|
|
|
self.run_briefly()
|
|
|
|
|
|
|
|
self.assertEqual(bag, [100])
|
|
|
|
self.assertEqual(f.exception(), exc)
|
|
|
|
|
|
|
|
def test_remove_done_callback(self):
|
|
|
|
bag = []
|
|
|
|
f = self._new_future()
|
|
|
|
cb1 = self._make_callback(bag, 1)
|
|
|
|
cb2 = self._make_callback(bag, 2)
|
|
|
|
cb3 = self._make_callback(bag, 3)
|
|
|
|
|
|
|
|
# Add one cb1 and one cb2.
|
|
|
|
f.add_done_callback(cb1)
|
|
|
|
f.add_done_callback(cb2)
|
|
|
|
|
|
|
|
# One instance of cb2 removed. Now there's only one cb1.
|
|
|
|
self.assertEqual(f.remove_done_callback(cb2), 1)
|
|
|
|
|
|
|
|
# Never had any cb3 in there.
|
|
|
|
self.assertEqual(f.remove_done_callback(cb3), 0)
|
|
|
|
|
|
|
|
# After this there will be 6 instances of cb1 and one of cb2.
|
|
|
|
f.add_done_callback(cb2)
|
|
|
|
for i in range(5):
|
|
|
|
f.add_done_callback(cb1)
|
|
|
|
|
|
|
|
# Remove all instances of cb1. One cb2 remains.
|
|
|
|
self.assertEqual(f.remove_done_callback(cb1), 6)
|
|
|
|
|
|
|
|
self.assertEqual(bag, [])
|
|
|
|
f.set_result('foo')
|
|
|
|
|
|
|
|
self.run_briefly()
|
|
|
|
|
|
|
|
self.assertEqual(bag, [2])
|
|
|
|
self.assertEqual(f.result(), 'foo')
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|