(self)
| 238 | self.io_loop.make_current() |
| 239 | |
| 240 | def tearDown(self) -> None: |
| 241 | # Native coroutines tend to produce warnings if they're not |
| 242 | # allowed to run to completion. It's difficult to ensure that |
| 243 | # this always happens in tests, so cancel any tasks that are |
| 244 | # still pending by the time we get here. |
| 245 | asyncio_loop = self.io_loop.asyncio_loop # type: ignore |
| 246 | if hasattr(asyncio, "all_tasks"): # py37 |
| 247 | tasks = asyncio.all_tasks(asyncio_loop) # type: ignore |
| 248 | else: |
| 249 | tasks = asyncio.Task.all_tasks(asyncio_loop) |
| 250 | # Tasks that are done may still appear here and may contain |
| 251 | # non-cancellation exceptions, so filter them out. |
| 252 | tasks = [t for t in tasks if not t.done()] # type: ignore |
| 253 | for t in tasks: |
| 254 | t.cancel() |
| 255 | # Allow the tasks to run and finalize themselves (which means |
| 256 | # raising a CancelledError inside the coroutine). This may |
| 257 | # just transform the "task was destroyed but it is pending" |
| 258 | # warning into a "uncaught CancelledError" warning, but |
| 259 | # catching CancelledErrors in coroutines that may leak is |
| 260 | # simpler than ensuring that no coroutines leak. |
| 261 | if tasks: |
| 262 | done, pending = self.io_loop.run_sync(lambda: asyncio.wait(tasks)) |
| 263 | assert not pending |
| 264 | # If any task failed with anything but a CancelledError, raise it. |
| 265 | for f in done: |
| 266 | try: |
| 267 | f.result() |
| 268 | except asyncio.CancelledError: |
| 269 | pass |
| 270 | |
| 271 | # Clean up Subprocess, so it can be used again with a new ioloop. |
| 272 | Subprocess.uninitialize() |
| 273 | with warnings.catch_warnings(): |
| 274 | warnings.simplefilter("ignore", DeprecationWarning) |
| 275 | self.io_loop.clear_current() |
| 276 | if not isinstance(self.io_loop, _NON_OWNED_IOLOOPS): |
| 277 | # Try to clean up any file descriptors left open in the ioloop. |
| 278 | # This avoids leaks, especially when tests are run repeatedly |
| 279 | # in the same process with autoreload (because curl does not |
| 280 | # set FD_CLOEXEC on its file descriptors) |
| 281 | self.io_loop.close(all_fds=True) |
| 282 | if self.should_close_asyncio_loop: |
| 283 | self.asyncio_loop.close() |
| 284 | super().tearDown() |
| 285 | # In case an exception escaped or the StackContext caught an exception |
| 286 | # when there wasn't a wait() to re-raise it, do so here. |
| 287 | # This is our last chance to raise an exception in a way that the |
| 288 | # unittest machinery understands. |
| 289 | self.__rethrow() |
| 290 | |
| 291 | def get_new_ioloop(self) -> IOLoop: |
| 292 | """Returns the `.IOLoop` to use for this test. |
no test coverage detected