コード例 #1
0
        public void ExecuteAction_StateShouldBeClosedWhenJustCreated()
        {
            //Arrange
            string key = "testKey";
            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);
            var cbClosed = circuitBreakerFactory.Create(key, 2);

            //Act

            //Assert
            Assert.False(cbClosed.IsOpen());
        }
コード例 #2
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));
        }
コード例 #3
0
        public void ExecuteAction_StateShouldBeOpenAfterNumberOfFailuresExceedsThreshold()
        {
            //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);

            int actualNumberOfFailures = 0;

            //Act
            for (int i = 1; i < numberOfFailuresThreshold + 3; i++)
            {
                try
                {
                    var cb = circuitBreakerFactory.Create(key, numberOfFailuresThreshold);
                    cb.ExecuteAction(() => { throw new TimeoutException(); });
                }
                catch (TimeoutException)
                {
                }
                catch (BrokenCircuitException ex)
                {
                    actualNumberOfFailures = i - 1;//i stores the actual trial
                    break;
                }
            }

            var cbf = circuitBreakerFactory.Create(key, numberOfFailuresThreshold);

            //Assert
            Assert.True(cbf.IsOpen());
            Assert.Equal(numberOfFailuresThreshold, actualNumberOfFailures);
        }