-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathtest_exception_config.py
More file actions
114 lines (77 loc) · 3.61 KB
/
test_exception_config.py
File metadata and controls
114 lines (77 loc) · 3.61 KB
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
"""
Unit tests for exception_config module.
Tests the BackoffConfig and ExceptionHandlerConfig classes, including:
1. Backoff decorator creation
2. Per-exception backoff overrides
3. ResponseQualityError default no-backoff configuration
4. Exception grouping to avoid double backoff
"""
import pytest
from eval_protocol.pytest.exception_config import BackoffConfig, ExceptionHandlerConfig, DEFAULT_RETRYABLE_EXCEPTIONS
from eval_protocol.exceptions import ResponseQualityError
def test_backoff_config_no_exceptions():
"""Test that BackoffConfig returns no-op decorator when no exceptions specified."""
config = BackoffConfig()
decorator = config.get_backoff_decorator(set())
# Should be a no-op decorator
def test_func():
return "test"
decorated = decorator(test_func)
assert decorated() == "test"
assert decorated is test_func # Should be the same function
def test_backoff_config_no_overrides():
"""Test that BackoffConfig creates a single decorator."""
config = BackoffConfig(strategy="constant", base_delay=0.1, max_tries=2)
exceptions = {ConnectionError, TimeoutError}
decorator = config.get_backoff_decorator(exceptions)
assert decorator is not None
# Decorator should be callable
def test_func():
raise ConnectionError("test")
decorated = decorator(test_func)
assert callable(decorated)
def test_exception_handler_config_default_response_quality_error():
"""Test that ExceptionHandlerConfig includes ResponseQualityError by default."""
config = ExceptionHandlerConfig()
# ResponseQualityError should be in retryable_exceptions
assert ResponseQualityError in config.retryable_exceptions
def test_exception_handler_config_get_backoff_decorator():
"""Test that ExceptionHandlerConfig.get_backoff_decorator() works correctly."""
config = ExceptionHandlerConfig()
decorator = config.get_backoff_decorator()
assert decorator is not None
assert callable(decorator)
# Should be able to decorate a function
def test_func():
raise ConnectionError("test")
decorated = decorator(test_func)
assert callable(decorated)
def test_backoff_config_expo_strategy():
"""Test that BackoffConfig creates expo decorator correctly."""
config = BackoffConfig(strategy="expo", base_delay=1.0, max_tries=2)
exceptions = {ConnectionError}
decorator = config.get_backoff_decorator(exceptions)
assert decorator is not None
def test_func():
raise ConnectionError("test")
decorated = decorator(test_func)
assert callable(decorated)
def test_backoff_config_constant_strategy():
"""Test that BackoffConfig creates constant decorator correctly."""
config = BackoffConfig(strategy="constant", base_delay=0.1, max_tries=2)
exceptions = {ConnectionError}
decorator = config.get_backoff_decorator(exceptions)
assert decorator is not None
def test_func():
raise ConnectionError("test")
decorated = decorator(test_func)
assert callable(decorated)
def test_backoff_config_invalid_strategy():
"""Test that BackoffConfig raises ValueError for invalid strategy."""
config = BackoffConfig(strategy="invalid", base_delay=1.0, max_tries=2)
exceptions = {ConnectionError}
with pytest.raises(ValueError, match="Unknown backoff strategy"):
config.get_backoff_decorator(exceptions)
def test_exception_handler_config_response_quality_error_in_defaults():
"""Test that ResponseQualityError is in DEFAULT_RETRYABLE_EXCEPTIONS."""
assert ResponseQualityError in DEFAULT_RETRYABLE_EXCEPTIONS