#!/usr/bin/env python3 """ Test script to verify health check improvements for data-ingestion-service """ import asyncio import aiohttp import time import sys from datetime import datetime async def test_health_check(): """Test the health check endpoint with improved startup handling""" print("Testing data-ingestion-service health check improvements...") print("=" * 60) service_url = "http://localhost:8008" async with aiohttp.ClientSession() as session: # Test 1: Check if service responds at all print("Test 1: Basic connectivity") try: async with session.get(f"{service_url}/", timeout=5) as response: if response.status == 200: data = await response.json() print(f"✅ Service responding: {data['service']}") else: print(f"❌ Service returned status {response.status}") except Exception as e: print(f"❌ Service not reachable: {e}") print("Make sure the service is running: python main.py") return False print() # Test 2: Health check during startup (multiple checks) print("Test 2: Health check progression during startup") print("Checking health status every 2 seconds for 30 seconds...") startup_healthy = False for i in range(15): # 30 seconds total try: async with session.get(f"{service_url}/health", timeout=3) as response: data = await response.json() service_status = data.get('service', 'unknown') db_status = data.get('database', 'unknown') ftp_status = data.get('ftp_monitor', 'unknown') status_icon = "✅" if service_status == "healthy" else "🟡" if service_status == "starting" else "❌" print(f" {i+1:2d}s: {status_icon} Service={service_status}, DB={db_status}, FTP={ftp_status}") if service_status == "healthy": startup_healthy = True print(f" 🎉 Service became healthy after {(i+1)*2} seconds!") break if service_status not in ["starting", "healthy"]: print(f" ❌ Service in unexpected state: {service_status}") break except asyncio.TimeoutError: print(f" {i+1:2d}s: ⏰ Health check timeout") except Exception as e: print(f" {i+1:2d}s: ❌ Error: {e}") await asyncio.sleep(2) print() # Test 3: Final detailed health status print("Test 3: Detailed health status") try: async with session.get(f"{service_url}/health", timeout=5) as response: data = await response.json() print("Final health status:") print(f" Service Status: {data.get('service', 'unknown')}") print(f" Database: {data.get('database', 'unknown')}") print(f" FTP Monitor: {data.get('ftp_monitor', 'unknown')}") print(f" Last Check: {data.get('last_check', 'none')}") print(f" Files Processed: {data.get('files_processed', 0)}") if 'issues' in data: print(f" Issues: {data['issues']}") except Exception as e: print(f"❌ Error getting final status: {e}") print() # Test 4: Readiness check print("Test 4: Readiness check") try: async with session.get(f"{service_url}/readiness", timeout=5) as response: if response.status == 200: data = await response.json() print(f"✅ Service is ready: {data.get('ready', False)}") print(f" FTP Monitor Status: {data.get('ftp_monitor_status', 'unknown')}") else: text = await response.text() print(f"❌ Service not ready (HTTP {response.status}): {text}") except Exception as e: print(f"❌ Error checking readiness: {e}") print() print("=" * 60) if startup_healthy: print("✅ SUCCESS: Service health check improvements are working!") print(" - Service can become healthy even during FTP initialization") print(" - Health checks are responsive and don't block") return True else: print("⚠️ WARNING: Service didn't become healthy within 30 seconds") print(" This might be expected if:") print(" - Database connection is slow") print(" - FTP server is unreachable") print(" - Service is still initializing (check logs)") return False if __name__ == "__main__": print(f"Starting health check test at {datetime.now()}") try: success = asyncio.run(test_health_check()) sys.exit(0 if success else 1) except KeyboardInterrupt: print("\nTest interrupted by user") sys.exit(1) except Exception as e: print(f"Test failed with error: {e}") sys.exit(1)