| Conditions | 31 |
| Total Lines | 68 |
| 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_recover_half_open() 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 |
||
| 76 | @patch('test_functional.pseudo_remote_call', return_value=True) |
||
| 77 | def test_circuitbreaker_recover_half_open(mock_remote: Mock): |
||
| 78 | circuitbreaker = CircuitBreakerMonitor.get('threshold_3') |
||
| 79 | |||
| 80 | # initial state: closed |
||
| 81 | assert circuitbreaker.closed |
||
| 82 | assert circuitbreaker.state == STATE_CLOSED |
||
| 83 | |||
| 84 | # no exception -> success |
||
| 85 | assert circuit_threshold_3_timeout_1() |
||
| 86 | |||
| 87 | # from now all subsequent calls will fail |
||
| 88 | mock_remote.side_effect = ConnectionError('Connection refused') |
||
| 89 | |||
| 90 | # 1. failed call -> original exception |
||
| 91 | with raises(ConnectionError): |
||
| 92 | circuit_threshold_3_timeout_1() |
||
| 93 | assert circuitbreaker.closed |
||
| 94 | assert circuitbreaker.failure_count == 1 |
||
| 95 | |||
| 96 | # 2. failed call -> original exception |
||
| 97 | with raises(ConnectionError): |
||
| 98 | circuit_threshold_3_timeout_1() |
||
| 99 | assert circuitbreaker.closed |
||
| 100 | assert circuitbreaker.failure_count == 2 |
||
| 101 | |||
| 102 | # 3. failed call -> original exception |
||
| 103 | with raises(ConnectionError): |
||
| 104 | circuit_threshold_3_timeout_1() |
||
| 105 | |||
| 106 | # Circuit breaker opens, threshold has been reached |
||
| 107 | assert circuitbreaker.opened |
||
| 108 | assert circuitbreaker.state == STATE_OPEN |
||
| 109 | assert circuitbreaker.failure_count == 3 |
||
| 110 | assert 0 < circuitbreaker.open_remaining <= 1 |
||
| 111 | |||
| 112 | # 4. failed call -> not passed to function -> CircuitBreakerError |
||
| 113 | with raises(CircuitBreakerError): |
||
| 114 | circuit_threshold_3_timeout_1() |
||
| 115 | assert circuitbreaker.opened |
||
| 116 | assert circuitbreaker.failure_count == 3 |
||
| 117 | assert 0 < circuitbreaker.open_remaining <= 1 |
||
| 118 | |||
| 119 | # 5. failed call -> not passed to function -> CircuitBreakerError |
||
| 120 | with raises(CircuitBreakerError): |
||
| 121 | circuit_threshold_3_timeout_1() |
||
| 122 | assert circuitbreaker.opened |
||
| 123 | assert circuitbreaker.failure_count == 3 |
||
| 124 | assert 0 < circuitbreaker.open_remaining <= 1 |
||
| 125 | |||
| 126 | # wait for 1 second (recover timeout) |
||
| 127 | sleep(1) |
||
| 128 | |||
| 129 | # circuit half-open -> next call will be passed through |
||
| 130 | assert circuitbreaker.closed |
||
| 131 | assert circuitbreaker.open_remaining < 0 |
||
| 132 | assert circuitbreaker.state == STATE_HALF_OPEN |
||
| 133 | |||
| 134 | # State half-open -> function is executed -> original exception |
||
| 135 | with raises(ConnectionError): |
||
| 136 | circuit_threshold_3_timeout_1() |
||
| 137 | assert circuitbreaker.opened |
||
| 138 | assert circuitbreaker.failure_count == 4 |
||
| 139 | assert 0 < circuitbreaker.open_remaining <= 1 |
||
| 140 | |||
| 141 | # State open > not passed to function -> CircuitBreakerError |
||
| 142 | with raises(CircuitBreakerError): |
||
| 143 | circuit_threshold_3_timeout_1() |
||
| 144 | |||
| 238 |