|
| 1 | +import asyncio |
| 2 | +from unittest.mock import patch |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from returns.primitives.reawaitable import ReAwaitable, reawaitable |
| 7 | + |
| 8 | + |
| 9 | +class CallCounter: |
| 10 | + """Helper class to count function calls.""" |
| 11 | + |
| 12 | + def __init__(self) -> None: |
| 13 | + """Initialize counter.""" |
| 14 | + self.count = 0 |
| 15 | + |
| 16 | + def increment(self) -> None: |
| 17 | + """Increment the counter.""" |
| 18 | + self.count += 1 |
| 19 | + |
| 20 | + |
| 21 | +@pytest.mark.asyncio |
| 22 | +async def test_concurrent_await(): |
| 23 | + """Test that ReAwaitable can be awaited concurrently from multiple tasks.""" |
| 24 | + counter = CallCounter() |
| 25 | + |
| 26 | + async def example_coro() -> int: |
| 27 | + counter.increment() |
| 28 | + await asyncio.sleep(0.01) # Simulate some async work |
| 29 | + return 42 |
| 30 | + |
| 31 | + awaitable = ReAwaitable(example_coro()) |
| 32 | + |
| 33 | + async def await_helper(): |
| 34 | + return await awaitable |
| 35 | + |
| 36 | + # Create multiple tasks that await the same ReAwaitable instance |
| 37 | + tasks = [ |
| 38 | + asyncio.create_task(await_helper()), |
| 39 | + asyncio.create_task(await_helper()), |
| 40 | + asyncio.create_task(await_helper()), |
| 41 | + ] |
| 42 | + |
| 43 | + # All tasks should complete without error |
| 44 | + gathered_results = await asyncio.gather(*tasks, return_exceptions=True) |
| 45 | + |
| 46 | + # Check that no exceptions were raised |
| 47 | + for result in gathered_results: |
| 48 | + assert not isinstance(result, Exception) |
| 49 | + |
| 50 | + # The underlying coroutine should only be called once |
| 51 | + assert counter.count == 1 |
| 52 | + |
| 53 | + # All results should be the same |
| 54 | + assert all(res == 42 for res in gathered_results) |
| 55 | + |
| 56 | + |
| 57 | +@pytest.mark.asyncio |
| 58 | +async def test_concurrent_await_with_different_values(): |
| 59 | + """Test that multiple ReAwaitable instances work correctly.""" |
| 60 | + |
| 61 | + async def example_with_value(input_value: int) -> int: |
| 62 | + await asyncio.sleep(0.01) |
| 63 | + return input_value |
| 64 | + |
| 65 | + awaitables = [ |
| 66 | + ReAwaitable(example_with_value(0)), |
| 67 | + ReAwaitable(example_with_value(1)), |
| 68 | + ReAwaitable(example_with_value(2)), |
| 69 | + ] |
| 70 | + |
| 71 | + async def await_helper_with_arg(awaitable_arg): |
| 72 | + return await awaitable_arg |
| 73 | + |
| 74 | + # Create tasks for each awaitable |
| 75 | + tasks = [] |
| 76 | + for awaitable in awaitables: |
| 77 | + # Each awaitable is awaited multiple times |
| 78 | + tasks.extend([ |
| 79 | + asyncio.create_task(await_helper_with_arg(awaitable)), |
| 80 | + asyncio.create_task(await_helper_with_arg(awaitable)), |
| 81 | + ]) |
| 82 | + |
| 83 | + gathered_results = await asyncio.gather(*tasks, return_exceptions=True) |
| 84 | + |
| 85 | + # Check that no exceptions were raised |
| 86 | + for result in gathered_results: |
| 87 | + assert not isinstance(result, Exception) |
| 88 | + |
| 89 | + # Check that each awaitable returned its correct value multiple times |
| 90 | + assert gathered_results[0] == gathered_results[1] == 0 |
| 91 | + assert gathered_results[2] == gathered_results[3] == 1 |
| 92 | + assert gathered_results[4] == gathered_results[5] == 2 |
| 93 | + |
| 94 | + |
| 95 | +@pytest.mark.asyncio |
| 96 | +async def test_sequential_await(): |
| 97 | + """Test that ReAwaitable still works correctly with sequential awaits.""" |
| 98 | + counter = CallCounter() |
| 99 | + |
| 100 | + async def example_sequential() -> int: |
| 101 | + counter.increment() |
| 102 | + return 42 |
| 103 | + |
| 104 | + awaitable = ReAwaitable(example_sequential()) |
| 105 | + |
| 106 | + # Sequential awaits should work as before |
| 107 | + result1 = await awaitable |
| 108 | + result2 = await awaitable |
| 109 | + result3 = await awaitable |
| 110 | + |
| 111 | + assert result1 == result2 == result3 == 42 |
| 112 | + assert counter.count == 1 # Should only be called once |
| 113 | + |
| 114 | + |
| 115 | +@pytest.mark.asyncio |
| 116 | +async def test_no_event_loop_fallback(): |
| 117 | + """Test that ReAwaitable works when no event loop is available.""" |
| 118 | + counter = CallCounter() |
| 119 | + |
| 120 | + async def example_coro() -> int: |
| 121 | + counter.increment() |
| 122 | + return 42 |
| 123 | + |
| 124 | + awaitable = ReAwaitable(example_coro()) |
| 125 | + |
| 126 | + # Mock asyncio.Lock to raise RuntimeError (simulating no event loop) |
| 127 | + with patch('asyncio.Lock', side_effect=RuntimeError('No event loop')): |
| 128 | + # First await should execute the coroutine and cache the result |
| 129 | + result1 = await awaitable |
| 130 | + assert result1 == 42 |
| 131 | + assert counter.count == 1 |
| 132 | + |
| 133 | + # Second await should return cached result without executing again |
| 134 | + result2 = await awaitable |
| 135 | + assert result2 == 42 |
| 136 | + assert counter.count == 1 # Should still be 1, not incremented |
| 137 | + |
| 138 | + |
| 139 | +@pytest.mark.asyncio |
| 140 | +async def test_lock_path_branch_coverage(): |
| 141 | + """Test to ensure branch coverage in the lock acquisition path.""" |
| 142 | + counter = CallCounter() |
| 143 | + |
| 144 | + async def example_coro() -> int: |
| 145 | + counter.increment() |
| 146 | + return 42 |
| 147 | + |
| 148 | + awaitable = ReAwaitable(example_coro()) |
| 149 | + |
| 150 | + # First ensure normal path works (should create lock and execute) |
| 151 | + result1 = await awaitable |
| 152 | + assert result1 == 42 |
| 153 | + assert counter.count == 1 |
| 154 | + |
| 155 | + # Second call should go through the locked path and find cache |
| 156 | + result2 = await awaitable |
| 157 | + assert result2 == 42 |
| 158 | + assert counter.count == 1 |
| 159 | + |
| 160 | + |
| 161 | +@pytest.mark.asyncio |
| 162 | +async def test_reawaitable_decorator(): |
| 163 | + """Test the reawaitable decorator function.""" |
| 164 | + counter = CallCounter() |
| 165 | + |
| 166 | + @reawaitable |
| 167 | + async def decorated_coro() -> int: |
| 168 | + counter.increment() |
| 169 | + return 42 |
| 170 | + |
| 171 | + # Test that the decorator works |
| 172 | + result = decorated_coro() |
| 173 | + assert isinstance(result, ReAwaitable) |
| 174 | + |
| 175 | + # Test multiple awaits |
| 176 | + value1 = await result |
| 177 | + value2 = await result |
| 178 | + assert value1 == value2 == 42 |
| 179 | + assert counter.count == 1 |
| 180 | + |
| 181 | + |
| 182 | +def test_reawaitable_repr(): |
| 183 | + """Test that ReAwaitable repr matches the coroutine repr.""" |
| 184 | + async def test_coro() -> int: |
| 185 | + return 1 |
| 186 | + |
| 187 | + coro = test_coro() |
| 188 | + awaitable = ReAwaitable(coro) |
| 189 | + |
| 190 | + # The repr should match (though the exact format may vary) |
| 191 | + # We just check that repr works without error |
| 192 | + repr_result = repr(awaitable) |
| 193 | + assert isinstance(repr_result, str) |
| 194 | + assert len(repr_result) > 0 |
0 commit comments