test_sniffio.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import os
  2. import sys
  3. import pytest
  4. from .. import (
  5. current_async_library, AsyncLibraryNotFoundError,
  6. current_async_library_cvar, thread_local
  7. )
  8. def test_basics_cvar():
  9. with pytest.raises(AsyncLibraryNotFoundError):
  10. current_async_library()
  11. token = current_async_library_cvar.set("generic-lib")
  12. try:
  13. assert current_async_library() == "generic-lib"
  14. finally:
  15. current_async_library_cvar.reset(token)
  16. with pytest.raises(AsyncLibraryNotFoundError):
  17. current_async_library()
  18. def test_basics_tlocal():
  19. with pytest.raises(AsyncLibraryNotFoundError):
  20. current_async_library()
  21. old_name, thread_local.name = thread_local.name, "generic-lib"
  22. try:
  23. assert current_async_library() == "generic-lib"
  24. finally:
  25. thread_local.name = old_name
  26. with pytest.raises(AsyncLibraryNotFoundError):
  27. current_async_library()
  28. def test_asyncio():
  29. import asyncio
  30. with pytest.raises(AsyncLibraryNotFoundError):
  31. current_async_library()
  32. ran = []
  33. async def this_is_asyncio():
  34. assert current_async_library() == "asyncio"
  35. # Call it a second time to exercise the caching logic
  36. assert current_async_library() == "asyncio"
  37. ran.append(True)
  38. asyncio.run(this_is_asyncio())
  39. assert ran == [True]
  40. with pytest.raises(AsyncLibraryNotFoundError):
  41. current_async_library()
  42. @pytest.mark.skipif(
  43. sys.version_info >= (3, 12),
  44. reason=
  45. "curio broken on 3.12 (https://github.com/python-trio/sniffio/pull/42)",
  46. )
  47. def test_curio():
  48. import curio
  49. with pytest.raises(AsyncLibraryNotFoundError):
  50. current_async_library()
  51. ran = []
  52. async def this_is_curio():
  53. assert current_async_library() == "curio"
  54. # Call it a second time to exercise the caching logic
  55. assert current_async_library() == "curio"
  56. ran.append(True)
  57. curio.run(this_is_curio)
  58. assert ran == [True]
  59. with pytest.raises(AsyncLibraryNotFoundError):
  60. current_async_library()