Beispiel #1
0
 public HttpOptionsTests()
 {
     defaultOptions = Options.Configure()
                      .WithHandlerFactory(x => new TestHandlerFactory())
                      .WithCommandRegistry(x => new TestCommandRegistry())
                      .ForHttp()
                      .WithCommandNameResolver(x => new TestCommandNameResolver())
                      .WithCommandFactory(x => new TestCommandFactory());
 }
        public async Task ConfigurationIsWorking()
        {
            // serviceCollection.AddSingleton<HttpJsonPlaceholderService, HttpJsonPlaceholderService>();
            var factory = HttpOptionsBuilder.Configure(services =>
            {
                services.Configure(options =>
                {
                    options.Defaults.Connection.Server = "defaults.com";
                    options.AddClient("service", options =>
                    {
                        options.ServiceName          = "service";
                        options.Connection.Server    = "service.com";
                        options.Connection.Schema    = "https";
                        options.Connection.Port      = 443;
                        options.Connection.TimeoutMS = 50;
                    });

                    options.AddClient("service2", options =>
                    {
                        options.Connection.Server    = "service2.com";
                        options.Connection.Schema    = "http";
                        options.Connection.Port      = 443;
                        options.Connection.TimeoutMS = 50;
                    });

                    options.AddClient("service3", options =>
                    {
                        options.Connection.Server    = "service3.com";
                        options.Connection.Schema    = "https";
                        options.Connection.Port      = 1234;
                        options.Connection.TimeoutMS = 50;
                    });
                });
            })
                          .Build();

            var client   = factory.CreateClient("service");
            var client2  = factory.CreateClient("service2");
            var client3  = factory.CreateClient("service3");
            var defaults = factory.CreateClient("service4");

            Assert.Multiple(() =>
            {
                Assert.AreEqual(client.BaseAddress.ToString(), new Uri("https://service.com:443").ToString());
                Assert.AreEqual(client2.BaseAddress.ToString(), new Uri("http://service2.com:443").ToString());
                Assert.AreEqual(client3.BaseAddress.ToString(), new Uri("https://service3.com:1234").ToString());
                Assert.AreEqual(defaults.BaseAddress.ToString(), new Uri("http://defaults.com").ToString());
            });
        }
        public async Task HttpConnectionChangesAfterTimeoutChange_RemoveFromCache()
        {
            using var server = new WireServer(WireMockServer.Start());

            var timeout = new HttpTimeoutOptions()
            {
                TimeoutMS = 5000
            };

            var httpClientCollection = HttpOptionsBuilder.Configure(builder =>
            {
                builder.Configure(options =>
                {
                    options.AddClient("service", clientOptions =>
                    {
                        server.ConfigureWireMockServer(clientOptions);

                        clientOptions.Handler.HandlerLifeTimeMinutes = 0.05;
                        clientOptions.Timeout.Timeout = timeout.Timeout;
                    });
                }
                                  );
            }).Build();


            var client = httpClientCollection.CreateClient("service");
            await client.GetAsync("/delay/300ms");

            timeout.TimeoutMS = 1;
            httpClientCollection.ServiceProvider().GetRequiredService <IOptionsMonitorCache <HttpClientOptions> >().Clear();
            httpClientCollection.ServiceProvider().GetRequiredService <IOptionsMonitorCache <HttpClientCollectionOptions> >().TryRemove(HttpClientCollectionOptions.DefaultName);

            await Task.Delay(TimeSpan.FromSeconds(10));

            var ex = Policy.Handle <AssertionException>().WaitAndRetry(new[]
            {
                TimeSpan.FromSeconds(10),
                TimeSpan.FromSeconds(10),
                TimeSpan.FromSeconds(20),
            }).Execute(() =>
                       Assert.ThrowsAsync <TimeoutException>(() => httpClientCollection.CreateClient("service").GetAsync("/delay/300ms")));

            // factory.ServiceProvider().GetRequiredService<IOptionsMonitorCache<HttpClientOptions>>().Clear();
            // factory.ServiceProvider().GetRequiredService<IOptionsMonitorCache<HttpClientCollectionOptions>>().Clear();

            Assert.That(ex.Data.Keys, Has.One.Items.Contains("timeout"));
            Assert.That(ex.Data["timeout"], Is.EqualTo(TimeSpan.FromMilliseconds(1)));
        }
        public async Task HttpClient_ErrorCircuitBreakerTest()
        {
            using var server = new WireServer(WireMockServer.Start());


            var httpClientCollection = HttpOptionsBuilder.Configure(builder =>
            {
                builder.Configure(options =>
                {
                    options.AddClient("service", clientOptions =>
                    {
                        server.ConfigureWireMockServer(clientOptions);
                        clientOptions.Polly.CircuitBreaker.SamplingDuration = 3000;
                        clientOptions.Polly.CircuitBreaker.Enabled          = true;
                    });
                }
                                  );
            }).Build();


            var factory = httpClientCollection.GetFactory();
            var client  = factory.CreateClient("service");

            await Observable
            .FromAsync(ct =>
                       client
                       .GetAsync("/error/5ms", ct))
            .Catch(Observable.Return(new HttpResponseMessage()))
            .Repeat(50)
            .RepeatWhen(c => c.DelaySubscription(TimeSpan.FromMilliseconds(10)))
            .TakeUntil(DateTimeOffset.Now.AddSeconds(5));

            Assert.That(async() => await client.GetAsync("/error/5ms"), Throws.InstanceOf <BrokenCircuitException>());
            Assert.That(async() => await client.GetAsync("/delay/5ms"), Throws.InstanceOf <BrokenCircuitException>());
            Assert.That(async() => await client.GetAsync("/delay/1s"), Throws.InstanceOf <BrokenCircuitException>());
        }
Beispiel #5
0
 public static HttpOptionsBuilder WithOwinDefaults(this HttpOptionsBuilder optionsBuilder)
 {
     return(optionsBuilder.WithCommandNameResolver(x => new RequestHeaderCommandNameResolver())
            .WithCommandFactory(x => new JsonDeserializerCommandFactory(x.CommandRegistry, new OwinEnvironmentBodyProvider())));
 }