18
18
}
19
19
20
20
21
+ class MockCircuitBreaker (CircuitBreaker ):
22
+ """
23
+ A circuit breaker with extra methods useful for mocking state.
24
+
25
+ To understand the methods below, it helps to understand the `RedisSlidingWindowRateLimiter`
26
+ which powers the circuit breaker. Details can be found in
27
+ https://github.com/getsentry/sentry-redis-tools/blob/d4f3dc883b1137d82b6b7a92f4b5b41991c1fc8a/sentry_redis_tools/sliding_windows_rate_limiter.py,
28
+ (which is the implementation behind the rate limiter) but TL;DR, quota usage during the time
29
+ window is tallied in buckets ("granules"), and as time passes the window slides forward one
30
+ granule at a time. To be able to mimic this, most of the methods here operate at the granule
31
+ level.
32
+ """
33
+
34
+
21
35
@freeze_time ()
22
36
class CircuitBreakerTest (TestCase ):
23
37
def setUp (self ) -> None :
24
38
self .config = DEFAULT_CONFIG
25
- self .breaker = CircuitBreaker ("dogs_are_great" , self .config )
39
+ self .breaker = MockCircuitBreaker ("dogs_are_great" , self .config )
26
40
27
41
# Clear all existing keys from redis
28
42
self .breaker .redis_pipeline .flushall ()
@@ -78,7 +92,7 @@ def test_fixes_too_loose_recovery_limit(self, mock_logger: MagicMock):
78
92
(False , mock_logger .warning ),
79
93
]:
80
94
settings .DEBUG = settings_debug_value
81
- breaker = CircuitBreaker ("dogs_are_great" , config )
95
+ breaker = MockCircuitBreaker ("dogs_are_great" , config )
82
96
83
97
expected_log_function .assert_called_with (
84
98
"Circuit breaker '%s' has a recovery error limit (%d) greater than or equal"
@@ -104,7 +118,7 @@ def test_fixes_mismatched_state_durations(self, mock_logger: MagicMock):
104
118
(False , mock_logger .warning ),
105
119
]:
106
120
settings .DEBUG = settings_debug_value
107
- breaker = CircuitBreaker ("dogs_are_great" , config )
121
+ breaker = MockCircuitBreaker ("dogs_are_great" , config )
108
122
109
123
expected_log_function .assert_called_with (
110
124
"Circuit breaker '%s' has BROKEN and RECOVERY state durations (%d and %d sec, respectively)"
0 commit comments