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);
        }
Esempio n. 2
0
        public async Task Should_retry_when_client_returns_500()
        {
            const int retryCount    = 3;
            var       retrySettings = RetryPolicySettings.Constant(retryCount, TimeSpan.FromMilliseconds(1));

            var wrapper = Create.HttpClientWrapperWrapperBuilder
                          .WithStatusCode(HttpStatusCode.InternalServerError)
                          .WithRetrySettings(retrySettings)
                          .Please();

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

            Assert.AreEqual(retryCount + 1, wrapper.NumberOfCalls);
        }
Esempio n. 3
0
        public async Task Should_retry_3_times_when_client_returns_503()
        {
            const int retryCount    = 3;
            var       retrySettings = RetryPolicySettings.Constant(retryCount, TimeSpan.FromMilliseconds(1));

            var wrapper = Create.HttpClientWrapperWrapperBuilder
                          .WithStatusCode(HttpStatusCode.ServiceUnavailable)
                          .WithRetrySettings(retrySettings)
                          .Please();

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

            Assert.AreEqual(HttpStatusCode.ServiceUnavailable, result.StatusCode);
            Assert.AreEqual(retryCount + 1, 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"));
        }
        public void Should_fail_on_HttpClient_timeout_with_retry()
        {
            const int retryCount    = 5;
            var       retrySettings = RetryPolicySettings.Constant(retryCount, TimeSpan.FromMilliseconds(1));

            var wrapper = Create.HttpClientWrapperWrapperBuilder
                          .WithStatusCode(HttpStatusCode.ServiceUnavailable)
                          .WithResponseLatency(TimeSpan.FromMilliseconds(50))
                          .WithTimeoutOverall(TimeSpan.FromMilliseconds(100))
                          .WithRetrySettings(retrySettings)
                          .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       retrySettings = RetryPolicySettings.Constant(retryCount, TimeSpan.FromMilliseconds(200));

            var wrapper = Create.HttpClientWrapperWrapperBuilder
                          .WithStatusCode(HttpStatusCode.OK)
                          .WithResponseLatency(TimeSpan.FromMilliseconds(200))
                          .WithTimeoutPerTry(TimeSpan.FromMilliseconds(100))
                          .WithRetrySettings(retrySettings)
                          .Please();

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

            Assert.AreEqual(retryCount + 1, wrapper.NumberOfCalls);
        }
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 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 void Should_break_after_4_concurrent_calls()
        {
            const int retryCount        = 5;
            const int minimumThroughput = 2;
            var       retrySettings     = RetryPolicySettings.Constant(retryCount, TimeSpan.FromMilliseconds(50));

            var wrapper = Create.HttpClientWrapperWrapperBuilder
                          .WithStatusCode(HttpStatusCode.ServiceUnavailable)
                          .WithTimeoutOverall(TimeSpan.FromSeconds(5))
                          .WithCircuitBreakerSettings(BuildCircuitBreakerSettings(minimumThroughput))
                          .WithRetrySettings(retrySettings)
                          .Please();

            const int taskCount = 4;

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

            Assert.AreEqual(minimumThroughput, wrapper.NumberOfCalls);
        }
        public void Should_catch_timeout_1_times_because_of_overall_timeout_less_than_per_try_timeout()
        {
            const int retryCount = 5;
            var       settings   = new ResiliencePoliciesSettings
            {
                OverallTimeout      = TimeSpan.FromMilliseconds(100),
                TimeoutPerTry       = TimeSpan.FromMilliseconds(200),
                RetryPolicySettings = RetryPolicySettings.Constant(retryCount, TimeSpan.FromMilliseconds(200)),
            };
            var wrapper = Create.HttpClientWrapperWrapperBuilder
                          .WithStatusCode(HttpStatusCode.OK)
                          .WithResponseLatency(TimeSpan.FromMilliseconds(300))
                          .WithResiliencePolicySettings(settings)
                          .Please();

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

            Assert.AreEqual(1, wrapper.NumberOfCalls);
        }