Esempio n. 1
0
        public void GetPolicyTPolicy_should_return_null_if_no_TPolicy()
        {
            Policy     policyA = Policy.NoOp();
            Policy     policyB = Policy.Handle <Exception>().Retry();
            Policy     policyC = Policy.NoOp();
            PolicyWrap wrap    = policyA.Wrap(policyB.Wrap(policyC));

            wrap.GetPolicy <CircuitBreakerPolicy>().Should().BeNull();
        }
Esempio n. 2
0
        public void GetPolicyTPolicy_should_return_single_policy_of_type_TPolicy()
        {
            Policy     policyA = Policy.NoOp();
            Policy     policyB = Policy.Handle <Exception>().Retry();
            Policy     policyC = Policy.NoOp();
            PolicyWrap wrap    = policyA.Wrap(policyB.Wrap(policyC));

            wrap.GetPolicy <RetryPolicy>().Should().BeSameAs(policyB);
        }
Esempio n. 3
0
        public void GetPolicyTPolicy_with_predicate_should_throw_if_predicate_is_null()
        {
            Policy     policyA = Policy.NoOp();
            Policy     policyB = Policy.NoOp();
            PolicyWrap wrap    = policyA.Wrap(policyB);

            Action configure = () => wrap.GetPolicy <NoOpPolicy>(null);

            configure.ShouldThrow <ArgumentNullException>().And.ParamName.Should().Be("filter");
        }
Esempio n. 4
0
        public void GetPolicyTPolicy_should_return_null_if_none_match_predicate()
        {
            CircuitBreakerPolicy policyA = Policy.Handle <Exception>().CircuitBreaker(1, TimeSpan.Zero);
            Policy policyB = Policy.Handle <Exception>().Retry();
            CircuitBreakerPolicy policyC = Policy.Handle <Exception>().CircuitBreaker(1, TimeSpan.Zero);

            PolicyWrap wrap = policyA.Wrap(policyB.Wrap(policyC));

            wrap.GetPolicy <CircuitBreakerPolicy>(p => p.CircuitState == CircuitState.Open).Should().BeNull();
        }
        /// <summary>
        /// 重试并熔断
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="fun"></param>
        /// <returns></returns>
        public async Task <T> ExecRetryBreakerAsync <T>(Func <Task <T> > fun)
        {
            var cb = _policyRetryBreakerAsync.GetPolicy <CircuitBreakerPolicy>();

            if (cb.CircuitState.HasFlag(CircuitState.Open))
            {
                throw new Exception("处于熔断状态");
            }

            return(await _policyRetryBreaker.ExecuteAsync(fun));
        }
        /// <summary>
        /// 重试并熔断
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="fun"></param>
        /// <returns></returns>
        public T ExecRetryBreaker <T>(Func <T> fun)
        {
            var cb = _policyRetryBreaker.GetPolicy <CircuitBreakerPolicy>();

            if (cb.CircuitState.HasFlag(CircuitState.Open))
            {
                throw new Exception("处于熔断状态");
            }

            return(_policyRetryBreaker.Execute(fun));
        }
Esempio n. 7
0
        public void GetPolicyTPolicy_should_return_single_policy_of_type_TPolicy_matching_predicate()
        {
            CircuitBreakerPolicy policyA = Policy.Handle <Exception>().CircuitBreaker(1, TimeSpan.Zero);
            Policy policyB = Policy.Handle <Exception>().Retry();
            CircuitBreakerPolicy policyC = Policy.Handle <Exception>().CircuitBreaker(1, TimeSpan.Zero);

            policyA.Isolate();

            PolicyWrap wrap = policyA.Wrap(policyB.Wrap(policyC));

            wrap.GetPolicy <CircuitBreakerPolicy>(p => p.CircuitState == CircuitState.Closed).Should().BeSameAs(policyC);
        }