11"""Vendored fork of pytest_tornasync from
22 https://github.com/eukaryote/pytest-tornasync/blob/9f1bdeec3eb5816e0183f975ca65b5f6f29fbfbb/src/pytest_tornasync/plugin.py
33"""
4+ import asyncio
45from contextlib import closing
5- from inspect import iscoroutinefunction
66
77try :
88 import tornado .ioloop
1414import pytest
1515
1616# mypy: disable-error-code="no-untyped-call"
17+ # Bring in local plugins.
18+ from pytest_jupyter .jupyter_core import * # noqa: F403
1719
1820
19- @pytest .hookimpl (tryfirst = True )
20- def pytest_pycollect_makeitem (collector , name , obj ):
21- """Custom pytest collection hook."""
22- if collector .funcnamefilter (name ) and iscoroutinefunction (obj ):
23- return list (collector ._genfunctions (name , obj ))
24- return None
21+ @pytest .fixture ()
22+ def io_loop (jp_asyncio_loop ):
23+ """Get the current tornado event loop."""
24+ return tornado .ioloop .IOLoop .current ()
25+
2526
27+ @pytest .fixture ()
28+ def http_server (jp_asyncio_loop , http_server_port , jp_web_app ):
29+ """Start a tornado HTTP server that listens on all available interfaces."""
2630
27- @ pytest . hookimpl ( tryfirst = True )
28- def pytest_pyfunc_call ( pyfuncitem ):
29- """Custom pytest function call hook."""
30- funcargs = pyfuncitem . funcargs
31- testargs = { arg : funcargs [ arg ] for arg in pyfuncitem . _fixtureinfo . argnames }
31+ async def get_server ():
32+ """Get a server asynchronously."""
33+ server = tornado . httpserver . HTTPServer ( jp_web_app )
34+ server . add_socket ( http_server_port [ 0 ])
35+ return server
3236
33- if not iscoroutinefunction ( pyfuncitem . obj ):
34- pyfuncitem . obj ( ** testargs )
35- return True
37+ server = jp_asyncio_loop . run_until_complete ( get_server ())
38+ yield server
39+ server . stop ()
3640
37- try :
38- loop = funcargs ["io_loop" ]
39- except KeyError :
40- loop = tornado .ioloop .IOLoop .current ()
41+ if hasattr (server , "close_all_connections" ):
42+ try :
43+ jp_asyncio_loop .run_until_complete (server .close_all_connections ())
44+ except asyncio .TimeoutError :
45+ pass
4146
42- loop .run_sync (lambda : pyfuncitem .obj (** testargs ))
43- return True
47+ http_server_port [0 ].close ()
4448
4549
4650@pytest .fixture ()
@@ -52,7 +56,7 @@ def http_server_port():
5256
5357
5458@pytest .fixture ()
55- def http_server_client (http_server , io_loop ):
59+ def http_server_client (http_server , jp_asyncio_loop ):
5660 """
5761 Create an asynchronous HTTP client that can fetch from `http_server`.
5862 """
@@ -61,7 +65,7 @@ async def get_client():
6165 """Get a client."""
6266 return AsyncHTTPServerClient (http_server = http_server )
6367
64- client = io_loop . run_sync (get_client )
68+ client = jp_asyncio_loop . run_until_complete (get_client () )
6569 with closing (client ) as context :
6670 yield context
6771
0 commit comments