tts-gpu-service / test_logging_fix.py
Peter Michael Gits
fix: Apply comprehensive uvicorn logging protection
9197e69
#!/usr/bin/env python3
"""
Test script to validate the comprehensive uvicorn logging fix for TTS service.
This script tests all layers of protection without starting the full service.
"""
import sys
import os
import subprocess
import tempfile
def test_import_safety():
"""Test that imports work without logging conflicts"""
print("πŸ§ͺ Testing import safety...")
test_code = '''
import sys
print("[TEST] Starting import test")
# This should trigger our SafeStream protection
import os
import warnings
from typing import List, Dict, Any, Optional
# Environment protection should be active
print(f"[TEST] PYTHONWARNINGS: {os.environ.get('PYTHONWARNINGS', 'not set')}")
print(f"[TEST] GRADIO_ANALYTICS_ENABLED: {os.environ.get('GRADIO_ANALYTICS_ENABLED', 'not set')}")
# Logging should be completely disabled
import logging
print(f"[TEST] Logging disabled: {logging.root.disabled}")
# Safe imports that previously caused conflicts
try:
import gradio as gr
print("[TEST] βœ… Gradio import successful")
except Exception as e:
print(f"[TEST] ❌ Gradio import failed: {e}")
try:
import torch
print("[TEST] βœ… PyTorch import successful")
except Exception as e:
print(f"[TEST] ❌ PyTorch import failed: {e}")
try:
import transformers
print("[TEST] βœ… Transformers import successful")
except Exception as e:
print(f"[TEST] ❌ Transformers import failed: {e}")
print("[TEST] Import test completed")
'''
# Write test code to temporary file
with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as f:
f.write(test_code)
temp_file = f.name
try:
# Run the test code using the TTS app imports
result = subprocess.run([
sys.executable, '-c',
f'exec(open("{os.path.abspath("app.py")}").read()[:2000]); exec(open("{temp_file}").read())'
], capture_output=True, text=True, cwd=os.path.dirname(os.path.abspath(__file__)))
print("STDOUT:")
print(result.stdout)
if result.stderr:
print("STDERR:")
print(result.stderr)
if result.returncode == 0:
print("βœ… Import safety test PASSED")
return True
else:
print("❌ Import safety test FAILED")
return False
finally:
os.unlink(temp_file)
def test_safe_log_function():
"""Test the safe_log function"""
print("\nπŸ§ͺ Testing safe_log function...")
test_code = '''
# Import the safe_log function from app.py
import sys
import os
exec(open("app.py").read()[:3000]) # Import first part including safe_log
# Test safe_log function
try:
safe_log("info", "Test info message")
safe_log("warning", "Test warning message")
safe_log("error", "Test error message")
print("[TEST] βœ… safe_log function working correctly")
except Exception as e:
print(f"[TEST] ❌ safe_log function failed: {e}")
'''
result = subprocess.run([
sys.executable, '-c', test_code
], capture_output=True, text=True, cwd=os.path.dirname(os.path.abspath(__file__)))
print("STDOUT:")
print(result.stdout)
if result.stderr:
print("STDERR:")
print(result.stderr)
if result.returncode == 0 and "[TTS-INFO]" in result.stdout:
print("βœ… safe_log function test PASSED")
return True
else:
print("❌ safe_log function test FAILED")
return False
def test_syntax_validation():
"""Test Python syntax validation"""
print("\nπŸ§ͺ Testing syntax validation...")
result = subprocess.run([
sys.executable, '-m', 'py_compile', 'app.py'
], capture_output=True, text=True, cwd=os.path.dirname(os.path.abspath(__file__)))
if result.returncode == 0:
print("βœ… Syntax validation PASSED")
return True
else:
print("❌ Syntax validation FAILED")
print("STDERR:", result.stderr)
return False
def test_stream_protection():
"""Test SafeStream protection"""
print("\nπŸ§ͺ Testing SafeStream protection...")
test_code = '''
import sys
import os
# Test SafeStream class from app.py
exec(open("app.py").read()[:1000]) # Get SafeStream class
# Test SafeStream functionality
try:
stream = SafeStream('stdout')
stream.write("Test write operation")
stream.flush()
print("[TEST] βœ… SafeStream write/flush successful")
# Test error conditions
try:
stream.fileno()
print("[TEST] ❌ SafeStream should raise OSError for fileno()")
except OSError:
print("[TEST] βœ… SafeStream correctly raises OSError for fileno()")
print(f"[TEST] SafeStream isatty(): {stream.isatty()}")
except Exception as e:
print(f"[TEST] ❌ SafeStream test failed: {e}")
'''
result = subprocess.run([
sys.executable, '-c', test_code
], capture_output=True, text=True, cwd=os.path.dirname(os.path.abspath(__file__)))
print("STDOUT:")
print(result.stdout)
if result.stderr:
print("STDERR:")
print(result.stderr)
if result.returncode == 0 and "SafeStream write/flush successful" in result.stdout:
print("βœ… SafeStream protection test PASSED")
return True
else:
print("❌ SafeStream protection test FAILED")
return False
def main():
"""Run all logging fix tests"""
print("πŸš€ TTS Service - Comprehensive Logging Fix Validation")
print("=" * 60)
tests = [
("Syntax Validation", test_syntax_validation),
("SafeStream Protection", test_stream_protection),
("safe_log Function", test_safe_log_function),
("Import Safety", test_import_safety),
]
results = []
for test_name, test_func in tests:
print(f"\nπŸ“‹ Running {test_name}...")
try:
result = test_func()
results.append((test_name, result))
except Exception as e:
print(f"❌ {test_name} failed with exception: {e}")
results.append((test_name, False))
print("\n" + "=" * 60)
print("🎯 TEST SUMMARY")
print("=" * 60)
passed = 0
total = len(results)
for test_name, result in results:
status = "βœ… PASSED" if result else "❌ FAILED"
print(f"{test_name:.<40} {status}")
if result:
passed += 1
print("-" * 60)
print(f"Total: {passed}/{total} tests passed")
if passed == total:
print("\nπŸŽ‰ ALL TESTS PASSED - TTS logging fix is working correctly!")
return True
else:
print(f"\n⚠️ {total - passed} tests failed - review output above")
return False
if __name__ == "__main__":
success = main()
sys.exit(0 if success else 1)