|
| 1 | +import asyncio |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from realtime._async.timer import AsyncTimer |
| 6 | + |
| 7 | + |
| 8 | +def linear_backoff(tries: int) -> int: |
| 9 | + return tries * 0.1 |
| 10 | + |
| 11 | + |
| 12 | +@pytest.mark.asyncio |
| 13 | +async def test_timer_initialization(): |
| 14 | + async def callback(): |
| 15 | + pass |
| 16 | + |
| 17 | + timer = AsyncTimer(callback, linear_backoff) |
| 18 | + assert timer.tries == 0 |
| 19 | + assert timer.timer is None |
| 20 | + assert timer.callback == callback |
| 21 | + assert timer.timer_calc == linear_backoff |
| 22 | + |
| 23 | + |
| 24 | +@pytest.mark.asyncio |
| 25 | +async def test_timer_schedule(): |
| 26 | + callback_called = False |
| 27 | + |
| 28 | + async def callback(): |
| 29 | + nonlocal callback_called |
| 30 | + callback_called = True |
| 31 | + |
| 32 | + timer = AsyncTimer(callback, linear_backoff) |
| 33 | + timer.schedule_timeout() |
| 34 | + |
| 35 | + assert timer.tries == 1 |
| 36 | + assert timer.timer is not None |
| 37 | + assert not timer.timer.done() |
| 38 | + |
| 39 | + # Wait for the timer to complete |
| 40 | + await timer.timer |
| 41 | + assert callback_called |
| 42 | + |
| 43 | + |
| 44 | +@pytest.mark.asyncio |
| 45 | +async def test_timer_reset(): |
| 46 | + callback_called = False |
| 47 | + |
| 48 | + async def callback(): |
| 49 | + nonlocal callback_called |
| 50 | + callback_called = True |
| 51 | + |
| 52 | + timer = AsyncTimer(callback, linear_backoff) |
| 53 | + timer.schedule_timeout() |
| 54 | + |
| 55 | + # Reset before the timer completes |
| 56 | + timer.reset() |
| 57 | + assert timer.tries == 0 |
| 58 | + assert timer.timer is None |
| 59 | + |
| 60 | + # Wait a bit to ensure the original timer doesn't fire |
| 61 | + await asyncio.sleep(0.2) |
| 62 | + assert not callback_called |
| 63 | + |
| 64 | + |
| 65 | +@pytest.mark.asyncio |
| 66 | +async def test_timer_multiple_schedules(): |
| 67 | + callback_count = 0 |
| 68 | + |
| 69 | + async def callback(): |
| 70 | + nonlocal callback_count |
| 71 | + callback_count += 1 |
| 72 | + |
| 73 | + timer = AsyncTimer(callback, linear_backoff) |
| 74 | + |
| 75 | + # Schedule multiple times |
| 76 | + timer.schedule_timeout() |
| 77 | + timer.schedule_timeout() |
| 78 | + timer.schedule_timeout() |
| 79 | + |
| 80 | + assert timer.tries == 3 |
| 81 | + assert timer.timer is not None |
| 82 | + |
| 83 | + # Wait for the last timer to complete |
| 84 | + await timer.timer |
| 85 | + assert callback_count == 1 # Only the last schedule should fire |
| 86 | + |
| 87 | + |
| 88 | +@pytest.mark.asyncio |
| 89 | +async def test_timer_callback_error(): |
| 90 | + error_caught = False |
| 91 | + |
| 92 | + async def callback(): |
| 93 | + raise ValueError("Test error") |
| 94 | + |
| 95 | + timer = AsyncTimer(callback, linear_backoff) |
| 96 | + timer.schedule_timeout() |
| 97 | + |
| 98 | + # Wait for the timer to complete |
| 99 | + await timer.timer |
| 100 | + # The error should be caught and logged, but not re-raised |
| 101 | + assert timer.timer.done() |
| 102 | + |
| 103 | + |
| 104 | +@pytest.mark.asyncio |
| 105 | +async def test_timer_cancellation(): |
| 106 | + callback_called = False |
| 107 | + |
| 108 | + async def callback(): |
| 109 | + nonlocal callback_called |
| 110 | + callback_called = True |
| 111 | + |
| 112 | + timer = AsyncTimer(callback, linear_backoff) |
| 113 | + timer.schedule_timeout() |
| 114 | + |
| 115 | + # Cancel the timer |
| 116 | + timer.timer.cancel() |
| 117 | + |
| 118 | + # Wait a bit to ensure the timer doesn't fire |
| 119 | + await asyncio.sleep(0.2) |
| 120 | + assert not callback_called |
| 121 | + assert timer.timer.done() |
| 122 | + assert timer.timer.cancelled() |
0 commit comments