def test_success_result(self, rule_should_not_open):
     with CircuitBreaker(
             rule=rule_should_not_open,
             cache=cache,
             failure_exception=None,
             catch_exceptions=None,
     ):
         success_function()
Beispiel #2
0
 def test_success_result(self):
     with CircuitBreaker(
         cache=cache,
         failure_cache_key='success',
         max_failures=1,
         max_failure_exception=None,
         catch_exceptions=None,
     ):
         success_function()
 def test_should_raise_error(self, rule_should_open):
     with pytest.raises(MyException):
         with CircuitBreaker(
                 rule=rule_should_open,
                 cache=cache,
                 failure_exception=MyException,
                 catch_exceptions=(ValueError, ),
         ):
             fail_function()
Beispiel #4
0
 def test_should_raise_error_when_max_failures_is_exceeded(self):
     with pytest.raises(MyException):
         with CircuitBreaker(
             cache=cache,
             failure_cache_key='fail',
             max_failures=0,
             max_failure_exception=MyException,
             catch_exceptions=(ValueError,),
         ):
             fail_function()
    def test_should_increase_request_cache_count(self, request_cache_key,
                                                 rule_should_not_open):
        cache.set(request_cache_key, 0)

        with CircuitBreaker(
                rule=rule_should_not_open,
                cache=cache,
                failure_exception=MyException,
                catch_exceptions=(ValueError, ),
        ):
            success_function()

        with pytest.raises(ValueError):
            with CircuitBreaker(
                    rule=rule_should_not_open,
                    cache=cache,
                    failure_exception=MyException,
                    catch_exceptions=(ValueError, ),
            ):
                fail_function()

        assert cache.get(request_cache_key) == 2
    def test_should_not_increment_failure_when_rule_is_false(
            self, rule_should_not_increase_failure, failure_cache_key):
        cache.set(failure_cache_key, 5)

        with pytest.raises(ValueError):
            with CircuitBreaker(
                    rule=rule_should_not_increase_failure,
                    cache=cache,
                    failure_exception=MyException,
                    catch_exceptions=(ValueError, ),
            ):
                fail_function()

        assert cache.get(failure_cache_key) == 5
Beispiel #7
0
    def test_should_raise_exception_when_circuit_is_open(self):
        cache.set('circuit_circuit_open', True)

        with pytest.raises(MyException):
            with CircuitBreaker(
                cache=cache,
                failure_cache_key='circuit_open',
                max_failures=10,
                max_failure_exception=MyException,
                catch_exceptions=(ValueError,),
            ) as circuit_breaker:
                success_function()

            assert circuit_breaker.is_circuit_open
    def test_should_raise_exception_when_circuit_is_open(
            self, rule_should_open, failure_cache_key):
        circuit_cache_key = 'circuit_{}'.format(failure_cache_key)
        cache.set(circuit_cache_key, True)

        with pytest.raises(MyException):
            with CircuitBreaker(
                    rule=rule_should_open,
                    cache=cache,
                    failure_exception=MyException,
                    catch_exceptions=(ValueError, ),
            ) as circuit_breaker:
                success_function()

            assert circuit_breaker.is_circuit_open
Beispiel #9
0
    def test_should_increase_fail_cache_count(self):
        failure_cache_key = 'fail_count'

        cache.set(failure_cache_key, 171)

        with pytest.raises(ValueError):
            with CircuitBreaker(
                cache=cache,
                failure_cache_key=failure_cache_key,
                max_failures=5000,
                max_failure_exception=MyException,
                catch_exceptions=(ValueError,),
            ):
                fail_function()

        assert cache.get(failure_cache_key, 172)
    def test_should_create_failure_cache_when_increase_request_count(
        self,
        rule_should_not_open,
        failure_cache_key,
        request_cache_key,
    ):
        assert cache.get(failure_cache_key) is None

        with CircuitBreaker(
                rule=rule_should_not_open,
                cache=cache,
                failure_exception=MyException,
                catch_exceptions=(ValueError, ),
        ):
            success_function()

        assert cache.get(failure_cache_key) == 0
    def test_should_open_circuit_when_failures_exceeds(
        self,
        rule_should_open,
        failure_cache_key,
    ):
        cache.set(failure_cache_key, 3)

        with pytest.raises(MyException):
            with CircuitBreaker(
                    rule=rule_should_open,
                    cache=cache,
                    failure_exception=MyException,
                    catch_exceptions=(ValueError, ),
            ) as circuit_breaker:
                fail_function()

            assert circuit_breaker.is_circuit_open
    def test_should_delete_count_key_when_circuit_is_open(
            self, rule_should_open, failure_cache_key, request_cache_key):
        cache.set(failure_cache_key, 2)
        cache.set(request_cache_key, 5)

        with pytest.raises(MyException):
            with CircuitBreaker(
                    rule=rule_should_open,
                    cache=cache,
                    failure_exception=MyException,
                    catch_exceptions=(ValueError, ),
            ) as circuit_breaker:
                fail_function()

            assert circuit_breaker.is_circuit_open

        assert cache.get(failure_cache_key) is None
        assert cache.get(request_cache_key) is None
    def test_should_not_increment_fail_when_circuit_is_open(
            self, rule_should_open, failure_cache_key):
        """
        It should not increment fail count over the max failures limit, when
        circuit breaker is open after a successful enter.
        """
        cache.set(failure_cache_key, 3)

        with pytest.raises(MyException):
            with CircuitBreaker(
                    rule=rule_should_open,
                    cache=cache,
                    failure_exception=MyException,
                    catch_exceptions=(ValueError, ),
            ):
                fail_function()

        assert not cache.get(failure_cache_key)
    def test_should_not_call_exit_when_circuit_is_open(self, rule_should_open,
                                                       failure_cache_key):
        circuit_cache_key = 'circuit_{}'.format(failure_cache_key)
        cache.set(circuit_cache_key, True)

        with pytest.raises(MyException):
            with mock.patch(
                    'django_toolkit.fallbacks.circuit_breaker.CircuitBreaker.__exit__'  # noqa
            ) as exit:
                with CircuitBreaker(
                        rule=rule_should_open,
                        cache=cache,
                        failure_exception=MyException,
                        catch_exceptions=(ValueError, ),
                ):
                    success_function()

        assert not exit.called
Beispiel #15
0
    def test_should_open_circuit_when_max_failures_exceeds(self):
        failure_cache_key = 'circuit'

        cache.set(failure_cache_key, 1)

        with pytest.raises(MyException):
            with CircuitBreaker(
                cache=cache,
                failure_cache_key=failure_cache_key,
                max_failures=2,
                max_failure_exception=MyException,
                catch_exceptions=(ValueError,),
            ) as circuit_breaker:
                fail_function()

            assert circuit_breaker.is_circuit_open

        assert cache.get(failure_cache_key, 2)
Beispiel #16
0
    def test_should_not_increment_fail_when_circuit_is_open(self):
        """
        It should not increment fail count over the max failures limit, when
        circuit breaker is open after a successful enter.
        """
        failure_cache_key = 'fail_count'
        max_failures = 10

        with pytest.raises(MyException):
            with CircuitBreaker(
                cache=cache,
                failure_cache_key=failure_cache_key,
                max_failures=max_failures,
                max_failure_exception=MyException,
                catch_exceptions=(ValueError,),
            ) as circuit_breaker:
                cache.set(failure_cache_key, max_failures)
                circuit_breaker.open_circuit()

                fail_function()

        assert not cache.get(failure_cache_key)