asyncio: New error handling API. Issue #20681.

This commit is contained in:
Yury Selivanov 2014-02-18 18:02:19 -05:00
parent 065efc3072
commit ff827f08ac
15 changed files with 491 additions and 99 deletions

View file

@ -25,6 +25,9 @@ from asyncio import test_utils
from asyncio import unix_events
MOCK_ANY = unittest.mock.ANY
@unittest.skipUnless(signal, 'Signals are not supported')
class SelectorEventLoopSignalTests(unittest.TestCase):
@ -45,7 +48,8 @@ class SelectorEventLoopSignalTests(unittest.TestCase):
self.loop._handle_signal(signal.NSIG + 1, ())
def test_handle_signal_cancelled_handler(self):
h = asyncio.Handle(unittest.mock.Mock(), ())
h = asyncio.Handle(unittest.mock.Mock(), (),
loop=unittest.mock.Mock())
h.cancel()
self.loop._signal_handlers[signal.NSIG + 1] = h
self.loop.remove_signal_handler = unittest.mock.Mock()
@ -91,7 +95,7 @@ class SelectorEventLoopSignalTests(unittest.TestCase):
signal.SIGINT, lambda: True)
@unittest.mock.patch('asyncio.unix_events.signal')
@unittest.mock.patch('asyncio.unix_events.logger')
@unittest.mock.patch('asyncio.base_events.logger')
def test_add_signal_handler_install_error2(self, m_logging, m_signal):
m_signal.NSIG = signal.NSIG
@ -108,7 +112,7 @@ class SelectorEventLoopSignalTests(unittest.TestCase):
self.assertEqual(1, m_signal.set_wakeup_fd.call_count)
@unittest.mock.patch('asyncio.unix_events.signal')
@unittest.mock.patch('asyncio.unix_events.logger')
@unittest.mock.patch('asyncio.base_events.logger')
def test_add_signal_handler_install_error3(self, m_logging, m_signal):
class Err(OSError):
errno = errno.EINVAL
@ -153,7 +157,7 @@ class SelectorEventLoopSignalTests(unittest.TestCase):
m_signal.signal.call_args[0])
@unittest.mock.patch('asyncio.unix_events.signal')
@unittest.mock.patch('asyncio.unix_events.logger')
@unittest.mock.patch('asyncio.base_events.logger')
def test_remove_signal_handler_cleanup_error(self, m_logging, m_signal):
m_signal.NSIG = signal.NSIG
self.loop.add_signal_handler(signal.SIGHUP, lambda: True)
@ -347,7 +351,7 @@ class UnixReadPipeTransportTests(unittest.TestCase):
test_utils.run_briefly(self.loop)
self.assertFalse(self.protocol.data_received.called)
@unittest.mock.patch('asyncio.log.logger.exception')
@unittest.mock.patch('asyncio.log.logger.error')
@unittest.mock.patch('os.read')
def test__read_ready_error(self, m_read, m_logexc):
tr = unix_events._UnixReadPipeTransport(
@ -359,7 +363,10 @@ class UnixReadPipeTransportTests(unittest.TestCase):
m_read.assert_called_with(5, tr.max_size)
tr._close.assert_called_with(err)
m_logexc.assert_called_with('Fatal error for %s', tr)
m_logexc.assert_called_with(
test_utils.MockPattern(
'Fatal transport error\nprotocol:.*\ntransport:.*'),
exc_info=(OSError, MOCK_ANY, MOCK_ANY))
@unittest.mock.patch('os.read')
def test_pause_reading(self, m_read):
@ -423,7 +430,7 @@ class UnixReadPipeTransportTests(unittest.TestCase):
self.assertEqual(2, sys.getrefcount(self.protocol),
pprint.pformat(gc.get_referrers(self.protocol)))
self.assertIsNone(tr._loop)
self.assertEqual(2, sys.getrefcount(self.loop),
self.assertEqual(4, sys.getrefcount(self.loop),
pprint.pformat(gc.get_referrers(self.loop)))
def test__call_connection_lost_with_err(self):
@ -436,10 +443,11 @@ class UnixReadPipeTransportTests(unittest.TestCase):
self.pipe.close.assert_called_with()
self.assertIsNone(tr._protocol)
self.assertEqual(2, sys.getrefcount(self.protocol),
pprint.pformat(gc.get_referrers(self.protocol)))
self.assertIsNone(tr._loop)
self.assertEqual(2, sys.getrefcount(self.loop),
self.assertEqual(4, sys.getrefcount(self.loop),
pprint.pformat(gc.get_referrers(self.loop)))
@ -635,7 +643,7 @@ class UnixWritePipeTransportTests(unittest.TestCase):
self.loop.assert_writer(5, tr._write_ready)
self.assertEqual([b'data'], tr._buffer)
@unittest.mock.patch('asyncio.log.logger.exception')
@unittest.mock.patch('asyncio.log.logger.error')
@unittest.mock.patch('os.write')
def test__write_ready_err(self, m_write, m_logexc):
tr = unix_events._UnixWritePipeTransport(
@ -650,7 +658,10 @@ class UnixWritePipeTransportTests(unittest.TestCase):
self.assertFalse(self.loop.readers)
self.assertEqual([], tr._buffer)
self.assertTrue(tr._closing)
m_logexc.assert_called_with('Fatal error for %s', tr)
m_logexc.assert_called_with(
test_utils.MockPattern(
'Fatal transport error\nprotocol:.*\ntransport:.*'),
exc_info=(OSError, MOCK_ANY, MOCK_ANY))
self.assertEqual(1, tr._conn_lost)
test_utils.run_briefly(self.loop)
self.protocol.connection_lost.assert_called_with(err)
@ -702,7 +713,7 @@ class UnixWritePipeTransportTests(unittest.TestCase):
self.assertEqual(2, sys.getrefcount(self.protocol),
pprint.pformat(gc.get_referrers(self.protocol)))
self.assertIsNone(tr._loop)
self.assertEqual(2, sys.getrefcount(self.loop),
self.assertEqual(4, sys.getrefcount(self.loop),
pprint.pformat(gc.get_referrers(self.loop)))
def test__call_connection_lost_with_err(self):
@ -718,7 +729,7 @@ class UnixWritePipeTransportTests(unittest.TestCase):
self.assertEqual(2, sys.getrefcount(self.protocol),
pprint.pformat(gc.get_referrers(self.protocol)))
self.assertIsNone(tr._loop)
self.assertEqual(2, sys.getrefcount(self.loop),
self.assertEqual(4, sys.getrefcount(self.loop),
pprint.pformat(gc.get_referrers(self.loop)))
def test_close(self):
@ -1285,10 +1296,10 @@ class ChildWatcherTestsMixin:
m.waitpid.side_effect = ValueError
with unittest.mock.patch.object(log.logger,
"exception") as m_exception:
'error') as m_error:
self.assertEqual(self.watcher._sig_chld(), None)
self.assertTrue(m_exception.called)
self.assertTrue(m_error.called)
@waitpid_mocks
def test_sigchld_child_reaped_elsewhere(self, m):