zmc
2023-12-22 9fdbf60165db0400c2e8e6be2dc6e88138ac719a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import os
import sys
 
import pytest
 
from .. import (
    current_async_library, AsyncLibraryNotFoundError,
    current_async_library_cvar, thread_local
)
 
 
def test_basics_cvar():
    with pytest.raises(AsyncLibraryNotFoundError):
        current_async_library()
 
    token = current_async_library_cvar.set("generic-lib")
    try:
        assert current_async_library() == "generic-lib"
    finally:
        current_async_library_cvar.reset(token)
 
    with pytest.raises(AsyncLibraryNotFoundError):
        current_async_library()
 
 
def test_basics_tlocal():
    with pytest.raises(AsyncLibraryNotFoundError):
        current_async_library()
 
    old_name, thread_local.name = thread_local.name, "generic-lib"
    try:
        assert current_async_library() == "generic-lib"
    finally:
        thread_local.name = old_name
 
    with pytest.raises(AsyncLibraryNotFoundError):
        current_async_library()
 
 
def test_asyncio():
    import asyncio
 
    with pytest.raises(AsyncLibraryNotFoundError):
        current_async_library()
 
    ran = []
 
    async def this_is_asyncio():
        assert current_async_library() == "asyncio"
        # Call it a second time to exercise the caching logic
        assert current_async_library() == "asyncio"
        ran.append(True)
 
    asyncio.run(this_is_asyncio())
    assert ran == [True]
 
    with pytest.raises(AsyncLibraryNotFoundError):
        current_async_library()
 
 
# https://github.com/dabeaz/curio/pull/354
@pytest.mark.skipif(
    os.name == "nt" and sys.version_info >= (3, 9),
    reason="Curio breaks on Python 3.9+ on Windows. Fix was not released yet",
)
def test_curio():
    import curio
 
    with pytest.raises(AsyncLibraryNotFoundError):
        current_async_library()
 
    ran = []
 
    async def this_is_curio():
        assert current_async_library() == "curio"
        # Call it a second time to exercise the caching logic
        assert current_async_library() == "curio"
        ran.append(True)
 
    curio.run(this_is_curio)
    assert ran == [True]
 
    with pytest.raises(AsyncLibraryNotFoundError):
        current_async_library()