public async Task WrappedDependency_TestPoliciesSeperateFromCode()
        {
            // setup
            var resiliencyPolicy = DemoPolicyFactory.GetCustomerDatabaseResiliencyPolicy();

            // act
            var sw = new Stopwatch();

            sw.Start();

            // simulate a slow call (should timeout and return default)
            var customer = await resiliencyPolicy.ExecuteAsync(async() =>
            {
                await Task.Delay(10000);
                return(new CustomerModel()
                {
                    CustomerId = 1,
                    FirstName = "Test",
                    LastName = "Tester",
                    Email = "*****@*****.**"
                });
            });

            // assert
            Assert.AreEqual("Customer Is Not Available.", customer.Message);
            Assert.IsTrue(sw.ElapsedMilliseconds < 10000);
        }
Example #2
0
        public async Task HttpClientFactory_ApplyMultiplePolicies()
        {
            IServiceCollection services = new ServiceCollection();

            ///// ADD HTTP CLIENT VIA HTTPCLIENTFACTORY AND POLICIES
            // this would typically happen in Startup.ConfigureServices
            // using a named client, can specify a <TClient> to inject it into a specific class

            // setup circuit breaker seperately so you can maintain a pointer to it for testing (validate state, reset, etc)
            var circuitBreakerPolicy = DemoPolicyFactory.GetHttpCircuitBreakerPolicy();
            ICircuitBreakerPolicy <HttpResponseMessage> circuitBreakerPointer = (ICircuitBreakerPolicy <HttpResponseMessage>)circuitBreakerPolicy;

            services.AddHttpClient("CustomerService",
                                   client =>
            {
                client.BaseAddress = new Uri(DemoHelper.DemoBaseUrl);
                client.Timeout     =
                    TimeSpan.FromSeconds(
                        3);         // this will apply as an outer timeout to the entire request flow, including waits, retries,etc
            })

            // add policies from outer (1st executed) to inner (closest to dependency call)
            // all policies must implement IASyncPolicy
            .AddPolicyHandler(DemoPolicyFactory.GetHttpFallbackPolicy("Default!"))
            .AddPolicyHandler(DemoPolicyFactory.GetHttpRetryPolicy())
            .AddPolicyHandler(circuitBreakerPolicy)
            .AddPolicyHandler(DemoPolicyFactory.GetHttpInnerTimeoutPolicy());

            ///// TEST POLICIES /////
            await VerifyResiliencyPolicies(services, circuitBreakerPointer);
        }
        public async Task WrappedDependency_TestPoliciesWithCode()
        {
            // setup
            var resiliencyPolicy = DemoPolicyFactory.GetCustomerDatabaseResiliencyPolicy();
            var classUnderTest   = new CustomerService_Database(new TestRepository(), resiliencyPolicy);

            // act
            var sw = new Stopwatch();

            sw.Start();
            var customer = await classUnderTest.GetCustomerByIdAsync(1);

            // assert
            Assert.AreEqual("Customer Is Not Available.", customer.Message);
            Assert.IsTrue(sw.ElapsedMilliseconds < 10000);
        }
Example #4
0
        public async Task HttpClientFactory_OnlyApplyPolicyToGetRequests()
        {
            IServiceCollection services = new ServiceCollection();

            services.AddHttpClient("CustomerService",
                                   client =>
            {
                client.BaseAddress = new Uri(DemoHelper.DemoBaseUrl);
                client.Timeout     =
                    TimeSpan.FromSeconds(3);
            })
            .AddPolicyHandler(request =>
                              request.Method == HttpMethod.Get
                        ? DemoPolicyFactory.GetHttpRetryPolicy()
                        : Policy.NoOpAsync <HttpResponseMessage>());
            var serviceProvider = services.BuildServiceProvider();
            var clientFactory   = serviceProvider.GetService <IHttpClientFactory>();
            var httpClient      = clientFactory.CreateClient("CustomerService");

            var responseMessage = await httpClient.GetAsync("api/demo/error?failures=1");

            Assert.IsTrue(responseMessage.IsSuccessStatusCode);
        }