|
| 1 | +""" |
| 2 | +Unit tests for exception_config module. |
| 3 | +
|
| 4 | +Tests the BackoffConfig and ExceptionHandlerConfig classes, including: |
| 5 | +1. Backoff decorator creation |
| 6 | +2. Per-exception backoff overrides |
| 7 | +3. ResponseQualityError default no-backoff configuration |
| 8 | +4. Exception grouping to avoid double backoff |
| 9 | +""" |
| 10 | + |
| 11 | +import pytest |
| 12 | +from eval_protocol.pytest.exception_config import BackoffConfig, ExceptionHandlerConfig, DEFAULT_RETRYABLE_EXCEPTIONS |
| 13 | +from eval_protocol.exceptions import ResponseQualityError |
| 14 | + |
| 15 | + |
| 16 | +def test_backoff_config_no_exceptions(): |
| 17 | + """Test that BackoffConfig returns no-op decorator when no exceptions specified.""" |
| 18 | + config = BackoffConfig() |
| 19 | + decorator = config.get_backoff_decorator(set()) |
| 20 | + |
| 21 | + # Should be a no-op decorator |
| 22 | + def test_func(): |
| 23 | + return "test" |
| 24 | + |
| 25 | + decorated = decorator(test_func) |
| 26 | + assert decorated() == "test" |
| 27 | + assert decorated is test_func # Should be the same function |
| 28 | + |
| 29 | + |
| 30 | +def test_backoff_config_no_overrides(): |
| 31 | + """Test that BackoffConfig creates a single decorator.""" |
| 32 | + config = BackoffConfig(strategy="constant", base_delay=0.1, max_tries=2) |
| 33 | + exceptions = {ConnectionError, TimeoutError} |
| 34 | + |
| 35 | + decorator = config.get_backoff_decorator(exceptions) |
| 36 | + assert decorator is not None |
| 37 | + |
| 38 | + # Decorator should be callable |
| 39 | + def test_func(): |
| 40 | + raise ConnectionError("test") |
| 41 | + |
| 42 | + decorated = decorator(test_func) |
| 43 | + assert callable(decorated) |
| 44 | + |
| 45 | + |
| 46 | +def test_exception_handler_config_default_response_quality_error(): |
| 47 | + """Test that ExceptionHandlerConfig includes ResponseQualityError by default.""" |
| 48 | + config = ExceptionHandlerConfig() |
| 49 | + |
| 50 | + # ResponseQualityError should be in retryable_exceptions |
| 51 | + assert ResponseQualityError in config.retryable_exceptions |
| 52 | + |
| 53 | + |
| 54 | +def test_exception_handler_config_get_backoff_decorator(): |
| 55 | + """Test that ExceptionHandlerConfig.get_backoff_decorator() works correctly.""" |
| 56 | + config = ExceptionHandlerConfig() |
| 57 | + decorator = config.get_backoff_decorator() |
| 58 | + |
| 59 | + assert decorator is not None |
| 60 | + assert callable(decorator) |
| 61 | + |
| 62 | + # Should be able to decorate a function |
| 63 | + def test_func(): |
| 64 | + raise ConnectionError("test") |
| 65 | + |
| 66 | + decorated = decorator(test_func) |
| 67 | + assert callable(decorated) |
| 68 | + |
| 69 | + |
| 70 | +def test_backoff_config_expo_strategy(): |
| 71 | + |
| 72 | + """Test that BackoffConfig creates expo decorator correctly.""" |
| 73 | + config = BackoffConfig(strategy="expo", base_delay=1.0, max_tries=2) |
| 74 | + exceptions = {ConnectionError} |
| 75 | + |
| 76 | + decorator = config.get_backoff_decorator(exceptions) |
| 77 | + assert decorator is not None |
| 78 | + |
| 79 | + def test_func(): |
| 80 | + raise ConnectionError("test") |
| 81 | + |
| 82 | + decorated = decorator(test_func) |
| 83 | + assert callable(decorated) |
| 84 | + |
| 85 | + |
| 86 | +def test_backoff_config_constant_strategy(): |
| 87 | + """Test that BackoffConfig creates constant decorator correctly.""" |
| 88 | + config = BackoffConfig(strategy="constant", base_delay=0.1, max_tries=2) |
| 89 | + exceptions = {ConnectionError} |
| 90 | + |
| 91 | + decorator = config.get_backoff_decorator(exceptions) |
| 92 | + assert decorator is not None |
| 93 | + |
| 94 | + def test_func(): |
| 95 | + raise ConnectionError("test") |
| 96 | + |
| 97 | + decorated = decorator(test_func) |
| 98 | + assert callable(decorated) |
| 99 | + |
| 100 | + |
| 101 | +def test_backoff_config_invalid_strategy(): |
| 102 | + """Test that BackoffConfig raises ValueError for invalid strategy.""" |
| 103 | + config = BackoffConfig(strategy="invalid", base_delay=1.0, max_tries=2) |
| 104 | + exceptions = {ConnectionError} |
| 105 | + |
| 106 | + with pytest.raises(ValueError, match="Unknown backoff strategy"): |
| 107 | + config.get_backoff_decorator(exceptions) |
| 108 | + |
| 109 | + |
| 110 | +def test_exception_handler_config_response_quality_error_in_defaults(): |
| 111 | + """Test that ResponseQualityError is in DEFAULT_RETRYABLE_EXCEPTIONS.""" |
| 112 | + assert ResponseQualityError in DEFAULT_RETRYABLE_EXCEPTIONS |
| 113 | + |
| 114 | + |
0 commit comments