|
5 | 5 | import io |
6 | 6 | import os |
7 | 7 | import platform |
| 8 | +import re |
8 | 9 | import signal |
9 | 10 | import socket |
10 | 11 | try: |
@@ -1737,62 +1738,97 @@ def create_event_loop(self): |
1737 | 1738 | return asyncio.SelectorEventLoop(selectors.SelectSelector()) |
1738 | 1739 |
|
1739 | 1740 |
|
| 1741 | +def noop(): |
| 1742 | + pass |
| 1743 | + |
| 1744 | + |
1740 | 1745 | class HandleTests(unittest.TestCase): |
1741 | 1746 |
|
| 1747 | + def setUp(self): |
| 1748 | + self.loop = None |
| 1749 | + |
1742 | 1750 | def test_handle(self): |
1743 | 1751 | def callback(*args): |
1744 | 1752 | return args |
1745 | 1753 |
|
1746 | 1754 | args = () |
1747 | | - h = asyncio.Handle(callback, args, mock.Mock()) |
| 1755 | + h = asyncio.Handle(callback, args, self.loop) |
1748 | 1756 | self.assertIs(h._callback, callback) |
1749 | 1757 | self.assertIs(h._args, args) |
1750 | 1758 | self.assertFalse(h._cancelled) |
1751 | 1759 |
|
1752 | | - r = repr(h) |
1753 | | - self.assertTrue(r.startswith( |
1754 | | - 'Handle(' |
1755 | | - '<function HandleTests.test_handle.<locals>.callback')) |
1756 | | - self.assertTrue(r.endswith('())')) |
1757 | | - |
1758 | 1760 | h.cancel() |
1759 | 1761 | self.assertTrue(h._cancelled) |
1760 | 1762 |
|
1761 | | - r = repr(h) |
1762 | | - self.assertTrue(r.startswith( |
1763 | | - 'Handle(' |
1764 | | - '<function HandleTests.test_handle.<locals>.callback')) |
1765 | | - self.assertTrue(r.endswith('())<cancelled>'), r) |
1766 | | - |
1767 | 1763 | def test_handle_from_handle(self): |
1768 | 1764 | def callback(*args): |
1769 | 1765 | return args |
1770 | | - m_loop = object() |
1771 | | - h1 = asyncio.Handle(callback, (), loop=m_loop) |
| 1766 | + h1 = asyncio.Handle(callback, (), loop=self.loop) |
1772 | 1767 | self.assertRaises( |
1773 | | - AssertionError, asyncio.Handle, h1, (), m_loop) |
| 1768 | + AssertionError, asyncio.Handle, h1, (), self.loop) |
1774 | 1769 |
|
1775 | 1770 | def test_callback_with_exception(self): |
1776 | 1771 | def callback(): |
1777 | 1772 | raise ValueError() |
1778 | 1773 |
|
1779 | | - m_loop = mock.Mock() |
1780 | | - m_loop.call_exception_handler = mock.Mock() |
| 1774 | + self.loop = mock.Mock() |
| 1775 | + self.loop.call_exception_handler = mock.Mock() |
1781 | 1776 |
|
1782 | | - h = asyncio.Handle(callback, (), m_loop) |
| 1777 | + h = asyncio.Handle(callback, (), self.loop) |
1783 | 1778 | h._run() |
1784 | 1779 |
|
1785 | | - m_loop.call_exception_handler.assert_called_with({ |
| 1780 | + self.loop.call_exception_handler.assert_called_with({ |
1786 | 1781 | 'message': test_utils.MockPattern('Exception in callback.*'), |
1787 | 1782 | 'exception': mock.ANY, |
1788 | 1783 | 'handle': h |
1789 | 1784 | }) |
1790 | 1785 |
|
1791 | 1786 | def test_handle_weakref(self): |
1792 | 1787 | wd = weakref.WeakValueDictionary() |
1793 | | - h = asyncio.Handle(lambda: None, (), object()) |
| 1788 | + h = asyncio.Handle(lambda: None, (), self.loop) |
1794 | 1789 | wd['h'] = h # Would fail without __weakref__ slot. |
1795 | 1790 |
|
| 1791 | + def test_repr(self): |
| 1792 | + # simple function |
| 1793 | + h = asyncio.Handle(noop, (), self.loop) |
| 1794 | + src = test_utils.get_function_source(noop) |
| 1795 | + self.assertEqual(repr(h), |
| 1796 | + 'Handle(noop at %s:%s, ())' % src) |
| 1797 | + |
| 1798 | + # cancelled handle |
| 1799 | + h.cancel() |
| 1800 | + self.assertEqual(repr(h), |
| 1801 | + 'Handle(noop at %s:%s, ())<cancelled>' % src) |
| 1802 | + |
| 1803 | + # decorated function |
| 1804 | + cb = asyncio.coroutine(noop) |
| 1805 | + h = asyncio.Handle(cb, (), self.loop) |
| 1806 | + self.assertEqual(repr(h), |
| 1807 | + 'Handle(noop at %s:%s, ())' % src) |
| 1808 | + |
| 1809 | + # partial function |
| 1810 | + cb = functools.partial(noop) |
| 1811 | + h = asyncio.Handle(cb, (), self.loop) |
| 1812 | + filename, lineno = src |
| 1813 | + regex = (r'^Handle\(functools.partial\(' |
| 1814 | + r'<function noop .*>\) at %s:%s, ' |
| 1815 | + r'\(\)\)$' % (re.escape(filename), lineno)) |
| 1816 | + self.assertRegex(repr(h), regex) |
| 1817 | + |
| 1818 | + # partial method |
| 1819 | + if sys.version_info >= (3, 4): |
| 1820 | + method = HandleTests.test_repr |
| 1821 | + cb = functools.partialmethod(method) |
| 1822 | + src = test_utils.get_function_source(method) |
| 1823 | + h = asyncio.Handle(cb, (), self.loop) |
| 1824 | + |
| 1825 | + filename, lineno = src |
| 1826 | + regex = (r'^Handle\(functools.partialmethod\(' |
| 1827 | + r'<function HandleTests.test_repr .*>, , \) at %s:%s, ' |
| 1828 | + r'\(\)\)$' % (re.escape(filename), lineno)) |
| 1829 | + self.assertRegex(repr(h), regex) |
| 1830 | + |
| 1831 | + |
1796 | 1832 |
|
1797 | 1833 | class TimerTests(unittest.TestCase): |
1798 | 1834 |
|
|
0 commit comments