asyncio.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. # testing/asyncio.py
  2. # Copyright (C) 2005-2024 the SQLAlchemy authors and contributors
  3. # <see AUTHORS file>
  4. #
  5. # This module is part of SQLAlchemy and is released under
  6. # the MIT License: https://www.opensource.org/licenses/mit-license.php
  7. # mypy: ignore-errors
  8. # functions and wrappers to run tests, fixtures, provisioning and
  9. # setup/teardown in an asyncio event loop, conditionally based on the
  10. # current DB driver being used for a test.
  11. # note that SQLAlchemy's asyncio integration also supports a method
  12. # of running individual asyncio functions inside of separate event loops
  13. # using "async_fallback" mode; however running whole functions in the event
  14. # loop is a more accurate test for how SQLAlchemy's asyncio features
  15. # would run in the real world.
  16. from __future__ import annotations
  17. from functools import wraps
  18. import inspect
  19. from . import config
  20. from ..util.concurrency import _util_async_run
  21. from ..util.concurrency import _util_async_run_coroutine_function
  22. # may be set to False if the
  23. # --disable-asyncio flag is passed to the test runner.
  24. ENABLE_ASYNCIO = True
  25. def _run_coroutine_function(fn, *args, **kwargs):
  26. return _util_async_run_coroutine_function(fn, *args, **kwargs)
  27. def _assume_async(fn, *args, **kwargs):
  28. """Run a function in an asyncio loop unconditionally.
  29. This function is used for provisioning features like
  30. testing a database connection for server info.
  31. Note that for blocking IO database drivers, this means they block the
  32. event loop.
  33. """
  34. if not ENABLE_ASYNCIO:
  35. return fn(*args, **kwargs)
  36. return _util_async_run(fn, *args, **kwargs)
  37. def _maybe_async_provisioning(fn, *args, **kwargs):
  38. """Run a function in an asyncio loop if any current drivers might need it.
  39. This function is used for provisioning features that take
  40. place outside of a specific database driver being selected, so if the
  41. current driver that happens to be used for the provisioning operation
  42. is an async driver, it will run in asyncio and not fail.
  43. Note that for blocking IO database drivers, this means they block the
  44. event loop.
  45. """
  46. if not ENABLE_ASYNCIO:
  47. return fn(*args, **kwargs)
  48. if config.any_async:
  49. return _util_async_run(fn, *args, **kwargs)
  50. else:
  51. return fn(*args, **kwargs)
  52. def _maybe_async(fn, *args, **kwargs):
  53. """Run a function in an asyncio loop if the current selected driver is
  54. async.
  55. This function is used for test setup/teardown and tests themselves
  56. where the current DB driver is known.
  57. """
  58. if not ENABLE_ASYNCIO:
  59. return fn(*args, **kwargs)
  60. is_async = config._current.is_async
  61. if is_async:
  62. return _util_async_run(fn, *args, **kwargs)
  63. else:
  64. return fn(*args, **kwargs)
  65. def _maybe_async_wrapper(fn):
  66. """Apply the _maybe_async function to an existing function and return
  67. as a wrapped callable, supporting generator functions as well.
  68. This is currently used for pytest fixtures that support generator use.
  69. """
  70. if inspect.isgeneratorfunction(fn):
  71. _stop = object()
  72. def call_next(gen):
  73. try:
  74. return next(gen)
  75. # can't raise StopIteration in an awaitable.
  76. except StopIteration:
  77. return _stop
  78. @wraps(fn)
  79. def wrap_fixture(*args, **kwargs):
  80. gen = fn(*args, **kwargs)
  81. while True:
  82. value = _maybe_async(call_next, gen)
  83. if value is _stop:
  84. break
  85. yield value
  86. else:
  87. @wraps(fn)
  88. def wrap_fixture(*args, **kwargs):
  89. return _maybe_async(fn, *args, **kwargs)
  90. return wrap_fixture