Ejemplo n.º 1
0
        private static void ClearRepositoryKeys(string key)
        {
            var repository          = ServiceProviderFactory.ServiceProvider.GetService <IDistributedCircuitBreakerRepository>();
            CircuitBreakerKeys keys = new CircuitBreakerKeys(key);

            repository.Remove(keys.FailureCountKey);
            repository.Remove(keys.SuccessCountKey);
            repository.Remove(keys.StateKey);
        }
Ejemplo n.º 2
0
        public void StateCountKey_ShouldReturnKeyWithSuffixWhenKeyIsNotNull(string value, string expected)
        {
            //arrange
            var cbkeys = new CircuitBreakerKeys(value);

            //act
            //assert
            Assert.Equal(expected, cbkeys.StateKey);
        }
Ejemplo n.º 3
0
        public void StateCountKey_ShouldThrowExceptionWhenKeyIsNull(string value)
        {
            //arrange
            var cbkeys = new CircuitBreakerKeys(value);
            //act
            Func <string> act = new Func <string>(() => cbkeys.StateKey);

            //assert
            Assert.Throws <Exception>(act);
        }
        public void ExecuteAction_ShouldGenerateClearHealthCount()
        {
            //arrange
            string key     = "testKey";
            var    service = ServiceProviderFactory.ServiceProvider.GetService <IHealthCountService>();
            var    keys    = new CircuitBreakerKeys(key);

            //act
            service.IncrementFailure(keys);

            var repo = ServiceProviderFactory.ServiceProvider.GetService <IDistributedCircuitBreakerRepository>();

            var fail = repo.GetString(keys.FailureCountKey);
            //Assert
        }
Ejemplo n.º 5
0
        public async void ExecuteAction_ShouldPropagateExceptionWhenAsyncFunctionThrowsExceptionAsync()
        {
            //Arrange
            string key = "testKey";
            int    numberOfFailuresThreshold = 2;
            var    circuitBreakerFactory     = ServiceProviderFactory.ServiceProvider.GetService <ICircuitBreakerFactory>();
            var    repository = ServiceProviderFactory.ServiceProvider.GetService <IDistributedCircuitBreakerRepository>();
            Dictionary <string, string> dic = new Dictionary <string, string>();

            ServiceProviderFactory.SetRepositoryBehavior(key, repository, dic);

            bool exceptionExpectedWasThrown = false;

            try
            {
                var cb = circuitBreakerFactory.Create(key, numberOfFailuresThreshold);

                var funcTimeout = new Func <Task <int> >(async() =>
                {
                    await Task.Delay(1000);
                    throw new TimeoutException();
                });

                var i = await cb.ExecuteActionAsync(() => funcTimeout());
            }
            catch (TimeoutException)
            {
                exceptionExpectedWasThrown = true;
            }
            catch (BrokenCircuitException ex)
            {
            }
            Assert.True(exceptionExpectedWasThrown);

            var keyChain = new CircuitBreakerKeys(key);

            string failuresCount = repository.GetString(keyChain.FailureCountKey);
            string successCount  = repository.GetString(keyChain.SuccessCountKey);

            Assert.True(failuresCount == "1");
            Assert.True(successCount == "0" || string.IsNullOrEmpty(successCount));
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Initializes an instance of CircuitBreaker
 /// </summary>
 /// <param name="key">The Key that defines a unique unit to be controlled by circuit breaker. E.g. a URI.</param>
 /// <param name="rules">Rules to be evaluated to decide if the state should be set to OPEN</param>
 /// <param name="service">service that deal with the persistance of the information</param>
 internal CircuitBreaker(string key, List <IRule> rules, IHealthCountService service)
 {
     _rules = rules;
     _keys  = new CircuitBreakerKeys(key);
     _healthCountService = service;
 }