|
| 1 | +"""Tests for global rate limiter functionality.""" |
| 2 | + |
| 3 | +import asyncio |
| 4 | +import pytest |
| 5 | +from unittest.mock import patch |
| 6 | + |
| 7 | +from steam_mcp.client.steam_client import ( |
| 8 | + DEFAULT_RATE_LIMIT, |
| 9 | + RateLimiter, |
| 10 | + SteamClient, |
| 11 | + get_global_rate_limiter, |
| 12 | + reset_global_rate_limiter, |
| 13 | +) |
| 14 | + |
| 15 | + |
| 16 | +@pytest.fixture(autouse=True) |
| 17 | +def reset_limiter(): |
| 18 | + """Reset global rate limiter before each test.""" |
| 19 | + reset_global_rate_limiter() |
| 20 | + yield |
| 21 | + reset_global_rate_limiter() |
| 22 | + |
| 23 | + |
| 24 | +@pytest.fixture |
| 25 | +def mock_env(): |
| 26 | + """Set up mock environment variables.""" |
| 27 | + with patch.dict("os.environ", {"STEAM_API_KEY": "test_key"}, clear=False): |
| 28 | + yield |
| 29 | + |
| 30 | + |
| 31 | +class TestGetGlobalRateLimiter: |
| 32 | + """Tests for get_global_rate_limiter function.""" |
| 33 | + |
| 34 | + def test_returns_rate_limiter_instance(self): |
| 35 | + """Should return a RateLimiter instance.""" |
| 36 | + limiter = get_global_rate_limiter() |
| 37 | + assert isinstance(limiter, RateLimiter) |
| 38 | + |
| 39 | + def test_returns_same_instance(self): |
| 40 | + """Should return the same instance on subsequent calls.""" |
| 41 | + limiter1 = get_global_rate_limiter() |
| 42 | + limiter2 = get_global_rate_limiter() |
| 43 | + assert limiter1 is limiter2 |
| 44 | + |
| 45 | + def test_uses_default_rate_limit(self): |
| 46 | + """Should use DEFAULT_RATE_LIMIT when env var not set.""" |
| 47 | + limiter = get_global_rate_limiter() |
| 48 | + assert limiter.requests_per_second == DEFAULT_RATE_LIMIT |
| 49 | + |
| 50 | + def test_uses_env_var_rate_limit(self): |
| 51 | + """Should use STEAM_RATE_LIMIT env var when set.""" |
| 52 | + with patch.dict("os.environ", {"STEAM_RATE_LIMIT": "5.0"}): |
| 53 | + reset_global_rate_limiter() |
| 54 | + limiter = get_global_rate_limiter() |
| 55 | + assert limiter.requests_per_second == 5.0 |
| 56 | + |
| 57 | + |
| 58 | +class TestResetGlobalRateLimiter: |
| 59 | + """Tests for reset_global_rate_limiter function.""" |
| 60 | + |
| 61 | + def test_reset_creates_new_instance(self): |
| 62 | + """After reset, should create a new instance.""" |
| 63 | + limiter1 = get_global_rate_limiter() |
| 64 | + reset_global_rate_limiter() |
| 65 | + limiter2 = get_global_rate_limiter() |
| 66 | + assert limiter1 is not limiter2 |
| 67 | + |
| 68 | + |
| 69 | +class TestSteamClientRateLimiter: |
| 70 | + """Tests for SteamClient rate limiter integration.""" |
| 71 | + |
| 72 | + def test_uses_global_rate_limiter_by_default(self, mock_env): |
| 73 | + """SteamClient should use global rate limiter by default.""" |
| 74 | + global_limiter = get_global_rate_limiter() |
| 75 | + client = SteamClient() |
| 76 | + assert client.rate_limiter is global_limiter |
| 77 | + |
| 78 | + def test_multiple_clients_share_rate_limiter(self, mock_env): |
| 79 | + """Multiple SteamClient instances should share the same rate limiter.""" |
| 80 | + client1 = SteamClient() |
| 81 | + client2 = SteamClient() |
| 82 | + assert client1.rate_limiter is client2.rate_limiter |
| 83 | + |
| 84 | + def test_can_use_custom_rate_limiter(self, mock_env): |
| 85 | + """SteamClient can use a custom rate limiter.""" |
| 86 | + custom_limiter = RateLimiter(requests_per_second=5.0) |
| 87 | + client = SteamClient(rate_limiter=custom_limiter) |
| 88 | + assert client.rate_limiter is custom_limiter |
| 89 | + assert client.rate_limiter is not get_global_rate_limiter() |
| 90 | + |
| 91 | + def test_backward_compat_requests_per_second(self, mock_env): |
| 92 | + """SteamClient should support deprecated requests_per_second param.""" |
| 93 | + client = SteamClient(requests_per_second=5.0) |
| 94 | + # Should create a dedicated limiter, not use global |
| 95 | + assert client.rate_limiter is not get_global_rate_limiter() |
| 96 | + assert client.rate_limiter.requests_per_second == 5.0 |
| 97 | + |
| 98 | + def test_rate_limiter_takes_precedence_over_requests_per_second(self, mock_env): |
| 99 | + """Explicit rate_limiter should take precedence over requests_per_second.""" |
| 100 | + custom_limiter = RateLimiter(requests_per_second=20.0) |
| 101 | + client = SteamClient(rate_limiter=custom_limiter, requests_per_second=5.0) |
| 102 | + assert client.rate_limiter is custom_limiter |
| 103 | + assert client.rate_limiter.requests_per_second == 20.0 |
| 104 | + |
| 105 | + |
| 106 | +class TestConcurrentAccess: |
| 107 | + """Tests for concurrent access to rate limiter.""" |
| 108 | + |
| 109 | + @pytest.mark.asyncio |
| 110 | + async def test_concurrent_acquire_is_serialized(self): |
| 111 | + """Concurrent acquire calls should be properly serialized.""" |
| 112 | + limiter = RateLimiter(requests_per_second=100.0) # Fast for testing |
| 113 | + acquire_times = [] |
| 114 | + |
| 115 | + async def track_acquire(): |
| 116 | + await limiter.acquire() |
| 117 | + acquire_times.append(asyncio.get_event_loop().time()) |
| 118 | + |
| 119 | + # Launch multiple concurrent acquires |
| 120 | + tasks = [asyncio.create_task(track_acquire()) for _ in range(5)] |
| 121 | + await asyncio.gather(*tasks) |
| 122 | + |
| 123 | + # Verify all acquired (should have 5 entries) |
| 124 | + assert len(acquire_times) == 5 |
| 125 | + |
| 126 | + @pytest.mark.asyncio |
| 127 | + async def test_global_limiter_serializes_across_clients(self, mock_env): |
| 128 | + """Global rate limiter should serialize requests across multiple clients.""" |
| 129 | + reset_global_rate_limiter() |
| 130 | + |
| 131 | + # Create multiple clients sharing the global limiter |
| 132 | + client1 = SteamClient() |
| 133 | + client2 = SteamClient() |
| 134 | + |
| 135 | + # Verify they share the same rate limiter |
| 136 | + assert client1.rate_limiter is client2.rate_limiter |
| 137 | + |
| 138 | + acquire_count = 0 |
| 139 | + lock = asyncio.Lock() |
| 140 | + |
| 141 | + async def acquire_from_client(client): |
| 142 | + nonlocal acquire_count |
| 143 | + await client.rate_limiter.acquire() |
| 144 | + async with lock: |
| 145 | + acquire_count += 1 |
| 146 | + |
| 147 | + # Launch concurrent acquires from different clients |
| 148 | + tasks = [ |
| 149 | + asyncio.create_task(acquire_from_client(client1)), |
| 150 | + asyncio.create_task(acquire_from_client(client2)), |
| 151 | + asyncio.create_task(acquire_from_client(client1)), |
| 152 | + asyncio.create_task(acquire_from_client(client2)), |
| 153 | + ] |
| 154 | + await asyncio.gather(*tasks) |
| 155 | + |
| 156 | + # All should have completed |
| 157 | + assert acquire_count == 4 |
| 158 | + |
| 159 | + @pytest.mark.asyncio |
| 160 | + async def test_rate_limiter_enforces_rate(self): |
| 161 | + """Rate limiter should enforce the configured rate limit.""" |
| 162 | + import time |
| 163 | + |
| 164 | + # Very slow rate to make timing measurable |
| 165 | + limiter = RateLimiter(requests_per_second=10.0) # 100ms between requests |
| 166 | + |
| 167 | + start = time.monotonic() |
| 168 | + |
| 169 | + # Make 3 requests |
| 170 | + await limiter.acquire() |
| 171 | + await limiter.acquire() |
| 172 | + await limiter.acquire() |
| 173 | + |
| 174 | + elapsed = time.monotonic() - start |
| 175 | + |
| 176 | + # Should take at least 200ms for 3 requests at 10 req/s |
| 177 | + # (first is instant, second waits 100ms, third waits 100ms) |
| 178 | + assert elapsed >= 0.18 # Allow small margin for timing variance |
| 179 | + |
| 180 | + @pytest.mark.asyncio |
| 181 | + async def test_no_race_conditions_under_load(self, mock_env): |
| 182 | + """Global limiter should handle many concurrent requests without races.""" |
| 183 | + reset_global_rate_limiter() |
| 184 | + |
| 185 | + # Use a faster rate for this test |
| 186 | + with patch.dict("os.environ", {"STEAM_RATE_LIMIT": "1000.0"}): |
| 187 | + reset_global_rate_limiter() |
| 188 | + |
| 189 | + # Create multiple clients |
| 190 | + clients = [SteamClient() for _ in range(3)] |
| 191 | + |
| 192 | + # Verify all share the same limiter |
| 193 | + assert all(c.rate_limiter is clients[0].rate_limiter for c in clients) |
| 194 | + |
| 195 | + results = [] |
| 196 | + lock = asyncio.Lock() |
| 197 | + |
| 198 | + async def acquire_and_record(client_idx): |
| 199 | + client = clients[client_idx % len(clients)] |
| 200 | + await client.rate_limiter.acquire() |
| 201 | + async with lock: |
| 202 | + results.append(client_idx) |
| 203 | + |
| 204 | + # Launch many concurrent requests |
| 205 | + tasks = [ |
| 206 | + asyncio.create_task(acquire_and_record(i)) for i in range(20) |
| 207 | + ] |
| 208 | + await asyncio.gather(*tasks) |
| 209 | + |
| 210 | + # All should complete without errors |
| 211 | + assert len(results) == 20 |
0 commit comments