pytest_plugin.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. from __future__ import annotations
  2. import socket
  3. import sys
  4. from collections.abc import Callable, Generator, Iterator
  5. from contextlib import ExitStack, contextmanager
  6. from inspect import isasyncgenfunction, iscoroutinefunction, ismethod
  7. from typing import Any, cast
  8. import pytest
  9. from _pytest.fixtures import SubRequest
  10. from _pytest.outcomes import Exit
  11. from . import get_available_backends
  12. from ._core._eventloop import (
  13. current_async_library,
  14. get_async_backend,
  15. reset_current_async_library,
  16. set_current_async_library,
  17. )
  18. from ._core._exceptions import iterate_exceptions
  19. from .abc import TestRunner
  20. if sys.version_info < (3, 11):
  21. from exceptiongroup import ExceptionGroup
  22. _current_runner: TestRunner | None = None
  23. _runner_stack: ExitStack | None = None
  24. _runner_leases = 0
  25. def extract_backend_and_options(backend: object) -> tuple[str, dict[str, Any]]:
  26. if isinstance(backend, str):
  27. return backend, {}
  28. elif isinstance(backend, tuple) and len(backend) == 2:
  29. if isinstance(backend[0], str) and isinstance(backend[1], dict):
  30. return cast(tuple[str, dict[str, Any]], backend)
  31. raise TypeError("anyio_backend must be either a string or tuple of (string, dict)")
  32. @contextmanager
  33. def get_runner(
  34. backend_name: str, backend_options: dict[str, Any]
  35. ) -> Iterator[TestRunner]:
  36. global _current_runner, _runner_leases, _runner_stack
  37. if _current_runner is None:
  38. asynclib = get_async_backend(backend_name)
  39. _runner_stack = ExitStack()
  40. if current_async_library() is None:
  41. # Since we're in control of the event loop, we can cache the name of the
  42. # async library
  43. token = set_current_async_library(backend_name)
  44. _runner_stack.callback(reset_current_async_library, token)
  45. backend_options = backend_options or {}
  46. _current_runner = _runner_stack.enter_context(
  47. asynclib.create_test_runner(backend_options)
  48. )
  49. _runner_leases += 1
  50. try:
  51. yield _current_runner
  52. finally:
  53. _runner_leases -= 1
  54. if not _runner_leases:
  55. assert _runner_stack is not None
  56. _runner_stack.close()
  57. _runner_stack = _current_runner = None
  58. def pytest_addoption(parser: pytest.Parser) -> None:
  59. parser.addini(
  60. "anyio_mode",
  61. default="strict",
  62. help='AnyIO plugin mode (either "strict" or "auto")',
  63. )
  64. def pytest_configure(config: pytest.Config) -> None:
  65. config.addinivalue_line(
  66. "markers",
  67. "anyio: mark the (coroutine function) test to be run asynchronously via anyio.",
  68. )
  69. if (
  70. config.getini("anyio_mode") == "auto"
  71. and config.pluginmanager.has_plugin("asyncio")
  72. and config.getini("asyncio_mode") == "auto"
  73. ):
  74. config.issue_config_time_warning(
  75. pytest.PytestConfigWarning(
  76. "AnyIO auto mode has been enabled together with pytest-asyncio auto "
  77. "mode. This may cause unexpected behavior."
  78. ),
  79. 1,
  80. )
  81. @pytest.hookimpl(hookwrapper=True)
  82. def pytest_fixture_setup(fixturedef: Any, request: Any) -> Generator[Any]:
  83. def wrapper(anyio_backend: Any, request: SubRequest, **kwargs: Any) -> Any:
  84. # Rebind any fixture methods to the request instance
  85. if (
  86. request.instance
  87. and ismethod(func)
  88. and type(func.__self__) is type(request.instance)
  89. ):
  90. local_func = func.__func__.__get__(request.instance)
  91. else:
  92. local_func = func
  93. backend_name, backend_options = extract_backend_and_options(anyio_backend)
  94. if has_backend_arg:
  95. kwargs["anyio_backend"] = anyio_backend
  96. if has_request_arg:
  97. kwargs["request"] = request
  98. with get_runner(backend_name, backend_options) as runner:
  99. if isasyncgenfunction(local_func):
  100. yield from runner.run_asyncgen_fixture(local_func, kwargs)
  101. else:
  102. yield runner.run_fixture(local_func, kwargs)
  103. # Only apply this to coroutine functions and async generator functions in requests
  104. # that involve the anyio_backend fixture
  105. func = fixturedef.func
  106. if isasyncgenfunction(func) or iscoroutinefunction(func):
  107. if "anyio_backend" in request.fixturenames:
  108. fixturedef.func = wrapper
  109. original_argname = fixturedef.argnames
  110. if not (has_backend_arg := "anyio_backend" in fixturedef.argnames):
  111. fixturedef.argnames += ("anyio_backend",)
  112. if not (has_request_arg := "request" in fixturedef.argnames):
  113. fixturedef.argnames += ("request",)
  114. try:
  115. return (yield)
  116. finally:
  117. fixturedef.func = func
  118. fixturedef.argnames = original_argname
  119. return (yield)
  120. @pytest.hookimpl(tryfirst=True)
  121. def pytest_pycollect_makeitem(
  122. collector: pytest.Module | pytest.Class, name: str, obj: object
  123. ) -> None:
  124. if collector.istestfunction(obj, name):
  125. inner_func = obj.hypothesis.inner_test if hasattr(obj, "hypothesis") else obj
  126. if iscoroutinefunction(inner_func):
  127. anyio_auto_mode = collector.config.getini("anyio_mode") == "auto"
  128. marker = collector.get_closest_marker("anyio")
  129. own_markers = getattr(obj, "pytestmark", ())
  130. if (
  131. anyio_auto_mode
  132. or marker
  133. or any(marker.name == "anyio" for marker in own_markers)
  134. ):
  135. pytest.mark.usefixtures("anyio_backend")(obj)
  136. @pytest.hookimpl(tryfirst=True)
  137. def pytest_pyfunc_call(pyfuncitem: Any) -> bool | None:
  138. def run_with_hypothesis(**kwargs: Any) -> None:
  139. with get_runner(backend_name, backend_options) as runner:
  140. runner.run_test(original_func, kwargs)
  141. backend = pyfuncitem.funcargs.get("anyio_backend")
  142. if backend:
  143. backend_name, backend_options = extract_backend_and_options(backend)
  144. if hasattr(pyfuncitem.obj, "hypothesis"):
  145. # Wrap the inner test function unless it's already wrapped
  146. original_func = pyfuncitem.obj.hypothesis.inner_test
  147. if original_func.__qualname__ != run_with_hypothesis.__qualname__:
  148. if iscoroutinefunction(original_func):
  149. pyfuncitem.obj.hypothesis.inner_test = run_with_hypothesis
  150. return None
  151. if iscoroutinefunction(pyfuncitem.obj):
  152. funcargs = pyfuncitem.funcargs
  153. testargs = {arg: funcargs[arg] for arg in pyfuncitem._fixtureinfo.argnames}
  154. with get_runner(backend_name, backend_options) as runner:
  155. try:
  156. runner.run_test(pyfuncitem.obj, testargs)
  157. except ExceptionGroup as excgrp:
  158. for exc in iterate_exceptions(excgrp):
  159. if isinstance(exc, (Exit, KeyboardInterrupt, SystemExit)):
  160. raise exc from excgrp
  161. raise
  162. return True
  163. return None
  164. @pytest.fixture(scope="module", params=get_available_backends())
  165. def anyio_backend(request: Any) -> Any:
  166. return request.param
  167. @pytest.fixture
  168. def anyio_backend_name(anyio_backend: Any) -> str:
  169. if isinstance(anyio_backend, str):
  170. return anyio_backend
  171. else:
  172. return anyio_backend[0]
  173. @pytest.fixture
  174. def anyio_backend_options(anyio_backend: Any) -> dict[str, Any]:
  175. if isinstance(anyio_backend, str):
  176. return {}
  177. else:
  178. return anyio_backend[1]
  179. class FreePortFactory:
  180. """
  181. Manages port generation based on specified socket kind, ensuring no duplicate
  182. ports are generated.
  183. This class provides functionality for generating available free ports on the
  184. system. It is initialized with a specific socket kind and can generate ports
  185. for given address families while avoiding reuse of previously generated ports.
  186. Users should not instantiate this class directly, but use the
  187. ``free_tcp_port_factory`` and ``free_udp_port_factory`` fixtures instead. For simple
  188. uses cases, ``free_tcp_port`` and ``free_udp_port`` can be used instead.
  189. """
  190. def __init__(self, kind: socket.SocketKind) -> None:
  191. self._kind = kind
  192. self._generated = set[int]()
  193. @property
  194. def kind(self) -> socket.SocketKind:
  195. """
  196. The type of socket connection (e.g., :data:`~socket.SOCK_STREAM` or
  197. :data:`~socket.SOCK_DGRAM`) used to bind for checking port availability
  198. """
  199. return self._kind
  200. def __call__(self, family: socket.AddressFamily | None = None) -> int:
  201. """
  202. Return an unbound port for the given address family.
  203. :param family: if omitted, both IPv4 and IPv6 addresses will be tried
  204. :return: a port number
  205. """
  206. if family is not None:
  207. families = [family]
  208. else:
  209. families = [socket.AF_INET]
  210. if socket.has_ipv6:
  211. families.append(socket.AF_INET6)
  212. while True:
  213. port = 0
  214. with ExitStack() as stack:
  215. for family in families:
  216. sock = stack.enter_context(socket.socket(family, self._kind))
  217. addr = "::1" if family == socket.AF_INET6 else "127.0.0.1"
  218. try:
  219. sock.bind((addr, port))
  220. except OSError:
  221. break
  222. if not port:
  223. port = sock.getsockname()[1]
  224. else:
  225. if port not in self._generated:
  226. self._generated.add(port)
  227. return port
  228. @pytest.fixture(scope="session")
  229. def free_tcp_port_factory() -> FreePortFactory:
  230. return FreePortFactory(socket.SOCK_STREAM)
  231. @pytest.fixture(scope="session")
  232. def free_udp_port_factory() -> FreePortFactory:
  233. return FreePortFactory(socket.SOCK_DGRAM)
  234. @pytest.fixture
  235. def free_tcp_port(free_tcp_port_factory: Callable[[], int]) -> int:
  236. return free_tcp_port_factory()
  237. @pytest.fixture
  238. def free_udp_port(free_udp_port_factory: Callable[[], int]) -> int:
  239. return free_udp_port_factory()