pytest_plugin.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. from __future__ import annotations
  2. import dataclasses
  3. import socket
  4. import sys
  5. from collections.abc import Callable, Generator, Iterator
  6. from contextlib import ExitStack, contextmanager
  7. from inspect import isasyncgenfunction, iscoroutinefunction, ismethod
  8. from typing import TYPE_CHECKING, Any, cast
  9. import pytest
  10. from _pytest.fixtures import FuncFixtureInfo, SubRequest
  11. from _pytest.outcomes import Exit
  12. from _pytest.scope import Scope
  13. from . import get_available_backends
  14. from ._core._eventloop import (
  15. current_async_library,
  16. get_async_backend,
  17. reset_current_async_library,
  18. set_current_async_library,
  19. )
  20. from ._core._exceptions import iterate_exceptions
  21. if TYPE_CHECKING:
  22. from .abc import TestRunner
  23. if sys.version_info < (3, 11):
  24. from exceptiongroup import ExceptionGroup
  25. if TYPE_CHECKING:
  26. # pytest >= 9.2 keeps CallSpec2 as a TYPE_CHECKING-only alias of CallSpec
  27. from _pytest.python import CallSpec2 as CallSpec
  28. else:
  29. try:
  30. # c.f. https://github.com/pytest-dev/pytest/pull/14742
  31. # pytest >= 9.2
  32. from _pytest.python import CallSpec
  33. except ImportError:
  34. from _pytest.python import CallSpec2 as CallSpec
  35. _current_runner: TestRunner | None = None
  36. _runner_stack: ExitStack | None = None
  37. _runner_leases = 0
  38. def extract_backend_and_options(backend: object) -> tuple[str, dict[str, Any]]:
  39. if isinstance(backend, str):
  40. return backend, {}
  41. elif isinstance(backend, tuple) and len(backend) == 2:
  42. if isinstance(backend[0], str) and isinstance(backend[1], dict):
  43. return cast(tuple[str, dict[str, Any]], backend)
  44. raise TypeError("anyio_backend must be either a string or tuple of (string, dict)")
  45. @contextmanager
  46. def get_runner(
  47. backend_name: str, backend_options: dict[str, Any]
  48. ) -> Iterator[TestRunner]:
  49. global _current_runner, _runner_leases, _runner_stack
  50. if _current_runner is None:
  51. asynclib = get_async_backend(backend_name)
  52. _runner_stack = ExitStack()
  53. if current_async_library() is None:
  54. # Since we're in control of the event loop, we can cache the name of the
  55. # async library
  56. token = set_current_async_library(backend_name)
  57. _runner_stack.callback(reset_current_async_library, token)
  58. backend_options = backend_options or {}
  59. _current_runner = _runner_stack.enter_context(
  60. asynclib.create_test_runner(backend_options)
  61. )
  62. _runner_leases += 1
  63. try:
  64. yield _current_runner
  65. finally:
  66. _runner_leases -= 1
  67. if not _runner_leases:
  68. assert _runner_stack is not None
  69. _runner_stack.close()
  70. _runner_stack = _current_runner = None
  71. def pytest_addoption(parser: pytest.Parser) -> None:
  72. group = parser.getgroup("anyio")
  73. parser.addini(
  74. "anyio_mode",
  75. default="strict",
  76. help='AnyIO plugin mode (either "strict" or "auto")',
  77. )
  78. group.addoption(
  79. "--anyio-mode",
  80. default=None,
  81. dest="anyio_mode",
  82. help="""
  83. 'auto' - All async test functions will be handled by AnyIO pytest plugin
  84. 'strict' - Disabling autoprocessing(useful when anyio tests
  85. need to coexist with other async test plugins)
  86. """,
  87. )
  88. def _is_auto_mode(config: pytest.Config, mode: str) -> bool:
  89. return (
  90. config.getoption(mode, default=None) == "auto" or config.getini(mode) == "auto"
  91. )
  92. def pytest_configure(config: pytest.Config) -> None:
  93. config.addinivalue_line(
  94. "markers",
  95. "anyio: mark the (coroutine function) test to be run asynchronously via anyio.",
  96. )
  97. if (
  98. _is_auto_mode(config, "anyio_mode")
  99. and config.pluginmanager.has_plugin("asyncio")
  100. and _is_auto_mode(config, "asyncio_mode")
  101. ):
  102. config.issue_config_time_warning(
  103. pytest.PytestConfigWarning(
  104. "AnyIO auto mode has been enabled together with pytest-asyncio auto "
  105. "mode. This may cause unexpected behavior."
  106. ),
  107. 1,
  108. )
  109. @pytest.hookimpl(hookwrapper=True)
  110. def pytest_fixture_setup(fixturedef: Any, request: Any) -> Generator[Any]:
  111. def wrapper(anyio_backend: Any, request: SubRequest, **kwargs: Any) -> Any:
  112. # Rebind any fixture methods to the request instance
  113. if (
  114. request.instance
  115. and ismethod(func)
  116. and type(func.__self__) is type(request.instance)
  117. ):
  118. local_func = func.__func__.__get__(request.instance)
  119. else:
  120. local_func = func
  121. backend_name, backend_options = extract_backend_and_options(anyio_backend)
  122. if has_backend_arg:
  123. kwargs["anyio_backend"] = anyio_backend
  124. if has_request_arg:
  125. kwargs["request"] = request
  126. with get_runner(backend_name, backend_options) as runner:
  127. # re-entrant call into the test runner detected. this happens when an async fixture
  128. # is dynamically requested via request.getfixturevalue() from inside a running async
  129. # test or fixture. on asyncio this raises RuntimeError: This event loop is already
  130. # running, on trio the runner deadlocks - the host loop blocks waiting for the
  131. # coroutine to return, but the coroutine is waiting for the host loop. raising here
  132. # prevents the hang and gives a consistent error across backends.
  133. if runner.is_running():
  134. raise RuntimeError(
  135. "Cannot schedule a coroutine in the test runner while another is already running; "
  136. "likely caused by request.getfixturevalue() on an async fixture."
  137. )
  138. if isasyncgenfunction(local_func):
  139. yield from runner.run_asyncgen_fixture(local_func, kwargs)
  140. else:
  141. yield runner.run_fixture(local_func, kwargs)
  142. # Only apply this to coroutine functions and async generator functions in requests
  143. # that involve the anyio_backend fixture
  144. func = fixturedef.func
  145. if isasyncgenfunction(func) or iscoroutinefunction(func):
  146. if "anyio_backend" in request.fixturenames:
  147. fixturedef.func = wrapper
  148. original_argname = fixturedef.argnames
  149. if not (has_backend_arg := "anyio_backend" in fixturedef.argnames):
  150. fixturedef.argnames += ("anyio_backend",)
  151. if not (has_request_arg := "request" in fixturedef.argnames):
  152. fixturedef.argnames += ("request",)
  153. try:
  154. return (yield)
  155. finally:
  156. fixturedef.func = func
  157. fixturedef.argnames = original_argname
  158. return (yield)
  159. @pytest.hookimpl(tryfirst=True)
  160. def pytest_pycollect_makeitem(
  161. collector: pytest.Module | pytest.Class, name: str, obj: object
  162. ) -> None:
  163. if collector.istestfunction(obj, name):
  164. inner_func = obj.hypothesis.inner_test if hasattr(obj, "hypothesis") else obj
  165. if iscoroutinefunction(inner_func):
  166. anyio_auto_mode = _is_auto_mode(collector.config, "anyio_mode")
  167. marker = collector.get_closest_marker("anyio")
  168. own_markers = getattr(obj, "pytestmark", ())
  169. if (
  170. anyio_auto_mode
  171. or marker
  172. or any(marker.name == "anyio" for marker in own_markers)
  173. ):
  174. pytest.mark.usefixtures("anyio_backend")(obj)
  175. def pytest_collection_finish(session: pytest.Session) -> None:
  176. for i, item in reversed(list(enumerate(session.items))):
  177. if (
  178. isinstance(item, pytest.Function)
  179. and iscoroutinefunction(item.function)
  180. and item.get_closest_marker("anyio") is not None
  181. and "anyio_backend" not in item.fixturenames
  182. ):
  183. new_items = []
  184. try:
  185. cs_fields = {f.name for f in dataclasses.fields(CallSpec)}
  186. except TypeError:
  187. cs_fields = set()
  188. for param_index, backend in enumerate(get_available_backends()):
  189. if "_arg2scope" in cs_fields: # pytest >= 8
  190. callspec = CallSpec(
  191. params={"anyio_backend": backend},
  192. indices={"anyio_backend": param_index},
  193. _arg2scope={"anyio_backend": Scope.Module},
  194. _idlist=[backend],
  195. marks=[],
  196. )
  197. else: # pytest 7.x
  198. callspec = CallSpec( # type: ignore[call-arg]
  199. funcargs={},
  200. params={"anyio_backend": backend},
  201. indices={"anyio_backend": param_index},
  202. arg2scope={"anyio_backend": Scope.Module},
  203. idlist=[backend],
  204. marks=[],
  205. )
  206. fi = item._fixtureinfo
  207. new_names_closure = list(fi.names_closure)
  208. if "anyio_backend" not in new_names_closure:
  209. new_names_closure.append("anyio_backend")
  210. new_fixtureinfo = FuncFixtureInfo(
  211. argnames=fi.argnames,
  212. initialnames=fi.initialnames,
  213. names_closure=new_names_closure,
  214. name2fixturedefs=fi.name2fixturedefs,
  215. )
  216. new_item = pytest.Function.from_parent(
  217. item.parent,
  218. name=f"{item.originalname}[{backend}]",
  219. callspec=callspec,
  220. callobj=item.obj,
  221. fixtureinfo=new_fixtureinfo,
  222. keywords=item.keywords,
  223. originalname=item.originalname,
  224. )
  225. new_items.append(new_item)
  226. session.items[i : i + 1] = new_items
  227. @pytest.hookimpl(tryfirst=True)
  228. def pytest_pyfunc_call(pyfuncitem: Any) -> bool | None:
  229. def run_with_hypothesis(**kwargs: Any) -> None:
  230. with get_runner(backend_name, backend_options) as runner:
  231. runner.run_test(original_func, kwargs)
  232. backend = pyfuncitem.funcargs.get("anyio_backend")
  233. if backend:
  234. backend_name, backend_options = extract_backend_and_options(backend)
  235. if hasattr(pyfuncitem.obj, "hypothesis"):
  236. # Wrap the inner test function unless it's already wrapped
  237. original_func = pyfuncitem.obj.hypothesis.inner_test
  238. if original_func.__qualname__ != run_with_hypothesis.__qualname__:
  239. if iscoroutinefunction(original_func):
  240. pyfuncitem.obj.hypothesis.inner_test = run_with_hypothesis
  241. return None
  242. if iscoroutinefunction(pyfuncitem.obj):
  243. funcargs = pyfuncitem.funcargs
  244. testargs = {arg: funcargs[arg] for arg in pyfuncitem._fixtureinfo.argnames}
  245. with get_runner(backend_name, backend_options) as runner:
  246. try:
  247. runner.run_test(pyfuncitem.obj, testargs)
  248. except ExceptionGroup as excgrp:
  249. for exc in iterate_exceptions(excgrp):
  250. if isinstance(exc, (Exit, KeyboardInterrupt, SystemExit)):
  251. raise exc from excgrp
  252. raise
  253. return True
  254. return None
  255. @pytest.fixture(scope="module", params=get_available_backends())
  256. def anyio_backend(request: Any) -> Any:
  257. return request.param
  258. @pytest.fixture
  259. def anyio_backend_name(anyio_backend: Any) -> str:
  260. if isinstance(anyio_backend, str):
  261. return anyio_backend
  262. else:
  263. return anyio_backend[0]
  264. @pytest.fixture
  265. def anyio_backend_options(anyio_backend: Any) -> dict[str, Any]:
  266. if isinstance(anyio_backend, str):
  267. return {}
  268. else:
  269. return anyio_backend[1]
  270. class FreePortFactory:
  271. """
  272. Manages port generation based on specified socket kind, ensuring no duplicate
  273. ports are generated.
  274. This class provides functionality for generating available free ports on the
  275. system. It is initialized with a specific socket kind and can generate ports
  276. for given address families while avoiding reuse of previously generated ports.
  277. Users should not instantiate this class directly, but use the
  278. ``free_tcp_port_factory`` and ``free_udp_port_factory`` fixtures instead. For simple
  279. uses cases, ``free_tcp_port`` and ``free_udp_port`` can be used instead.
  280. """
  281. def __init__(self, kind: socket.SocketKind) -> None:
  282. self._kind = kind
  283. self._generated = set[int]()
  284. @property
  285. def kind(self) -> socket.SocketKind:
  286. """
  287. The type of socket connection (e.g., :data:`~socket.SOCK_STREAM` or
  288. :data:`~socket.SOCK_DGRAM`) used to bind for checking port availability
  289. """
  290. return self._kind
  291. def __call__(self, family: socket.AddressFamily | None = None) -> int:
  292. """
  293. Return an unbound port for the given address family.
  294. :param family: if omitted, both IPv4 and IPv6 addresses will be tried
  295. :return: a port number
  296. """
  297. if family is not None:
  298. families = [family]
  299. else:
  300. families = [socket.AF_INET]
  301. if socket.has_ipv6:
  302. families.append(socket.AF_INET6)
  303. while True:
  304. port = 0
  305. with ExitStack() as stack:
  306. for fam in families:
  307. sock = stack.enter_context(socket.socket(fam, self._kind))
  308. addr = "::1" if fam == socket.AF_INET6 else "127.0.0.1"
  309. try:
  310. sock.bind((addr, port))
  311. except OSError:
  312. break
  313. if not port:
  314. port = sock.getsockname()[1]
  315. else:
  316. if port not in self._generated:
  317. self._generated.add(port)
  318. return port
  319. @pytest.fixture(scope="session")
  320. def free_tcp_port_factory() -> FreePortFactory:
  321. return FreePortFactory(socket.SOCK_STREAM)
  322. @pytest.fixture(scope="session")
  323. def free_udp_port_factory() -> FreePortFactory:
  324. return FreePortFactory(socket.SOCK_DGRAM)
  325. @pytest.fixture
  326. def free_tcp_port(free_tcp_port_factory: Callable[[], int]) -> int:
  327. return free_tcp_port_factory()
  328. @pytest.fixture
  329. def free_udp_port(free_udp_port_factory: Callable[[], int]) -> int:
  330. return free_udp_port_factory()