File size: 8,323 Bytes
a966d36 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 | """
Tests for ErrorHandler utility module
"""
import pytest
import asyncio
from unittest.mock import Mock, patch
from backend.mcp.orchestrator.error_handler import ErrorHandler, CircuitBreaker
class TestErrorHandler:
"""Test ErrorHandler class"""
def test_handle_llm_error_basic(self):
"""Test basic error handling with localized fallback"""
exception = Exception("Test error")
agent_name = "coach"
provider = Mock()
provider.model = "test-model"
result = ErrorHandler.handle_llm_error(exception, agent_name, provider, locale='cs')
assert isinstance(result, str)
assert len(result) > 0 # Should return fallback message
def test_handle_llm_error_with_none_provider(self):
"""Test error handling when provider is None"""
exception = Exception("Test error")
agent_name = "planner"
result = ErrorHandler.handle_llm_error(exception, agent_name, None, locale='cs')
assert isinstance(result, str)
assert len(result) > 0
def test_handle_llm_error_different_locales(self):
"""Test error handling with different locales"""
exception = Exception("Test error")
agent_name = "guardian"
provider = Mock()
result_cs = ErrorHandler.handle_llm_error(exception, agent_name, provider, locale='cs')
result_en = ErrorHandler.handle_llm_error(exception, agent_name, provider, locale='en')
assert isinstance(result_cs, str)
assert isinstance(result_en, str)
# Both should return valid strings (may be same fallback)
assert len(result_cs) > 0
assert len(result_en) > 0
@patch.dict('os.environ', {'VERBOSE_STREAM': '1'})
def test_log_error_verbose_enabled(self, capsys):
"""Test verbose error logging when enabled"""
exception = ValueError("Test verbose error")
context = {'agent': 'coach', 'provider': 'test-provider', 'operation': 'test_op'}
ErrorHandler.log_error_verbose(exception, context)
captured = capsys.readouterr()
assert 'ERROR DETAILS' in captured.out
assert 'AGENT: coach' in captured.out
assert 'PROVIDER: test-provider' in captured.out
@patch.dict('os.environ', {'VERBOSE_STREAM': '0'})
def test_log_error_verbose_disabled(self, capsys):
"""Test verbose error logging when disabled"""
exception = ValueError("Test error")
context = {'agent': 'coach'}
ErrorHandler.log_error_verbose(exception, context)
captured = capsys.readouterr()
assert captured.out == '' # No output when disabled
@pytest.mark.asyncio
async def test_retry_on_failure_success_first_try(self):
"""Test retry decorator with successful first attempt"""
call_count = 0
@ErrorHandler.retry_on_failure(max_retries=3)
async def successful_function():
nonlocal call_count
call_count += 1
return "success"
result = await successful_function()
assert result == "success"
assert call_count == 1 # Should succeed on first try
@pytest.mark.asyncio
async def test_retry_on_failure_success_after_retries(self):
"""Test retry decorator succeeding after failures"""
call_count = 0
@ErrorHandler.retry_on_failure(max_retries=3, delay=0.01)
async def flaky_function():
nonlocal call_count
call_count += 1
if call_count < 3:
raise ValueError("Temporary failure")
return "success"
result = await flaky_function()
assert result == "success"
assert call_count == 3 # Should succeed on third try
@pytest.mark.asyncio
async def test_retry_on_failure_exhausted(self):
"""Test retry decorator when all retries exhausted"""
call_count = 0
@ErrorHandler.retry_on_failure(max_retries=2, delay=0.01)
async def always_failing_function():
nonlocal call_count
call_count += 1
raise ValueError("Always fails")
with pytest.raises(ValueError, match="Always fails"):
await always_failing_function()
assert call_count == 3 # Initial + 2 retries
@pytest.mark.asyncio
async def test_retry_on_failure_exponential_backoff(self):
"""Test retry decorator uses exponential backoff"""
call_times = []
@ErrorHandler.retry_on_failure(max_retries=2, delay=0.1, backoff=2.0)
async def timing_function():
import time
call_times.append(time.time())
if len(call_times) < 3:
raise ValueError("Fail")
return "success"
await timing_function()
# Check that delays increase exponentially
assert len(call_times) == 3
delay1 = call_times[1] - call_times[0]
delay2 = call_times[2] - call_times[1]
# Second delay should be roughly 2x first delay (allowing for timing variance)
assert delay2 > delay1 * 1.5
class TestCircuitBreaker:
"""Test CircuitBreaker class"""
def test_circuit_breaker_starts_closed(self):
"""Test circuit breaker starts in closed state"""
breaker = CircuitBreaker(failure_threshold=3, timeout=1.0)
assert breaker.state == "closed"
assert not breaker.is_open()
def test_circuit_breaker_opens_after_threshold(self):
"""Test circuit opens after failure threshold"""
breaker = CircuitBreaker(failure_threshold=3, timeout=1.0)
# Record failures
breaker.record_failure()
assert not breaker.is_open() # Still closed
breaker.record_failure()
assert not breaker.is_open() # Still closed
breaker.record_failure()
assert breaker.is_open() # Should open now
def test_circuit_breaker_closes_after_success(self):
"""Test circuit closes after successful call in half-open state"""
breaker = CircuitBreaker(failure_threshold=2, timeout=0.1)
# Open the circuit
breaker.record_failure()
breaker.record_failure()
assert breaker.is_open()
# Wait for timeout
import time
time.sleep(0.15)
# Should transition to half-open
assert not breaker.is_open()
# Record success - should close
breaker.record_success()
assert breaker.state == "closed"
@pytest.mark.asyncio
async def test_circuit_breaker_decorator_blocks_when_open(self):
"""Test circuit breaker decorator blocks calls when open"""
breaker = CircuitBreaker(failure_threshold=2, timeout=1.0, name="test")
@breaker
async def test_function():
raise ValueError("Test error")
# Trigger failures to open circuit
with pytest.raises(ValueError):
await test_function()
with pytest.raises(ValueError):
await test_function()
assert breaker.is_open()
# Next call should be blocked
with pytest.raises(Exception, match="Circuit breaker test is open"):
await test_function()
@pytest.mark.asyncio
async def test_circuit_breaker_decorator_allows_after_timeout(self):
"""Test circuit breaker allows calls after timeout"""
breaker = CircuitBreaker(failure_threshold=2, timeout=0.1, name="test")
call_count = 0
@breaker
async def test_function():
nonlocal call_count
call_count += 1
if call_count < 3:
raise ValueError("Fail")
return "success"
# Open circuit
with pytest.raises(ValueError):
await test_function()
with pytest.raises(ValueError):
await test_function()
assert breaker.is_open()
# Wait for timeout
import time
time.sleep(0.15)
# Should allow call now
result = await test_function()
assert result == "success"
assert breaker.state == "closed"
|