-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from migibert/feature/registry
Add a registry to easily retrieve overall circuits state
- Loading branch information
Showing
5 changed files
with
213 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
from .pycircuitbreaker import circuit, CircuitBreaker | ||
from .exceptions import CircuitBreakerException | ||
from .pycircuitbreaker import circuit, CircuitBreaker, CircuitBreakerRegistry | ||
from .exceptions import CircuitBreakerException, CircuitBreakerRegistryException | ||
from .state import CircuitBreakerState | ||
from .strategies import CircuitBreakerStrategy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
from pycircuitbreaker import ( | ||
CircuitBreaker, | ||
CircuitBreakerRegistry, | ||
CircuitBreakerRegistryException, | ||
) | ||
|
||
import pytest | ||
|
||
|
||
def test_register_new_circuit_breaker(): | ||
registry = CircuitBreakerRegistry() | ||
circuit_breaker = CircuitBreaker() | ||
|
||
registry.register(circuit_breaker) | ||
|
||
registered = registry.get_circuits() | ||
assert len(registered) == 1 | ||
assert registered[0] == circuit_breaker | ||
|
||
|
||
def test_register_existing_circuit_breaker(): | ||
registry = CircuitBreakerRegistry() | ||
circuit_breaker = CircuitBreaker() | ||
|
||
registry.register(circuit_breaker) | ||
|
||
with pytest.raises(CircuitBreakerRegistryException): | ||
registry.register(circuit_breaker) | ||
|
||
registered = registry.get_circuits() | ||
assert len(registered) == 1 | ||
assert registered[0] == circuit_breaker | ||
|
||
|
||
def test_get_circuits_not_empty(): | ||
registry = CircuitBreakerRegistry() | ||
db_circuit = CircuitBreaker(breaker_id="db") | ||
service_circuit = CircuitBreaker(breaker_id="service") | ||
|
||
registry.register(db_circuit) | ||
registry.register(service_circuit) | ||
|
||
registered = registry.get_circuits() | ||
assert len(registered) == 2 | ||
assert db_circuit in registered | ||
assert service_circuit in registered | ||
|
||
|
||
def test_get_circuits_empty(): | ||
registry = CircuitBreakerRegistry() | ||
|
||
registered = registry.get_circuits() | ||
|
||
assert len(registered) == 0 | ||
|
||
|
||
def test_get_open_circuits_empty(): | ||
registry = CircuitBreakerRegistry() | ||
|
||
opened = registry.get_open_circuits() | ||
|
||
assert len(opened) == 0 | ||
|
||
|
||
def test_get_open_circuits_when_all_circuits_are_closed(): | ||
registry = CircuitBreakerRegistry() | ||
circuit = CircuitBreaker(breaker_id="closed_breaker", error_threshold=1) | ||
registry.register(circuit) | ||
|
||
opened = registry.get_open_circuits() | ||
|
||
assert len(opened) == 0 | ||
|
||
|
||
def test_get_open_circuits_when_one_circuit_is_open(error_func): | ||
registry = CircuitBreakerRegistry() | ||
open_circuit = CircuitBreaker(breaker_id="open_breaker", error_threshold=1) | ||
closed_circuit = CircuitBreaker(breaker_id="closed_breaker") | ||
registry.register(open_circuit) | ||
registry.register(closed_circuit) | ||
|
||
with pytest.raises(IOError): | ||
open_circuit.call(error_func) | ||
|
||
opened = registry.get_open_circuits() | ||
|
||
assert len(opened) == 1 | ||
assert open_circuit in opened | ||
assert closed_circuit not in opened |