| 96 | |
| 97 | |
| 98 | class TestCase(unittest.TestCase, metaclass=TestCaseMeta): |
| 99 | |
| 100 | @classmethod |
| 101 | def setUpClass(cls): |
| 102 | if os.environ.get('USE_UVLOOP'): |
| 103 | import uvloop |
| 104 | loop = uvloop.new_event_loop() |
| 105 | else: |
| 106 | loop = asyncio.new_event_loop() |
| 107 | |
| 108 | asyncio.set_event_loop(None) |
| 109 | cls.loop = loop |
| 110 | |
| 111 | @classmethod |
| 112 | def tearDownClass(cls): |
| 113 | cls.loop.close() |
| 114 | asyncio.set_event_loop(None) |
| 115 | |
| 116 | def setUp(self): |
| 117 | self.loop.set_exception_handler(self.loop_exception_handler) |
| 118 | self.__unhandled_exceptions = [] |
| 119 | |
| 120 | def tearDown(self): |
| 121 | excs = [] |
| 122 | for exc in self.__unhandled_exceptions: |
| 123 | if isinstance(exc, ConnectionResetError): |
| 124 | texc = traceback.TracebackException.from_exception( |
| 125 | exc, lookup_lines=False) |
| 126 | if texc.stack[-1].name == "_call_connection_lost": |
| 127 | # On Windows calling socket.shutdown may raise |
| 128 | # ConnectionResetError, which happens in the |
| 129 | # finally block of _call_connection_lost. |
| 130 | continue |
| 131 | excs.append(exc) |
| 132 | |
| 133 | if excs: |
| 134 | formatted = [] |
| 135 | |
| 136 | for i, context in enumerate(excs): |
| 137 | formatted.append(self._format_loop_exception(context, i + 1)) |
| 138 | |
| 139 | self.fail( |
| 140 | 'unexpected exceptions in asynchronous code:\n' + |
| 141 | '\n'.join(formatted)) |
| 142 | |
| 143 | @contextlib.contextmanager |
| 144 | def assertRunUnder(self, delta): |
| 145 | st = time.monotonic() |
| 146 | try: |
| 147 | yield |
| 148 | finally: |
| 149 | elapsed = time.monotonic() - st |
| 150 | if elapsed > delta: |
| 151 | raise AssertionError( |
| 152 | 'running block took {:0.3f}s which is longer ' |
| 153 | 'than the expected maximum of {:0.3f}s'.format( |
| 154 | elapsed, delta)) |
| 155 |
nothing calls this directly
no outgoing calls
no test coverage detected