99# Adjust this import path to match your project's structure
1010from forklet .infrastructure .rate_limiter import RateLimiter , RateLimitInfo
1111
12+ # Mark all tests in this file as asyncio
13+ # pytestmark = pytest.mark.asyncio
1214
1315
1416## 1. RateLimitInfo Helper Class Tests
@@ -125,7 +127,7 @@ async def test_acquire_uses_adaptive_delay(mock_sleep):
125127 rl = RateLimiter (default_delay = 1.0 )
126128
127129 # Mock time.time() to simulate time passing
128- with patch ('time.time' , side_effect = [1000.0 , 1000.1 , 1000.2 , 1000.3 ]) as mock_time :
130+ with patch ('time.time' , side_effect = [1000.0 , 1000.1 , 1000.2 , 1000.3 ]):
129131 # Ensure rate limit is not exhausted
130132 rl .rate_limit_info .remaining = 2000
131133
@@ -137,6 +139,12 @@ async def test_acquire_uses_adaptive_delay(mock_sleep):
137139 # SECOND call: This call is close to the first one, triggering the delay.
138140 await rl .acquire ()
139141
142+ # Check that sleep was called. The exact value has jitter, so we check if it was called.
143+ # mock_sleep.assert_called()
144+ # The first call to time.time() is at the start of acquire(),
145+ # the second is for _last_request. The delay calculation uses the first one.
146+ # Expected delay is around 1.0 seconds.
147+ # assert mock_sleep.call_args[0][0] > 0.5
140148 # Assert that sleep was finally called on the second run
141149 mock_sleep .assert_called ()
142150 # The delay should be > 0 because elapsed time (0.1s) < default_delay (1.0s)
@@ -147,7 +155,8 @@ async def test_acquire_updates_last_request_time():
147155 """Test that acquire() correctly updates the _last_request timestamp."""
148156 rl = RateLimiter ()
149157
150- with patch ('time.time' , return_value = 12345.0 ) as mock_time :
158+ with patch ('time.time' , return_value = 12345.0 ):
159+ # Patch sleep to make the test run instantly
151160 with patch ('asyncio.sleep' ):
152161 await rl .acquire ()
153162 assert rl ._last_request == 12345.0
0 commit comments