Skip to content

Commit 0ee6bb9

Browse files
committed
add MockCircuitBreaker test class
1 parent af87f7f commit 0ee6bb9

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

tests/sentry/utils/test_circuit_breaker2.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,25 @@
1818
}
1919

2020

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+
2135
@freeze_time()
2236
class CircuitBreakerTest(TestCase):
2337
def setUp(self) -> None:
2438
self.config = DEFAULT_CONFIG
25-
self.breaker = CircuitBreaker("dogs_are_great", self.config)
39+
self.breaker = MockCircuitBreaker("dogs_are_great", self.config)
2640

2741
# Clear all existing keys from redis
2842
self.breaker.redis_pipeline.flushall()
@@ -78,7 +92,7 @@ def test_fixes_too_loose_recovery_limit(self, mock_logger: MagicMock):
7892
(False, mock_logger.warning),
7993
]:
8094
settings.DEBUG = settings_debug_value
81-
breaker = CircuitBreaker("dogs_are_great", config)
95+
breaker = MockCircuitBreaker("dogs_are_great", config)
8296

8397
expected_log_function.assert_called_with(
8498
"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):
104118
(False, mock_logger.warning),
105119
]:
106120
settings.DEBUG = settings_debug_value
107-
breaker = CircuitBreaker("dogs_are_great", config)
121+
breaker = MockCircuitBreaker("dogs_are_great", config)
108122

109123
expected_log_function.assert_called_with(
110124
"Circuit breaker '%s' has BROKEN and RECOVERY state durations (%d and %d sec, respectively)"

0 commit comments

Comments
 (0)