|
| 1 | +# Async Testing Rules for ProjectX SDK |
| 2 | + |
| 3 | +**CRITICAL**: This SDK is 100% async-first. All testing must follow async patterns correctly. |
| 4 | + |
| 5 | +## Async Test Requirements |
| 6 | + |
| 7 | +### 1. Async Test Decorators (MANDATORY) |
| 8 | + |
| 9 | +**ALWAYS use for async test methods:** |
| 10 | +```python |
| 11 | +import pytest |
| 12 | + |
| 13 | +@pytest.mark.asyncio |
| 14 | +async def test_async_method(): |
| 15 | + # Test implementation |
| 16 | + pass |
| 17 | +``` |
| 18 | + |
| 19 | +**FORBIDDEN patterns:** |
| 20 | +```python |
| 21 | +# ❌ WRONG: Missing @pytest.mark.asyncio |
| 22 | +async def test_async_method(): |
| 23 | + pass |
| 24 | + |
| 25 | +# ❌ WRONG: Using sync test for async code |
| 26 | +def test_async_method(): |
| 27 | + result = asyncio.run(async_method()) |
| 28 | +``` |
| 29 | + |
| 30 | +### 2. Async Context Manager Testing |
| 31 | + |
| 32 | +**ALWAYS test async context managers properly:** |
| 33 | +```python |
| 34 | +@pytest.mark.asyncio |
| 35 | +async def test_projectx_client_context_manager(): |
| 36 | + async with ProjectX.from_env() as client: |
| 37 | + await client.authenticate() |
| 38 | + # Test within context |
| 39 | + assert client.is_authenticated |
| 40 | + # Test cleanup happened |
| 41 | +``` |
| 42 | + |
| 43 | +### 3. Async Mock Patterns |
| 44 | + |
| 45 | +**Use aioresponses for HTTP mocking:** |
| 46 | +```python |
| 47 | +from aioresponses import aioresponses |
| 48 | +import pytest |
| 49 | + |
| 50 | +@pytest.mark.asyncio |
| 51 | +async def test_api_call(): |
| 52 | + with aioresponses() as m: |
| 53 | + m.get("https://api.example.com/data", payload={"result": "success"}) |
| 54 | + |
| 55 | + async with ProjectX.from_env() as client: |
| 56 | + result = await client.get_data() |
| 57 | + assert result["result"] == "success" |
| 58 | +``` |
| 59 | + |
| 60 | +**Use AsyncMock for async methods:** |
| 61 | +```python |
| 62 | +from unittest.mock import AsyncMock |
| 63 | +import pytest |
| 64 | + |
| 65 | +@pytest.mark.asyncio |
| 66 | +async def test_realtime_callback(): |
| 67 | + callback = AsyncMock() |
| 68 | + manager = RealtimeDataManager(callback=callback) |
| 69 | + |
| 70 | + await manager.process_tick({"price": 100.0}) |
| 71 | + |
| 72 | + callback.assert_called_once_with({"price": 100.0}) |
| 73 | +``` |
| 74 | + |
| 75 | +### 4. WebSocket Testing Patterns |
| 76 | + |
| 77 | +**Test WebSocket connections with proper async handling:** |
| 78 | +```python |
| 79 | +@pytest.mark.asyncio |
| 80 | +async def test_websocket_connection(): |
| 81 | + async with create_realtime_client("token", "account") as client: |
| 82 | + await client.connect() |
| 83 | + assert client.is_connected |
| 84 | + |
| 85 | + await client.subscribe("MNQ") |
| 86 | + # Test subscription |
| 87 | +``` |
| 88 | + |
| 89 | +### 5. Async Error Handling Tests |
| 90 | + |
| 91 | +**Test async exceptions properly:** |
| 92 | +```python |
| 93 | +@pytest.mark.asyncio |
| 94 | +async def test_api_error_handling(): |
| 95 | + with aioresponses() as m: |
| 96 | + m.get("https://api.example.com/data", status=500) |
| 97 | + |
| 98 | + async with ProjectX.from_env() as client: |
| 99 | + with pytest.raises(ProjectXAPIError): |
| 100 | + await client.get_data() |
| 101 | +``` |
| 102 | + |
| 103 | +## Async Test Organization |
| 104 | + |
| 105 | +### 6. Test File Structure |
| 106 | + |
| 107 | +**Organize async tests by component:** |
| 108 | +``` |
| 109 | +tests/ |
| 110 | +├── unit/ |
| 111 | +│ ├── test_async_client.py # Client async tests |
| 112 | +│ ├── test_async_order_manager.py # OrderManager async tests |
| 113 | +│ └── test_async_realtime.py # Realtime async tests |
| 114 | +├── integration/ |
| 115 | +│ └── test_async_integration.py # Cross-component async tests |
| 116 | +└── e2e/ |
| 117 | + └── test_async_e2e.py # End-to-end async tests |
| 118 | +``` |
| 119 | + |
| 120 | +### 7. Async Test Fixtures |
| 121 | + |
| 122 | +**Create reusable async fixtures:** |
| 123 | +```python |
| 124 | +@pytest_asyncio.fixture |
| 125 | +async def authenticated_client(): |
| 126 | + async with ProjectX.from_env() as client: |
| 127 | + await client.authenticate() |
| 128 | + yield client |
| 129 | + |
| 130 | +@pytest_asyncio.fixture |
| 131 | +async def realtime_client(authenticated_client): |
| 132 | + client = await create_realtime_client( |
| 133 | + authenticated_client.jwt_token, |
| 134 | + str(authenticated_client.account_id) |
| 135 | + ) |
| 136 | + yield client |
| 137 | + await client.close() |
| 138 | +``` |
| 139 | + |
| 140 | +## Performance Testing for Async Code |
| 141 | + |
| 142 | +### 8. Async Performance Tests |
| 143 | + |
| 144 | +**Test async performance characteristics:** |
| 145 | +```python |
| 146 | +@pytest.mark.asyncio |
| 147 | +async def test_concurrent_requests(): |
| 148 | + async with ProjectX.from_env() as client: |
| 149 | + await client.authenticate() |
| 150 | + |
| 151 | + # Test concurrent execution |
| 152 | + tasks = [ |
| 153 | + client.get_bars("MNQ", days=1), |
| 154 | + client.get_bars("ES", days=1), |
| 155 | + client.get_bars("RTY", days=1) |
| 156 | + ] |
| 157 | + |
| 158 | + start_time = time.time() |
| 159 | + results = await asyncio.gather(*tasks) |
| 160 | + duration = time.time() - start_time |
| 161 | + |
| 162 | + assert len(results) == 3 |
| 163 | + assert duration < 5.0 # Should be faster than sequential |
| 164 | +``` |
| 165 | + |
| 166 | +### 9. Memory Leak Testing |
| 167 | + |
| 168 | +**Test for async memory leaks:** |
| 169 | +```python |
| 170 | +@pytest.mark.asyncio |
| 171 | +async def test_no_memory_leaks(): |
| 172 | + import gc |
| 173 | + import tracemalloc |
| 174 | + |
| 175 | + tracemalloc.start() |
| 176 | + |
| 177 | + for _ in range(100): |
| 178 | + async with ProjectX.from_env() as client: |
| 179 | + await client.authenticate() |
| 180 | + await client.get_bars("MNQ", days=1) |
| 181 | + |
| 182 | + gc.collect() |
| 183 | + current, peak = tracemalloc.get_traced_memory() |
| 184 | + tracemalloc.stop() |
| 185 | + |
| 186 | + # Memory should not grow unbounded |
| 187 | + assert current < peak * 1.1 |
| 188 | +``` |
| 189 | + |
| 190 | +## Critical Async Testing Violations |
| 191 | + |
| 192 | +**These are NEVER acceptable:** |
| 193 | + |
| 194 | +❌ **Using sync tests for async code** |
| 195 | +❌ **Missing @pytest.mark.asyncio decorator** |
| 196 | +❌ **Using asyncio.run() in test methods** |
| 197 | +❌ **Blocking async code with .result() or similar** |
| 198 | +❌ **Not properly cleaning up async resources** |
| 199 | +❌ **Testing async code synchronously** |
| 200 | +❌ **Ignoring async context manager lifecycle** |
| 201 | + |
| 202 | +## Async Test Execution |
| 203 | + |
| 204 | +**Use proper test execution:** |
| 205 | +```bash |
| 206 | +# ✅ CORRECT: Use test.sh for proper environment |
| 207 | +./test.sh tests/test_async_client.py |
| 208 | + |
| 209 | +# ✅ CORRECT: Run specific async tests |
| 210 | +uv run pytest -k "async" tests/ |
| 211 | + |
| 212 | +# ✅ CORRECT: Run with asyncio mode |
| 213 | +uv run pytest --asyncio-mode=auto tests/ |
| 214 | +``` |
| 215 | + |
| 216 | +## Required Dependencies |
| 217 | + |
| 218 | +**Ensure these are in test dependencies:** |
| 219 | +```toml |
| 220 | +[project.optional-dependencies] |
| 221 | +dev = [ |
| 222 | + "pytest-asyncio>=0.21.0", |
| 223 | + "aioresponses>=0.7.4", |
| 224 | + "pytest>=7.0.0", |
| 225 | +] |
| 226 | +``` |
| 227 | + |
| 228 | +Remember: **Async code requires async tests. No exceptions.** |
0 commit comments