| Conditions | 33 |
| Total Lines | 72 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like test_circuitbreaker_reopens_after_successful_call() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | from time import sleep |
||
| 146 | @patch('test_functional.pseudo_remote_call', return_value=True) |
||
| 147 | def test_circuitbreaker_reopens_after_successful_call(mock_remote: Mock): |
||
| 148 | circuitbreaker = CircuitBreakerMonitor.get('threshold_2') |
||
| 149 | |||
| 150 | assert str(circuitbreaker) == 'threshold_2' |
||
| 151 | |||
| 152 | # initial state: closed |
||
| 153 | assert circuitbreaker.closed |
||
| 154 | assert circuitbreaker.state == STATE_CLOSED |
||
| 155 | assert circuitbreaker.failure_count == 0 |
||
| 156 | |||
| 157 | # successful call -> no exception |
||
| 158 | assert circuit_threshold_2_timeout_1() |
||
| 159 | |||
| 160 | # from now all subsequent calls will fail |
||
| 161 | mock_remote.side_effect = ConnectionError('Connection refused') |
||
| 162 | |||
| 163 | # 1. failed call -> original exception |
||
| 164 | with raises(ConnectionError): |
||
| 165 | circuit_threshold_2_timeout_1() |
||
| 166 | assert circuitbreaker.closed |
||
| 167 | assert circuitbreaker.failure_count == 1 |
||
| 168 | |||
| 169 | # 2. failed call -> original exception |
||
| 170 | with raises(ConnectionError): |
||
| 171 | circuit_threshold_2_timeout_1() |
||
| 172 | |||
| 173 | # Circuit breaker opens, threshold has been reached |
||
| 174 | assert circuitbreaker.opened |
||
| 175 | assert circuitbreaker.state == STATE_OPEN |
||
| 176 | assert circuitbreaker.failure_count == 2 |
||
| 177 | assert 0 < circuitbreaker.open_remaining <= 1 |
||
| 178 | |||
| 179 | # 4. failed call -> not passed to function -> CircuitBreakerError |
||
| 180 | with raises(CircuitBreakerError): |
||
| 181 | circuit_threshold_2_timeout_1() |
||
| 182 | assert circuitbreaker.opened |
||
| 183 | assert circuitbreaker.failure_count == 2 |
||
| 184 | assert 0 < circuitbreaker.open_remaining <= 1 |
||
| 185 | |||
| 186 | # from now all subsequent calls will succeed |
||
| 187 | mock_remote.side_effect = None |
||
| 188 | |||
| 189 | # but recover timeout has not been reached -> still open |
||
| 190 | # 5. failed call -> not passed to function -> CircuitBreakerError |
||
| 191 | with raises(CircuitBreakerError): |
||
| 192 | circuit_threshold_2_timeout_1() |
||
| 193 | assert circuitbreaker.opened |
||
| 194 | assert circuitbreaker.failure_count == 2 |
||
| 195 | assert 0 < circuitbreaker.open_remaining <= 1 |
||
| 196 | |||
| 197 | # wait for 1 second (recover timeout) |
||
| 198 | sleep(1) |
||
| 199 | |||
| 200 | # circuit half-open -> next call will be passed through |
||
| 201 | assert circuitbreaker.closed |
||
| 202 | assert circuitbreaker.failure_count == 2 |
||
| 203 | assert circuitbreaker.open_remaining < 0 |
||
| 204 | assert circuitbreaker.state == STATE_HALF_OPEN |
||
| 205 | |||
| 206 | # successful call |
||
| 207 | assert circuit_threshold_2_timeout_1() |
||
| 208 | |||
| 209 | # circuit closed and reset'ed |
||
| 210 | assert circuitbreaker.closed |
||
| 211 | assert circuitbreaker.state == STATE_CLOSED |
||
| 212 | assert circuitbreaker.failure_count == 0 |
||
| 213 | |||
| 214 | # some another successful calls |
||
| 215 | assert circuit_threshold_2_timeout_1() |
||
| 216 | assert circuit_threshold_2_timeout_1() |
||
| 217 | assert circuit_threshold_2_timeout_1() |
||
| 218 | |||
| 238 |