Files
qbitcheck/test_retry_logic.py
2025-11-10 06:56:40 -08:00

64 lines
2.1 KiB
Python

#!/usr/bin/env python3
"""
Test script to verify the retry logic implementation in ConnectionMonitor
"""
import sys
import os
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from checker import ConnectionMonitor
import logging
import time
# Configure logging for testing
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s - %(levelname)s: %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)
def test_retry_logic():
"""Test the retry functionality with a mock failing URL"""
print("Testing retry logic implementation...")
# Create a monitor instance with a URL that will fail
monitor = ConnectionMonitor(
qbittorrent_url='http://nonexistent-host-that-will-fail:8080',
nomad_url='http://127.0.0.1:4646',
tracker_name='test-tracker',
consul_url='http://127.0.0.1:8500'
)
# Test the retry configuration
print(f"Retry attempts: {monitor.api_retry_attempts}")
print(f"Initial delay: {monitor.api_retry_delay} seconds")
print(f"Backoff multiplier: {monitor.api_retry_backoff}")
# Test the get_connection_status method
print("\nTesting get_connection_status with retry logic...")
start_time = time.time()
result = monitor.get_connection_status()
end_time = time.time()
elapsed_time = end_time - start_time
print(f"Method completed in {elapsed_time:.2f} seconds")
print(f"Result: {result}")
# Verify that the retry logic was triggered
# The method should take at least (2 + 4 + 8) = 14 seconds for 3 attempts with exponential backoff
expected_min_time = monitor.api_retry_delay * (1 + monitor.api_retry_backoff + monitor.api_retry_backoff**2)
print(f"Expected minimum time for retries: {expected_min_time:.2f} seconds")
print(f"Actual elapsed time: {elapsed_time:.2f} seconds")
if elapsed_time >= expected_min_time:
print("✅ Retry logic working correctly - delays were applied")
else:
print("❌ Retry logic may not be working - delays not detected")
return True
if __name__ == '__main__':
test_retry_logic()