|
| 1 | +import asyncio |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from returns.primitives.reawaitable import ReAwaitable |
| 6 | + |
| 7 | + |
| 8 | +class CallCounter: |
| 9 | + """Helper class to count function calls.""" |
| 10 | + |
| 11 | + def __init__(self) -> None: |
| 12 | + """Initialize counter.""" |
| 13 | + self.count = 0 |
| 14 | + |
| 15 | + def increment(self) -> None: |
| 16 | + """Increment the counter.""" |
| 17 | + self.count += 1 |
| 18 | + |
| 19 | + |
| 20 | +@pytest.mark.asyncio |
| 21 | +async def test_concurrent_await(): |
| 22 | + """Test that ReAwaitable can be awaited concurrently from multiple tasks.""" |
| 23 | + counter = CallCounter() |
| 24 | + |
| 25 | + async def example_coro() -> int: |
| 26 | + counter.increment() |
| 27 | + await asyncio.sleep(0.01) # Simulate some async work |
| 28 | + return 42 |
| 29 | + |
| 30 | + awaitable = ReAwaitable(example_coro()) |
| 31 | + |
| 32 | + async def await_helper(): |
| 33 | + return await awaitable |
| 34 | + |
| 35 | + # Create multiple tasks that await the same ReAwaitable instance |
| 36 | + tasks = [ |
| 37 | + asyncio.create_task(await_helper()), |
| 38 | + asyncio.create_task(await_helper()), |
| 39 | + asyncio.create_task(await_helper()), |
| 40 | + ] |
| 41 | + |
| 42 | + # All tasks should complete without error |
| 43 | + gathered_results = await asyncio.gather(*tasks, return_exceptions=True) |
| 44 | + |
| 45 | + # Check that no exceptions were raised |
| 46 | + for result in gathered_results: |
| 47 | + assert not isinstance(result, Exception) |
| 48 | + |
| 49 | + # The underlying coroutine should only be called once |
| 50 | + assert counter.count == 1 |
| 51 | + |
| 52 | + # All results should be the same |
| 53 | + assert all(res == 42 for res in gathered_results) |
| 54 | + |
| 55 | + |
| 56 | +@pytest.mark.asyncio |
| 57 | +async def test_concurrent_await_with_different_values(): |
| 58 | + """Test that multiple ReAwaitable instances work correctly.""" |
| 59 | + |
| 60 | + async def example_with_value(input_value: int) -> int: |
| 61 | + await asyncio.sleep(0.01) |
| 62 | + return input_value |
| 63 | + |
| 64 | + awaitables = [ |
| 65 | + ReAwaitable(example_with_value(0)), |
| 66 | + ReAwaitable(example_with_value(1)), |
| 67 | + ReAwaitable(example_with_value(2)), |
| 68 | + ] |
| 69 | + |
| 70 | + async def await_helper_with_arg(awaitable_arg): |
| 71 | + return await awaitable_arg |
| 72 | + |
| 73 | + # Create tasks for each awaitable |
| 74 | + tasks = [] |
| 75 | + for awaitable in awaitables: |
| 76 | + # Each awaitable is awaited multiple times |
| 77 | + tasks.extend([ |
| 78 | + asyncio.create_task(await_helper_with_arg(awaitable)), |
| 79 | + asyncio.create_task(await_helper_with_arg(awaitable)), |
| 80 | + ]) |
| 81 | + |
| 82 | + gathered_results = await asyncio.gather(*tasks, return_exceptions=True) |
| 83 | + |
| 84 | + # Check that no exceptions were raised |
| 85 | + for result in gathered_results: |
| 86 | + assert not isinstance(result, Exception) |
| 87 | + |
| 88 | + # Check that each awaitable returned its correct value multiple times |
| 89 | + assert gathered_results[0] == gathered_results[1] == 0 |
| 90 | + assert gathered_results[2] == gathered_results[3] == 1 |
| 91 | + assert gathered_results[4] == gathered_results[5] == 2 |
| 92 | + |
| 93 | + |
| 94 | +@pytest.mark.asyncio |
| 95 | +async def test_sequential_await(): |
| 96 | + """Test that ReAwaitable still works correctly with sequential awaits.""" |
| 97 | + counter = CallCounter() |
| 98 | + |
| 99 | + async def example_sequential() -> int: |
| 100 | + counter.increment() |
| 101 | + return 42 |
| 102 | + |
| 103 | + awaitable = ReAwaitable(example_sequential()) |
| 104 | + |
| 105 | + # Sequential awaits should work as before |
| 106 | + result1 = await awaitable |
| 107 | + result2 = await awaitable |
| 108 | + result3 = await awaitable |
| 109 | + |
| 110 | + assert result1 == result2 == result3 == 42 |
| 111 | + assert counter.count == 1 # Should only be called once |
0 commit comments