Ejemplo n.º 1
0
        public void Wrapping_two_policies_by_static_syntax_and_executing_should_wrap_outer_then_inner_around_delegate()
        {
            var retry   = Policy.Handle <Exception>().RetryAsync(1); // Two tries in total: first try, plus one retry.
            var breaker = Policy.Handle <Exception>().CircuitBreakerAsync(2, TimeSpan.MaxValue);

            AsyncPolicyWrap retryWrappingBreaker = Policy.WrapAsync(retry, breaker);
            AsyncPolicyWrap breakerWrappingRetry = Policy.WrapAsync(breaker, retry);

            // When the retry wraps the breaker, the retry (being outer) should cause the call to be put through the breaker twice - causing the breaker to break.
            breaker.Reset();
            retryWrappingBreaker.Awaiting(async x => await x.RaiseExceptionAsync <DivideByZeroException>(2))
            .ShouldThrow <DivideByZeroException>();
            breaker.CircuitState.Should().Be(CircuitState.Open);

            // When the breaker wraps the retry, the retry (being inner) should retry twice before throwing the exception back on the breaker - the exception only hits the breaker once - so the breaker should not break.
            breaker.Reset();
            breakerWrappingRetry.Awaiting(async x => await x.RaiseExceptionAsync <DivideByZeroException>(2))
            .ShouldThrow <DivideByZeroException>();
            breaker.CircuitState.Should().Be(CircuitState.Closed);
        }