public async Task Should_Open_Circuit_Breaker_for_RU_and_do_not_affect_EE()
        {
            const int retryCount        = 5;
            const int minimumThroughput = 2;
            var       settings          = new ResiliencePoliciesSettings
            {
                OverallTimeout               = TimeSpan.FromSeconds(5),
                RetryPolicySettings          = RetryPolicySettings.Constant(retryCount, TimeSpan.FromMilliseconds(50)),
                CircuitBreakerPolicySettings = BuildCircuitBreakerSettings(minimumThroughput),
            };
            var wrapper = Create.HttpClientWrapperWrapperBuilder
                          .WithHostAndStatusCode("ru-prod.com", HttpStatusCode.ServiceUnavailable)
                          .WithHostAndStatusCode("ee-prod.com", HttpStatusCode.OK)
                          .WithResiliencePolicySettings(settings)
                          .Please();

            const int taskCount = 4;

            Assert.CatchAsync <BrokenCircuitException>(async() =>
                                                       await Helper.InvokeMultipleHttpRequests(wrapper.Client, taskCount,
                                                                                               "http://ru-prod.com/Test1/Test2"));

            await Helper.InvokeMultipleHttpRequests(wrapper.Client, taskCount,
                                                    "http://ee-prod.com/Test3/Test4/Test5");

            Assert.AreEqual(minimumThroughput + taskCount, wrapper.NumberOfCalls);
        }
        public void Should_set_HttpClient_Timeout_property_to_overall_timeout_plus_delta_1000ms()
        {
            const int overallTimeoutInMilliseconds = 200;
            var       settings = new ResiliencePoliciesSettings
            {
                OverallTimeout =
                    TimeSpan.FromMilliseconds(overallTimeoutInMilliseconds),
            };
            var wrapper = Create.HttpClientWrapperWrapperBuilder
                          .WithResiliencePolicySettings(settings)
                          .Please();

            Assert.AreEqual(overallTimeoutInMilliseconds + 1000, wrapper.Client.Timeout.TotalMilliseconds);
        }
        public async Task Should_retry_when_client_returns_500()
        {
            const int retryCount = 3;
            var       settings   = new ResiliencePoliciesSettings
            {
                RetryPolicySettings = RetryPolicySettings.Constant(retryCount, TimeSpan.FromMilliseconds(1)),
            };
            var wrapper = Create.HttpClientWrapperWrapperBuilder
                          .WithStatusCode(HttpStatusCode.InternalServerError)
                          .WithResiliencePolicySettings(settings)
                          .Please();

            await wrapper.Client.GetAsync("http://localhost");

            Assert.AreEqual(retryCount + 1, wrapper.NumberOfCalls);
        }
        public void Should_catch_timeout_because_of_overall_timeout()
        {
            var settings = new ResiliencePoliciesSettings
            {
                OverallTimeout = TimeSpan.FromMilliseconds(100),
            };
            var wrapper = Create.HttpClientWrapperWrapperBuilder
                          .WithStatusCode(HttpStatusCode.OK)
                          .WithResponseLatency(TimeSpan.FromMilliseconds(200))
                          .WithResiliencePolicySettings(settings)
                          .Please();

            Assert.CatchAsync <TimeoutRejectedException>(async() =>
                                                         await wrapper.Client.GetAsync("http://localhost"));
            Assert.AreEqual(1, wrapper.NumberOfCalls);
        }
        public async Task Should_retry_6_times_for_two_threads_when_client_returns_503()
        {
            const int retryCount = 3;
            var       settings   = new ResiliencePoliciesSettings
            {
                RetryPolicySettings = RetryPolicySettings.Jitter(retryCount, TimeSpan.FromMilliseconds(50)),
            };
            var wrapper = Create.HttpClientWrapperWrapperBuilder
                          .WithStatusCode(HttpStatusCode.ServiceUnavailable)
                          .WithResiliencePolicySettings(settings)
                          .Please();

            const int taskCount = 2;
            await Helper.InvokeMultipleHttpRequests(wrapper.Client, taskCount);

            Assert.AreEqual((retryCount + 1) * taskCount, wrapper.NumberOfCalls);
        }
        public void Should_catch_timeout_because_of_overall_less_then_sleep_duration_of_RetryAfterDecorator()
        {
            const int retryCount = 3;
            var       settings   = new ResiliencePoliciesSettings
            {
                OverallTimeout      = TimeSpan.FromSeconds(2),
                RetryPolicySettings = RetryPolicySettings.Constant(retryCount),
            };
            var wrapper = Create.HttpClientWrapperWrapperBuilder
                          .WithRetryAfterHeader(TimeSpan.FromSeconds(1))
                          .WithStatusCode(HttpStatusCode.InternalServerError)
                          .WithResiliencePolicySettings(settings)
                          .Please();

            Assert.CatchAsync <TimeoutRejectedException>(async() =>
                                                         await wrapper.Client.GetAsync("http://localhost"));
        }
Esempio n. 7
0
        public async Task Should_catch_retry_in_OnRetry_handler_passed_before_RetryPolicySettings()
        {
            var retryCounter = 0;
            var settings     = new ResiliencePoliciesSettings
            {
                OnRetry             = (_, __) => { retryCounter++; },
                RetryPolicySettings = RetryPolicySettings.Constant(Defaults.Retry.RetryCount),
            };
            var wrapper = Create.HttpClientWrapperWrapperBuilder
                          .WithStatusCode(HttpStatusCode.ServiceUnavailable)
                          .WithResiliencePolicySettings(settings)
                          .Please();

            await wrapper.Client.GetAsync("http://localhost");

            Assert.AreEqual(Defaults.Retry.RetryCount, retryCounter);
        }
        public void Should_fail_on_HttpClient_timeout_with_retry()
        {
            const int retryCount = 5;
            var       settings   = new ResiliencePoliciesSettings
            {
                OverallTimeout      = TimeSpan.FromMilliseconds(200),
                RetryPolicySettings = RetryPolicySettings.Constant(retryCount, TimeSpan.FromMilliseconds(1)),
            };
            var wrapper = Create.HttpClientWrapperWrapperBuilder
                          .WithStatusCode(HttpStatusCode.ServiceUnavailable)
                          .WithResponseLatency(TimeSpan.FromMilliseconds(100))
                          .WithResiliencePolicySettings(settings)
                          .Please();

            Assert.CatchAsync <TimeoutRejectedException>(async() =>
                                                         await wrapper.Client.GetAsync("http://localhost"));
            Assert.AreEqual(2, wrapper.NumberOfCalls);
        }
        public void Should_retry_5_times_200_status_code_because_of_per_try_timeout()
        {
            const int retryCount = 5;
            var       settings   = new ResiliencePoliciesSettings
            {
                TimeoutPerTry       = TimeSpan.FromMilliseconds(100),
                RetryPolicySettings = RetryPolicySettings.Constant(retryCount, TimeSpan.FromMilliseconds(200)),
            };
            var wrapper = Create.HttpClientWrapperWrapperBuilder
                          .WithStatusCode(HttpStatusCode.OK)
                          .WithResponseLatency(TimeSpan.FromMilliseconds(200))
                          .WithResiliencePolicySettings(settings)
                          .Please();

            Assert.CatchAsync <TimeoutRejectedException>(async() =>
                                                         await wrapper.Client.GetAsync("http://localhost"));

            Assert.AreEqual(retryCount + 1, wrapper.NumberOfCalls);
        }
        public async Task Should_retry_sleep_longer_when_RetryAfterDecorator_is_on()
        {
            const int retryCount = 3;
            var       settings   = new ResiliencePoliciesSettings
            {
                RetryPolicySettings = RetryPolicySettings.Constant(retryCount),
            };
            var wrapper = Create.HttpClientWrapperWrapperBuilder
                          .WithRetryAfterHeader(TimeSpan.FromSeconds(1))
                          .WithStatusCode(HttpStatusCode.InternalServerError)
                          .WithResiliencePolicySettings(settings)
                          .Please();

            var stopWatch = Stopwatch.StartNew();
            await wrapper.Client.GetAsync("http://localhost");

            stopWatch.Stop();

            Assert.That(3.0d, Is.GreaterThanOrEqualTo(stopWatch.Elapsed.TotalSeconds).Within(0.1));
        }
        public async Task Should_separately_distribute_retry_attempts_for_multiple_tasks()
        {
            const int retryCount    = 3;
            var       retryAttempts = new Dictionary <string, List <TimeSpan> >();
            var       settings      = new ResiliencePoliciesSettings
            {
                RetryPolicySettings = RetryPolicySettings.Jitter(retryCount, TimeSpan.FromMilliseconds(50)),
                OnRetry             = BuildOnRetryAction(retryAttempts),
            };

            var wrapper = Create.HttpClientWrapperWrapperBuilder
                          .WithStatusCode(HttpStatusCode.ServiceUnavailable)
                          .WithResiliencePolicySettings(settings)
                          .Please();

            const int taskCount = 2;
            await Helper.InvokeMultipleHttpRequests(wrapper.Client, taskCount);

            CollectionAssert.AreNotEquivalent(retryAttempts.First().Value, retryAttempts.Last().Value);
        }
        public void Should_break_after_4_concurrent_calls()
        {
            const int retryCount        = 5;
            const int minimumThroughput = 2;
            var       settings          = new ResiliencePoliciesSettings
            {
                OverallTimeout               = TimeSpan.FromSeconds(5),
                RetryPolicySettings          = RetryPolicySettings.Constant(retryCount, TimeSpan.FromMilliseconds(100)),
                CircuitBreakerPolicySettings = BuildCircuitBreakerSettings(minimumThroughput),
            };
            var wrapper = Create.HttpClientWrapperWrapperBuilder
                          .WithStatusCode(HttpStatusCode.ServiceUnavailable)
                          .WithResiliencePolicySettings(settings)
                          .Please();

            const int taskCount = 4;

            Assert.CatchAsync <BrokenCircuitException>(async() =>
                                                       await Helper.InvokeMultipleHttpRequests(wrapper.Client, taskCount));

            Assert.LessOrEqual(wrapper.NumberOfCalls, taskCount);
        }
Esempio n. 13
0
 public HttpClientWrapperBuilder WithResiliencePolicySettings(ResiliencePoliciesSettings resiliencePoliciesSettings)
 {
     _resiliencePoliciesSettings = resiliencePoliciesSettings;
     return(this);
 }