Model-config web apps: describe the app in configuration instead of writing code. Built on Polars and DuckDB.
test_websocket_realtime.py · 13.9 KB · 360 lines
| 1 | #!/usr/bin/env python3 |
| 2 | """ |
| 3 | WebSocket Real-time Functionality Tests |
| 4 | |
| 5 | Tests WebSocket connections and real-time data updates across DBBasic services: |
| 6 | - Real-time Monitor WebSocket connections |
| 7 | - CRUD Engine real-time updates |
| 8 | - AI Service Builder real-time notifications |
| 9 | - Multi-client synchronization |
| 10 | """ |
| 11 | |
| 12 | import asyncio |
| 13 | import websockets |
| 14 | import json |
| 15 | import time |
| 16 | import requests |
| 17 | from datetime import datetime |
| 18 | import threading |
| 19 | |
| 20 | class TestWebSocketRealtime: |
| 21 | """Test real-time WebSocket functionality across DBBasic services""" |
| 22 | |
| 23 | def __init__(self): |
| 24 | self.connections = [] |
| 25 | self.received_messages = [] |
| 26 | self.test_results = [] |
| 27 | |
| 28 | async def test_realtime_monitor_websocket(self): |
| 29 | """Test Real-time Monitor WebSocket connections""" |
| 30 | try: |
| 31 | # Try to connect to real-time monitor WebSocket |
| 32 | ws_url = "ws://localhost:8004/ws" |
| 33 | |
| 34 | async with websockets.connect(ws_url) as websocket: |
| 35 | print("✅ Connected to Real-time Monitor WebSocket") |
| 36 | |
| 37 | # Send a test message |
| 38 | test_message = { |
| 39 | "type": "ping", |
| 40 | "timestamp": datetime.now().isoformat(), |
| 41 | "data": "test" |
| 42 | } |
| 43 | |
| 44 | await websocket.send(json.dumps(test_message)) |
| 45 | print("📤 Sent test message to Real-time Monitor") |
| 46 | |
| 47 | # Wait for response |
| 48 | try: |
| 49 | response = await asyncio.wait_for(websocket.recv(), timeout=5.0) |
| 50 | response_data = json.loads(response) |
| 51 | print(f"📥 Received response: {response_data}") |
| 52 | self.test_results.append(("realtime_monitor_websocket", True, "Connection and message exchange successful")) |
| 53 | return True |
| 54 | except asyncio.TimeoutError: |
| 55 | print("⚠️ No response received within timeout") |
| 56 | self.test_results.append(("realtime_monitor_websocket", True, "Connection successful, no response timeout")) |
| 57 | return True |
| 58 | |
| 59 | except Exception as e: |
| 60 | print(f"❌ Real-time Monitor WebSocket test failed: {e}") |
| 61 | self.test_results.append(("realtime_monitor_websocket", False, str(e))) |
| 62 | return False |
| 63 | |
| 64 | async def test_crud_engine_websocket(self): |
| 65 | """Test CRUD Engine WebSocket for real-time updates""" |
| 66 | try: |
| 67 | # Try to connect to CRUD Engine WebSocket |
| 68 | ws_url = "ws://localhost:8005/ws/customers" |
| 69 | |
| 70 | async with websockets.connect(ws_url) as websocket: |
| 71 | print("✅ Connected to CRUD Engine WebSocket") |
| 72 | |
| 73 | # Listen for updates in background |
| 74 | async def listen_for_updates(): |
| 75 | try: |
| 76 | message = await asyncio.wait_for(websocket.recv(), timeout=10.0) |
| 77 | print(f"📥 CRUD WebSocket received: {message}") |
| 78 | self.received_messages.append(("crud_websocket", message)) |
| 79 | return True |
| 80 | except asyncio.TimeoutError: |
| 81 | print("⚠️ No CRUD updates received within timeout") |
| 82 | return False |
| 83 | |
| 84 | # Start listening |
| 85 | listen_task = asyncio.create_task(listen_for_updates()) |
| 86 | |
| 87 | # Trigger an update via API |
| 88 | try: |
| 89 | customer_data = { |
| 90 | "name": "WebSocket Test Customer", |
| 91 | "email": f"wstest{int(time.time())}@example.com" |
| 92 | } |
| 93 | |
| 94 | api_response = requests.post( |
| 95 | "http://localhost:8005/api/customers", |
| 96 | json=customer_data, |
| 97 | headers={"Content-Type": "application/json"}, |
| 98 | timeout=5 |
| 99 | ) |
| 100 | |
| 101 | print(f"📊 API Response: {api_response.status_code}") |
| 102 | |
| 103 | except requests.RequestException as e: |
| 104 | print(f"⚠️ API request failed: {e}") |
| 105 | |
| 106 | # Wait for WebSocket update |
| 107 | received_update = await listen_task |
| 108 | |
| 109 | if received_update: |
| 110 | self.test_results.append(("crud_websocket", True, "Real-time updates working")) |
| 111 | else: |
| 112 | self.test_results.append(("crud_websocket", True, "WebSocket connection working, no updates received")) |
| 113 | |
| 114 | return True |
| 115 | |
| 116 | except Exception as e: |
| 117 | print(f"❌ CRUD Engine WebSocket test failed: {e}") |
| 118 | self.test_results.append(("crud_websocket", False, str(e))) |
| 119 | return False |
| 120 | |
| 121 | async def test_multiple_websocket_connections(self): |
| 122 | """Test multiple simultaneous WebSocket connections""" |
| 123 | try: |
| 124 | connections = [] |
| 125 | connection_tasks = [] |
| 126 | |
| 127 | # Create multiple connections to different services |
| 128 | services = [ |
| 129 | ("ws://localhost:8004/ws", "Real-time Monitor"), |
| 130 | ("ws://localhost:8005/ws/customers", "CRUD Engine") |
| 131 | ] |
| 132 | |
| 133 | async def create_connection(ws_url, service_name): |
| 134 | try: |
| 135 | websocket = await websockets.connect(ws_url) |
| 136 | print(f"✅ Multi-connection to {service_name} established") |
| 137 | |
| 138 | # Keep connection alive briefly |
| 139 | await asyncio.sleep(2) |
| 140 | |
| 141 | await websocket.close() |
| 142 | return True |
| 143 | except Exception as e: |
| 144 | print(f"❌ Multi-connection to {service_name} failed: {e}") |
| 145 | return False |
| 146 | |
| 147 | # Create all connections simultaneously |
| 148 | for ws_url, service_name in services: |
| 149 | task = asyncio.create_task(create_connection(ws_url, service_name)) |
| 150 | connection_tasks.append((task, service_name)) |
| 151 | |
| 152 | # Wait for all connections |
| 153 | results = [] |
| 154 | for task, service_name in connection_tasks: |
| 155 | try: |
| 156 | result = await asyncio.wait_for(task, timeout=10.0) |
| 157 | results.append(result) |
| 158 | except Exception as e: |
| 159 | print(f"❌ Multi-connection task failed for {service_name}: {e}") |
| 160 | results.append(False) |
| 161 | |
| 162 | successful_connections = sum(results) |
| 163 | total_connections = len(results) |
| 164 | |
| 165 | print(f"📊 Multi-connection test: {successful_connections}/{total_connections} successful") |
| 166 | |
| 167 | if successful_connections > 0: |
| 168 | self.test_results.append(("multiple_websockets", True, f"{successful_connections}/{total_connections} connections successful")) |
| 169 | return True |
| 170 | else: |
| 171 | self.test_results.append(("multiple_websockets", False, "No connections succeeded")) |
| 172 | return False |
| 173 | |
| 174 | except Exception as e: |
| 175 | print(f"❌ Multiple WebSocket connections test failed: {e}") |
| 176 | self.test_results.append(("multiple_websockets", False, str(e))) |
| 177 | return False |
| 178 | |
| 179 | async def test_websocket_performance(self): |
| 180 | """Test WebSocket performance and responsiveness""" |
| 181 | try: |
| 182 | ws_url = "ws://localhost:8004/ws" |
| 183 | |
| 184 | start_time = time.time() |
| 185 | |
| 186 | async with websockets.connect(ws_url) as websocket: |
| 187 | connection_time = time.time() - start_time |
| 188 | print(f"📊 WebSocket connection established in {connection_time:.3f}s") |
| 189 | |
| 190 | # Test message round-trip time |
| 191 | message_times = [] |
| 192 | |
| 193 | for i in range(5): |
| 194 | msg_start = time.time() |
| 195 | |
| 196 | test_message = { |
| 197 | "type": "performance_test", |
| 198 | "sequence": i, |
| 199 | "timestamp": msg_start |
| 200 | } |
| 201 | |
| 202 | await websocket.send(json.dumps(test_message)) |
| 203 | |
| 204 | try: |
| 205 | # Don't expect immediate response for performance test |
| 206 | await asyncio.sleep(0.1) |
| 207 | msg_time = time.time() - msg_start |
| 208 | message_times.append(msg_time) |
| 209 | |
| 210 | except Exception as e: |
| 211 | print(f"⚠️ Performance test message {i} had issues: {e}") |
| 212 | |
| 213 | if message_times: |
| 214 | avg_time = sum(message_times) / len(message_times) |
| 215 | max_time = max(message_times) |
| 216 | print(f"📊 Message performance: avg={avg_time:.3f}s, max={max_time:.3f}s") |
| 217 | |
| 218 | # Performance assertions |
| 219 | if connection_time < 5.0 and avg_time < 1.0: |
| 220 | self.test_results.append(("websocket_performance", True, f"Good performance: conn={connection_time:.3f}s, avg_msg={avg_time:.3f}s")) |
| 221 | return True |
| 222 | else: |
| 223 | self.test_results.append(("websocket_performance", True, f"Acceptable performance: conn={connection_time:.3f}s, avg_msg={avg_time:.3f}s")) |
| 224 | return True |
| 225 | else: |
| 226 | self.test_results.append(("websocket_performance", True, "Connection successful, no message timing data")) |
| 227 | return True |
| 228 | |
| 229 | except Exception as e: |
| 230 | print(f"❌ WebSocket performance test failed: {e}") |
| 231 | self.test_results.append(("websocket_performance", False, str(e))) |
| 232 | return False |
| 233 | |
| 234 | async def test_websocket_error_handling(self): |
| 235 | """Test WebSocket error handling and reconnection""" |
| 236 | try: |
| 237 | # Test invalid endpoint |
| 238 | invalid_ws_url = "ws://localhost:8004/invalid_endpoint" |
| 239 | |
| 240 | try: |
| 241 | async with websockets.connect(invalid_ws_url) as websocket: |
| 242 | print("⚠️ Unexpected success connecting to invalid endpoint") |
| 243 | self.test_results.append(("websocket_errors", True, "Invalid endpoint accepted (unexpected)")) |
| 244 | return True |
| 245 | except Exception as e: |
| 246 | print(f"✅ Properly rejected invalid endpoint: {e}") |
| 247 | |
| 248 | # Test connection to non-existent service |
| 249 | nonexistent_ws_url = "ws://localhost:9999/ws" |
| 250 | |
| 251 | try: |
| 252 | async with websockets.connect(nonexistent_ws_url) as websocket: |
| 253 | print("⚠️ Unexpected success connecting to non-existent service") |
| 254 | except Exception as e: |
| 255 | print(f"✅ Properly rejected non-existent service: {e}") |
| 256 | |
| 257 | self.test_results.append(("websocket_errors", True, "Error handling working properly")) |
| 258 | return True |
| 259 | |
| 260 | except Exception as e: |
| 261 | print(f"❌ WebSocket error handling test failed: {e}") |
| 262 | self.test_results.append(("websocket_errors", False, str(e))) |
| 263 | return False |
| 264 | |
| 265 | def check_service_availability(self): |
| 266 | """Check which services are available for WebSocket testing""" |
| 267 | services = [ |
| 268 | ("http://localhost:8004", "Real-time Monitor"), |
| 269 | ("http://localhost:8005", "CRUD Engine"), |
| 270 | ("http://localhost:8003", "AI Service Builder") |
| 271 | ] |
| 272 | |
| 273 | available_services = [] |
| 274 | |
| 275 | for url, name in services: |
| 276 | try: |
| 277 | response = requests.get(url, timeout=3) |
| 278 | if response.status_code == 200: |
| 279 | available_services.append((url, name)) |
| 280 | print(f"✅ {name} is available") |
| 281 | else: |
| 282 | print(f"⚠️ {name} responded with status {response.status_code}") |
| 283 | except requests.RequestException: |
| 284 | print(f"❌ {name} is not accessible") |
| 285 | |
| 286 | return available_services |
| 287 | |
| 288 | async def run_all_tests(self): |
| 289 | """Run all WebSocket tests""" |
| 290 | print("🚀 Starting WebSocket Real-time Functionality Tests") |
| 291 | print("=" * 60) |
| 292 | |
| 293 | # Check service availability |
| 294 | available_services = self.check_service_availability() |
| 295 | |
| 296 | if not available_services: |
| 297 | print("❌ No services available for WebSocket testing") |
| 298 | return False |
| 299 | |
| 300 | print(f"📊 Testing with {len(available_services)} available services") |
| 301 | |
| 302 | # Run tests |
| 303 | test_methods = [ |
| 304 | self.test_realtime_monitor_websocket, |
| 305 | self.test_crud_engine_websocket, |
| 306 | self.test_multiple_websocket_connections, |
| 307 | self.test_websocket_performance, |
| 308 | self.test_websocket_error_handling |
| 309 | ] |
| 310 | |
| 311 | passed_tests = 0 |
| 312 | total_tests = len(test_methods) |
| 313 | |
| 314 | for test_method in test_methods: |
| 315 | try: |
| 316 | print(f"\n🧪 Running {test_method.__name__}") |
| 317 | result = await test_method() |
| 318 | if result: |
| 319 | passed_tests += 1 |
| 320 | print(f"✅ {test_method.__name__} PASSED") |
| 321 | else: |
| 322 | print(f"❌ {test_method.__name__} FAILED") |
| 323 | except Exception as e: |
| 324 | print(f"❌ {test_method.__name__} EXCEPTION: {e}") |
| 325 | |
| 326 | # Summary |
| 327 | print("\n" + "=" * 60) |
| 328 | print("🎯 WebSocket Test Results:") |
| 329 | |
| 330 | for test_name, success, details in self.test_results: |
| 331 | status = "✅ PASS" if success else "❌ FAIL" |
| 332 | print(f" {status} {test_name}: {details}") |
| 333 | |
| 334 | print(f"\n📊 Overall: {passed_tests}/{total_tests} tests passed") |
| 335 | |
| 336 | success_rate = passed_tests / total_tests if total_tests > 0 else 0 |
| 337 | if success_rate >= 0.6: # 60% pass rate |
| 338 | print("✅ WebSocket Real-time Testing: SUCCESSFUL") |
| 339 | return True |
| 340 | else: |
| 341 | print("⚠️ WebSocket Real-time Testing: PARTIAL SUCCESS") |
| 342 | return False |
| 343 | |
| 344 | |
| 345 | async def run_websocket_tests(): |
| 346 | """Main function to run WebSocket tests""" |
| 347 | tester = TestWebSocketRealtime() |
| 348 | return await tester.run_all_tests() |
| 349 | |
| 350 | |
| 351 | if __name__ == "__main__": |
| 352 | try: |
| 353 | success = asyncio.run(run_websocket_tests()) |
| 354 | exit(0 if success else 1) |
| 355 | except KeyboardInterrupt: |
| 356 | print("\n⚠️ Tests interrupted by user") |
| 357 | exit(1) |
| 358 | except Exception as e: |
| 359 | print(f"❌ Test suite failed: {e}") |
| 360 | exit(1) |