1
1
import anyio
2
2
import pytest
3
3
4
- from returns .primitives .reawaitable import ReAwaitable
4
+ from returns .primitives .reawaitable import ReAwaitable , reawaitable
5
5
6
6
7
7
async def sample_coro ():
@@ -11,11 +11,54 @@ async def sample_coro():
11
11
12
12
@pytest .mark .anyio
13
13
async def test_concurrent_awaitable ():
14
- reawaitable = ReAwaitable (sample_coro ())
14
+ test_target = ReAwaitable (sample_coro ())
15
15
16
16
async def await_reawaitable ():
17
- return await reawaitable
18
-
17
+ return await test_target
19
18
async with anyio .create_task_group () as tg :
20
19
task1 = tg .start_soon (await_reawaitable )
21
20
task2 = tg .start_soon (await_reawaitable )
21
+
22
+
23
+ @pytest .mark .anyio
24
+ async def test_reawaitable_decorator ():
25
+ """Test the reawaitable decorator with concurrent awaits."""
26
+
27
+ @reawaitable
28
+ async def decorated_coro ():
29
+ await anyio .sleep (0.1 )
30
+ return "decorated"
31
+
32
+ instance = decorated_coro ()
33
+
34
+ # Test multiple awaits
35
+ result1 = await instance
36
+ result2 = await instance
37
+
38
+ assert result1 == "decorated"
39
+ assert result1 == result2
40
+
41
+ # Test concurrent awaits
42
+ async def await_decorated ():
43
+ return await instance
44
+
45
+ async with anyio .create_task_group () as tg :
46
+ task1 = tg .start_soon (await_decorated )
47
+ task2 = tg .start_soon (await_decorated )
48
+
49
+
50
+ @pytest .mark .anyio
51
+ async def test_reawaitable_repr ():
52
+ """Test the __repr__ method of ReAwaitable."""
53
+
54
+ async def test_func ():
55
+ return 1
56
+
57
+ coro = test_func ()
58
+ reawaitable = ReAwaitable (coro )
59
+
60
+ # Test the representation
61
+ assert repr (reawaitable ) == repr (coro )
62
+
63
+ # Ensure the coroutine is properly awaited
64
+ await reawaitable
0 commit comments