| 1 | from contextlib import closing, contextmanager |
| 2 | import inspect |
| 3 | from inspect import iscoroutinefunction |
| 4 | |
| 5 | import tornado.ioloop |
| 6 | import tornado.testing |
| 7 | import tornado.simple_httpclient |
| 8 | |
| 9 | import pytest |
| 10 | |
| 11 | |
| 12 | def get_test_timeout(pyfuncitem): |
| 13 | timeout = pyfuncitem.config.option.async_test_timeout |
| 14 | marker = pyfuncitem.get_closest_marker("timeout") |
| 15 | if marker: |
| 16 | timeout = marker.kwargs.get("seconds", timeout) |
| 17 | return timeout |
| 18 | |
| 19 | |
| 20 | def pytest_addoption(parser): |
| 21 | parser.addoption( |
| 22 | "--async-test-timeout", |
| 23 | type=float, |
| 24 | help=("timeout in seconds before failing the test " "(default is no timeout)"), |
| 25 | ) |
| 26 | parser.addoption( |
| 27 | "--app-fixture", |
| 28 | default="app", |
| 29 | help=("fixture name returning a tornado application " '(default is "app")'), |
| 30 | ) |
| 31 | |
| 32 | |
| 33 | @pytest.mark.tryfirst |
| 34 | def pytest_pycollect_makeitem(collector, name, obj): |
| 35 | if collector.funcnamefilter(name) and iscoroutinefunction(obj): |
| 36 | return list(collector._genfunctions(name, obj)) |
| 37 | |
| 38 | |
| 39 | @pytest.mark.tryfirst |
| 40 | def pytest_pyfunc_call(pyfuncitem): |
| 41 | funcargs = pyfuncitem.funcargs |
| 42 | testargs = {arg: funcargs[arg] for arg in pyfuncitem._fixtureinfo.argnames} |
| 43 | |
| 44 | if not iscoroutinefunction(pyfuncitem.obj): |
| 45 | pyfuncitem.obj(**testargs) |
| 46 | return True |
| 47 | |
| 48 | try: |
| 49 | loop = funcargs["io_loop"] |
| 50 | except KeyError: |
| 51 | loop = tornado.ioloop.IOLoop.current() |
| 52 | |
| 53 | loop.run_sync( |
| 54 | lambda: pyfuncitem.obj(**testargs), timeout=get_test_timeout(pyfuncitem) |
| 55 | ) |
| 56 | return True |
| 57 | |
| 58 | |
| 59 | @pytest.fixture |
| 60 | def io_loop(): |
| 61 | """ |
| 62 | Create new io loop for each test, and tear it down after. |
| 63 | """ |
| 64 | loop = tornado.ioloop.IOLoop() |
| 65 | loop.make_current() |
| 66 | yield loop |
| 67 | loop.clear_current() |
| 68 | loop.close(all_fds=True) |
| 69 | |
| 70 | |
| 71 | @pytest.fixture |
| 72 | def http_server_port(): |
| 73 | """ |
| 74 | Port used by `http_server`. |
| 75 | """ |
| 76 | return tornado.testing.bind_unused_port() |
| 77 | |
| 78 | |
| 79 | @pytest.yield_fixture |
| 80 | def http_server(request, io_loop, http_server_port): |
| 81 | """Start a tornado HTTP server that listens on all available interfaces. |
| 82 | |
| 83 | You must create an `app` fixture, which returns |
| 84 | the `tornado.web.Application` to be tested. |
| 85 | |
| 86 | Raises: |
| 87 | FixtureLookupError: tornado application fixture not found |
| 88 | """ |
| 89 | http_app = request.getfixturevalue(request.config.option.app_fixture) |
| 90 | server = tornado.httpserver.HTTPServer(http_app) |
| 91 | server.add_socket(http_server_port[0]) |
| 92 | |
| 93 | yield server |
| 94 | |
| 95 | server.stop() |
| 96 | |
| 97 | if hasattr(server, "close_all_connections"): |
| 98 | io_loop.run_sync( |
| 99 | server.close_all_connections, |
| 100 | timeout=request.config.option.async_test_timeout, |
| 101 | ) |
| 102 | |
| 103 | |
| 104 | class AsyncHTTPServerClient(tornado.simple_httpclient.SimpleAsyncHTTPClient): |
| 105 | def initialize(self, *, http_server=None): |
| 106 | super().initialize() |
| 107 | self._http_server = http_server |
| 108 | |
| 109 | def fetch(self, path, **kwargs): |
| 110 | """ |
| 111 | Fetch `path` from test server, passing `kwargs` to the `fetch` |
| 112 | of the underlying `tornado.simple_httpclient.SimpleAsyncHTTPClient`. |
| 113 | """ |
| 114 | return super().fetch(self.get_url(path), **kwargs) |
| 115 | |
| 116 | def get_protocol(self): |
| 117 | return "http" |
| 118 | |
| 119 | def get_http_port(self): |
| 120 | for sock in self._http_server._sockets.values(): |
| 121 | return sock.getsockname()[1] |
| 122 | |
| 123 | def get_url(self, path): |
| 124 | return "%s://127.0.0.1:%s%s" % (self.get_protocol(), self.get_http_port(), path) |
| 125 | |
| 126 | |
| 127 | @pytest.fixture |
| 128 | def http_server_client(http_server): |
| 129 | """ |
| 130 | Create an asynchronous HTTP client that can fetch from `http_server`. |
| 131 | """ |
| 132 | with closing(AsyncHTTPServerClient(http_server=http_server)) as client: |
| 133 | yield client |
| 134 | |
| 135 | |
| 136 | @pytest.fixture |
| 137 | def http_client(http_server): |
| 138 | """ |
| 139 | Create an asynchronous HTTP client that can fetch from anywhere. |
| 140 | """ |
| 141 | with closing(tornado.httpclient.AsyncHTTPClient()) as client: |
| 142 | yield client |