pytest_plugin.py 9.2 KB

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