mirror of
				https://github.com/nsnail/Ocelot.git
				synced 2025-11-04 08:15:27 +08:00 
			
		
		
		
	Rewire LoadBalancerFactory to allow injecting custom load balancers, by introducing LoadBalancerCreators
This commit is contained in:
		@@ -87,6 +87,10 @@ namespace Ocelot.DependencyInjection
 | 
			
		||||
            Services.TryAddSingleton<IFileConfigurationRepository, DiskFileConfigurationRepository>();
 | 
			
		||||
            Services.TryAddSingleton<IFileConfigurationSetter, FileAndInternalConfigurationSetter>();
 | 
			
		||||
            Services.TryAddSingleton<IServiceDiscoveryProviderFactory, ServiceDiscoveryProviderFactory>();
 | 
			
		||||
            Services.AddSingleton<ILoadBalancerCreator, NoLoadBalancerCreator>();
 | 
			
		||||
            Services.AddSingleton<ILoadBalancerCreator, RoundRobinCreator>();
 | 
			
		||||
            Services.AddSingleton<ILoadBalancerCreator, CookieStickySessionsCreator>();
 | 
			
		||||
            Services.AddSingleton<ILoadBalancerCreator, LeastConnectionCreator>();
 | 
			
		||||
            Services.TryAddSingleton<ILoadBalancerFactory, LoadBalancerFactory>();
 | 
			
		||||
            Services.TryAddSingleton<ILoadBalancerHouse, LoadBalancerHouse>();
 | 
			
		||||
            Services.TryAddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,20 @@
 | 
			
		||||
namespace Ocelot.LoadBalancer.LoadBalancers
 | 
			
		||||
{
 | 
			
		||||
    using System.Threading.Tasks;
 | 
			
		||||
    using Ocelot.Configuration;
 | 
			
		||||
    using Ocelot.Infrastructure;
 | 
			
		||||
    using Ocelot.ServiceDiscovery.Providers;
 | 
			
		||||
 | 
			
		||||
    public class CookieStickySessionsCreator : ILoadBalancerCreator
 | 
			
		||||
    {
 | 
			
		||||
        public ILoadBalancer Create(DownstreamReRoute reRoute, IServiceDiscoveryProvider serviceProvider)
 | 
			
		||||
        {
 | 
			
		||||
            var loadBalancer = new RoundRobin(async () => await serviceProvider.Get());
 | 
			
		||||
            var bus = new InMemoryBus<StickySession>();
 | 
			
		||||
            return new CookieStickySessions(loadBalancer, reRoute.LoadBalancerOptions.Key,
 | 
			
		||||
                reRoute.LoadBalancerOptions.ExpiryInMs, bus);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public string Type => nameof(CookieStickySessions);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,11 @@
 | 
			
		||||
using Ocelot.Configuration;
 | 
			
		||||
using Ocelot.ServiceDiscovery.Providers;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.LoadBalancer.LoadBalancers
 | 
			
		||||
{
 | 
			
		||||
    public interface ILoadBalancerCreator
 | 
			
		||||
    {
 | 
			
		||||
        ILoadBalancer Create(DownstreamReRoute reRoute, IServiceDiscoveryProvider serviceProvider);
 | 
			
		||||
        string Type { get; }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,15 @@
 | 
			
		||||
namespace Ocelot.LoadBalancer.LoadBalancers
 | 
			
		||||
{
 | 
			
		||||
    using Ocelot.Configuration;
 | 
			
		||||
    using Ocelot.ServiceDiscovery.Providers;
 | 
			
		||||
 | 
			
		||||
    public class LeastConnectionCreator : ILoadBalancerCreator
 | 
			
		||||
    {
 | 
			
		||||
        public ILoadBalancer Create(DownstreamReRoute reRoute, IServiceDiscoveryProvider serviceProvider)
 | 
			
		||||
        {
 | 
			
		||||
            return new LeastConnection(async () => await serviceProvider.Get(), reRoute.ServiceName);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public string Type => nameof(LeastConnection);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,47 +1,42 @@
 | 
			
		||||
using Ocelot.Configuration;
 | 
			
		||||
using Ocelot.Infrastructure;
 | 
			
		||||
using Ocelot.Responses;
 | 
			
		||||
using Ocelot.ServiceDiscovery;
 | 
			
		||||
using System.Threading.Tasks;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.LoadBalancer.LoadBalancers
 | 
			
		||||
namespace Ocelot.LoadBalancer.LoadBalancers
 | 
			
		||||
{
 | 
			
		||||
    using System.Collections.Generic;
 | 
			
		||||
    using System.Linq;
 | 
			
		||||
    using Ocelot.Configuration;
 | 
			
		||||
    using Ocelot.Responses;
 | 
			
		||||
    using System.Threading.Tasks;
 | 
			
		||||
    using Ocelot.ServiceDiscovery;
 | 
			
		||||
 | 
			
		||||
    public class LoadBalancerFactory : ILoadBalancerFactory
 | 
			
		||||
    {
 | 
			
		||||
        private readonly IServiceDiscoveryProviderFactory _serviceProviderFactory;
 | 
			
		||||
        private readonly IEnumerable<ILoadBalancerCreator> _loadBalancerCreators;
 | 
			
		||||
 | 
			
		||||
        public LoadBalancerFactory(IServiceDiscoveryProviderFactory serviceProviderFactory)
 | 
			
		||||
        public LoadBalancerFactory(IServiceDiscoveryProviderFactory serviceProviderFactory, IEnumerable<ILoadBalancerCreator> loadBalancerCreators)
 | 
			
		||||
        {
 | 
			
		||||
            _serviceProviderFactory = serviceProviderFactory;
 | 
			
		||||
            _loadBalancerCreators = loadBalancerCreators;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public async Task<Response<ILoadBalancer>> Get(DownstreamReRoute reRoute, ServiceProviderConfiguration config)
 | 
			
		||||
        public Task<Response<ILoadBalancer>> Get(DownstreamReRoute reRoute, ServiceProviderConfiguration config)
 | 
			
		||||
        {
 | 
			
		||||
            var response = _serviceProviderFactory.Get(config, reRoute);
 | 
			
		||||
            var serviceProviderFactoryResponse = _serviceProviderFactory.Get(config, reRoute);
 | 
			
		||||
 | 
			
		||||
            if (response.IsError)
 | 
			
		||||
            Response<ILoadBalancer> response;
 | 
			
		||||
            if (serviceProviderFactoryResponse.IsError)
 | 
			
		||||
            {
 | 
			
		||||
                return new ErrorResponse<ILoadBalancer>(response.Errors);
 | 
			
		||||
                response = new ErrorResponse<ILoadBalancer>(serviceProviderFactoryResponse.Errors);
 | 
			
		||||
            }
 | 
			
		||||
            else
 | 
			
		||||
            {
 | 
			
		||||
                var serviceProvider = serviceProviderFactoryResponse.Data;
 | 
			
		||||
                var requestedType = reRoute.LoadBalancerOptions?.Type ?? nameof(NoLoadBalancer);
 | 
			
		||||
                var applicableCreator = _loadBalancerCreators.Single(c => c.Type == requestedType);
 | 
			
		||||
                var createdLoadBalancer = applicableCreator.Create(reRoute, serviceProvider);
 | 
			
		||||
                response = new OkResponse<ILoadBalancer>(createdLoadBalancer);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            var serviceProvider = response.Data;
 | 
			
		||||
 | 
			
		||||
            switch (reRoute.LoadBalancerOptions?.Type)
 | 
			
		||||
            {
 | 
			
		||||
                case nameof(RoundRobin):
 | 
			
		||||
                    return new OkResponse<ILoadBalancer>(new RoundRobin(async () => await serviceProvider.Get()));
 | 
			
		||||
 | 
			
		||||
                case nameof(LeastConnection):
 | 
			
		||||
                    return new OkResponse<ILoadBalancer>(new LeastConnection(async () => await serviceProvider.Get(), reRoute.ServiceName));
 | 
			
		||||
 | 
			
		||||
                case nameof(CookieStickySessions):
 | 
			
		||||
                    var loadBalancer = new RoundRobin(async () => await serviceProvider.Get());
 | 
			
		||||
                    var bus = new InMemoryBus<StickySession>();
 | 
			
		||||
                    return new OkResponse<ILoadBalancer>(new CookieStickySessions(loadBalancer, reRoute.LoadBalancerOptions.Key, reRoute.LoadBalancerOptions.ExpiryInMs, bus));
 | 
			
		||||
 | 
			
		||||
                default:
 | 
			
		||||
                    return new OkResponse<ILoadBalancer>(new NoLoadBalancer(async () => await serviceProvider.Get()));
 | 
			
		||||
            }
 | 
			
		||||
            return Task.FromResult(response);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,15 @@
 | 
			
		||||
namespace Ocelot.LoadBalancer.LoadBalancers
 | 
			
		||||
{ 
 | 
			
		||||
    using Ocelot.Configuration;
 | 
			
		||||
    using Ocelot.ServiceDiscovery.Providers;
 | 
			
		||||
 | 
			
		||||
    public class NoLoadBalancerCreator : ILoadBalancerCreator
 | 
			
		||||
    {
 | 
			
		||||
        public ILoadBalancer Create(DownstreamReRoute reRoute, IServiceDiscoveryProvider serviceProvider)
 | 
			
		||||
        {
 | 
			
		||||
            return new NoLoadBalancer(async () => await serviceProvider.Get());
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public string Type => nameof(NoLoadBalancer);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,12 +1,12 @@
 | 
			
		||||
using Ocelot.Middleware;
 | 
			
		||||
using Ocelot.Responses;
 | 
			
		||||
using Ocelot.Values;
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Threading.Tasks;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.LoadBalancer.LoadBalancers
 | 
			
		||||
namespace Ocelot.LoadBalancer.LoadBalancers
 | 
			
		||||
{
 | 
			
		||||
    using Ocelot.Middleware;
 | 
			
		||||
    using Ocelot.Responses;
 | 
			
		||||
    using Ocelot.Values;
 | 
			
		||||
    using System;
 | 
			
		||||
    using System.Collections.Generic;
 | 
			
		||||
    using System.Threading.Tasks;
 | 
			
		||||
 | 
			
		||||
    public class RoundRobin : ILoadBalancer
 | 
			
		||||
    {
 | 
			
		||||
        private readonly Func<Task<List<Service>>> _services;
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										15
									
								
								src/Ocelot/LoadBalancer/LoadBalancers/RoundRobinCreator.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								src/Ocelot/LoadBalancer/LoadBalancers/RoundRobinCreator.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,15 @@
 | 
			
		||||
namespace Ocelot.LoadBalancer.LoadBalancers
 | 
			
		||||
{
 | 
			
		||||
    using Ocelot.Configuration;
 | 
			
		||||
    using Ocelot.ServiceDiscovery.Providers;
 | 
			
		||||
    
 | 
			
		||||
    public class RoundRobinCreator : ILoadBalancerCreator
 | 
			
		||||
    {
 | 
			
		||||
        public ILoadBalancer Create(DownstreamReRoute reRoute, IServiceDiscoveryProvider serviceProvider)
 | 
			
		||||
        {
 | 
			
		||||
            return new RoundRobin(async () => await serviceProvider.Get());
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public string Type => nameof(RoundRobin);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,3 +1,7 @@
 | 
			
		||||
using Ocelot.Configuration;
 | 
			
		||||
using Ocelot.LoadBalancer.LoadBalancers;
 | 
			
		||||
using Ocelot.ServiceDiscovery.Providers;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.UnitTests.DependencyInjection
 | 
			
		||||
{
 | 
			
		||||
    using Microsoft.AspNetCore.Hosting;
 | 
			
		||||
@@ -158,11 +162,26 @@ namespace Ocelot.UnitTests.DependencyInjection
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_add_custom_load_balancer_creators()
 | 
			
		||||
        {
 | 
			
		||||
            this.Given(x => WhenISetUpOcelotServices())
 | 
			
		||||
                .When(x => AddSingletonLoadBalancerCreator<FakeCustomLoadBalancerCreator>())
 | 
			
		||||
                .Then(x => ThenTheProviderIsRegisteredAndReturnsBothBuiltInAndCustomLoadBalancerCreators())
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void AddSingletonDefinedAggregator<T>()
 | 
			
		||||
            where T : class, IDefinedAggregator
 | 
			
		||||
        {
 | 
			
		||||
            _ocelotBuilder.AddSingletonDefinedAggregator<T>();
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        private void AddSingletonLoadBalancerCreator<T>()
 | 
			
		||||
            where T : class, ILoadBalancerCreator
 | 
			
		||||
        {
 | 
			
		||||
            _ocelotBuilder.Services.AddSingleton<ILoadBalancerCreator, T>();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void AddTransientDefinedAggregator<T>()
 | 
			
		||||
            where T : class, IDefinedAggregator
 | 
			
		||||
@@ -238,6 +257,17 @@ namespace Ocelot.UnitTests.DependencyInjection
 | 
			
		||||
            handlers[0].ShouldBeOfType<TOne>();
 | 
			
		||||
            handlers[1].ShouldBeOfType<TWo>();
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        private void ThenTheProviderIsRegisteredAndReturnsBothBuiltInAndCustomLoadBalancerCreators()
 | 
			
		||||
        {
 | 
			
		||||
            _serviceProvider = _services.BuildServiceProvider();
 | 
			
		||||
            var creators = _serviceProvider.GetServices<ILoadBalancerCreator>().ToList();
 | 
			
		||||
            creators.Count(c => c.GetType() == typeof(NoLoadBalancerCreator)).ShouldBe(1);
 | 
			
		||||
            creators.Count(c => c.GetType() == typeof(RoundRobinCreator)).ShouldBe(1);
 | 
			
		||||
            creators.Count(c => c.GetType() == typeof(CookieStickySessionsCreator)).ShouldBe(1);
 | 
			
		||||
            creators.Count(c => c.GetType() == typeof(LeastConnectionCreator)).ShouldBe(1);
 | 
			
		||||
            creators.Count(c => c.GetType() == typeof(FakeCustomLoadBalancerCreator)).ShouldBe(1);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void ThenTheAggregatorsAreTransient<TOne, TWo>()
 | 
			
		||||
        {
 | 
			
		||||
@@ -323,5 +353,15 @@ namespace Ocelot.UnitTests.DependencyInjection
 | 
			
		||||
        {
 | 
			
		||||
            _ex.ShouldBeNull();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private class FakeCustomLoadBalancerCreator : ILoadBalancerCreator
 | 
			
		||||
        {
 | 
			
		||||
            public ILoadBalancer Create(DownstreamReRoute reRoute, IServiceDiscoveryProvider serviceProvider)
 | 
			
		||||
            {
 | 
			
		||||
                throw new NotImplementedException();
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            public string Type { get; }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,73 @@
 | 
			
		||||
using Moq;
 | 
			
		||||
using Ocelot.Configuration;
 | 
			
		||||
using Ocelot.Configuration.Builder;
 | 
			
		||||
using Ocelot.LoadBalancer.LoadBalancers;
 | 
			
		||||
using Ocelot.ServiceDiscovery.Providers;
 | 
			
		||||
using Shouldly;
 | 
			
		||||
using TestStack.BDDfy;
 | 
			
		||||
using Xunit;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.UnitTests.LoadBalancer
 | 
			
		||||
{
 | 
			
		||||
    public class CookieStickySessionsCreatorTests
 | 
			
		||||
    {
 | 
			
		||||
        private readonly CookieStickySessionsCreator _creator;
 | 
			
		||||
        private readonly Mock<IServiceDiscoveryProvider> _serviceProvider;
 | 
			
		||||
        private DownstreamReRoute _reRoute;
 | 
			
		||||
        private ILoadBalancer _loadBalancer;
 | 
			
		||||
        private string _typeName;
 | 
			
		||||
 | 
			
		||||
        public CookieStickySessionsCreatorTests()
 | 
			
		||||
        {
 | 
			
		||||
            _creator = new CookieStickySessionsCreator();
 | 
			
		||||
            _serviceProvider = new Mock<IServiceDiscoveryProvider>();
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_return_expected_name()
 | 
			
		||||
        {
 | 
			
		||||
            var reRoute = new DownstreamReRouteBuilder()
 | 
			
		||||
                .WithLoadBalancerOptions(new LoadBalancerOptions("myType", "myKey", 1000))
 | 
			
		||||
                .Build();
 | 
			
		||||
 | 
			
		||||
            this.Given(x => x.GivenAReRoute(reRoute))
 | 
			
		||||
                .When(x => x.WhenIGetTheLoadBalancer())
 | 
			
		||||
                .Then(x => x.ThenTheLoadBalancerIsReturned<CookieStickySessions>())
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
                
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_return_instance_of_expected_load_balancer_type()
 | 
			
		||||
        {
 | 
			
		||||
            this.When(x => x.WhenIGetTheLoadBalancerTypeName())
 | 
			
		||||
                .Then(x => x.ThenTheLoadBalancerTypeIs("CookieStickySessions"))
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void GivenAReRoute(DownstreamReRoute reRoute)
 | 
			
		||||
        {
 | 
			
		||||
            _reRoute = reRoute;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void WhenIGetTheLoadBalancer()
 | 
			
		||||
        {
 | 
			
		||||
            _loadBalancer = _creator.Create(_reRoute, _serviceProvider.Object);
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        private void WhenIGetTheLoadBalancerTypeName()
 | 
			
		||||
        {
 | 
			
		||||
            _typeName = _creator.Type;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void ThenTheLoadBalancerIsReturned<T>()
 | 
			
		||||
            where T : ILoadBalancer
 | 
			
		||||
        {
 | 
			
		||||
            _loadBalancer.ShouldBeOfType<T>();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void ThenTheLoadBalancerTypeIs(string type)
 | 
			
		||||
        {
 | 
			
		||||
            _typeName.ShouldBe(type);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,73 @@
 | 
			
		||||
using Moq;
 | 
			
		||||
using Ocelot.Configuration;
 | 
			
		||||
using Ocelot.Configuration.Builder;
 | 
			
		||||
using Ocelot.LoadBalancer.LoadBalancers;
 | 
			
		||||
using Ocelot.ServiceDiscovery.Providers;
 | 
			
		||||
using Shouldly;
 | 
			
		||||
using TestStack.BDDfy;
 | 
			
		||||
using Xunit;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.UnitTests.LoadBalancer
 | 
			
		||||
{
 | 
			
		||||
    public class LeastConnectionCreatorTests
 | 
			
		||||
    {
 | 
			
		||||
        private readonly LeastConnectionCreator _creator;
 | 
			
		||||
        private readonly Mock<IServiceDiscoveryProvider> _serviceProvider;
 | 
			
		||||
        private DownstreamReRoute _reRoute;
 | 
			
		||||
        private ILoadBalancer _loadBalancer;
 | 
			
		||||
        private string _typeName;
 | 
			
		||||
 | 
			
		||||
        public LeastConnectionCreatorTests()
 | 
			
		||||
        {
 | 
			
		||||
            _creator = new LeastConnectionCreator();
 | 
			
		||||
            _serviceProvider = new Mock<IServiceDiscoveryProvider>();
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_return_expected_name()
 | 
			
		||||
        {
 | 
			
		||||
            var reRoute = new DownstreamReRouteBuilder()
 | 
			
		||||
                .WithServiceName("myService")
 | 
			
		||||
                .Build();
 | 
			
		||||
 | 
			
		||||
            this.Given(x => x.GivenAReRoute(reRoute))
 | 
			
		||||
                .When(x => x.WhenIGetTheLoadBalancer())
 | 
			
		||||
                .Then(x => x.ThenTheLoadBalancerIsReturned<LeastConnection>())
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
                
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_return_instance_of_expected_load_balancer_type()
 | 
			
		||||
        {
 | 
			
		||||
            this.When(x => x.WhenIGetTheLoadBalancerTypeName())
 | 
			
		||||
                .Then(x => x.ThenTheLoadBalancerTypeIs("LeastConnection"))
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void GivenAReRoute(DownstreamReRoute reRoute)
 | 
			
		||||
        {
 | 
			
		||||
            _reRoute = reRoute;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void WhenIGetTheLoadBalancer()
 | 
			
		||||
        {
 | 
			
		||||
            _loadBalancer = _creator.Create(_reRoute, _serviceProvider.Object);
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        private void WhenIGetTheLoadBalancerTypeName()
 | 
			
		||||
        {
 | 
			
		||||
            _typeName = _creator.Type;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void ThenTheLoadBalancerIsReturned<T>()
 | 
			
		||||
            where T : ILoadBalancer
 | 
			
		||||
        {
 | 
			
		||||
            _loadBalancer.ShouldBeOfType<T>();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void ThenTheLoadBalancerTypeIs(string type)
 | 
			
		||||
        {
 | 
			
		||||
            _typeName.ShouldBe(type);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -7,6 +7,9 @@ using Ocelot.ServiceDiscovery;
 | 
			
		||||
using Ocelot.ServiceDiscovery.Providers;
 | 
			
		||||
using Shouldly;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Threading.Tasks;
 | 
			
		||||
using Ocelot.Middleware;
 | 
			
		||||
using Ocelot.Values;
 | 
			
		||||
using TestStack.BDDfy;
 | 
			
		||||
using Xunit;
 | 
			
		||||
 | 
			
		||||
@@ -18,6 +21,7 @@ namespace Ocelot.UnitTests.LoadBalancer
 | 
			
		||||
        private readonly LoadBalancerFactory _factory;
 | 
			
		||||
        private Response<ILoadBalancer> _result;
 | 
			
		||||
        private readonly Mock<IServiceDiscoveryProviderFactory> _serviceProviderFactory;
 | 
			
		||||
        private readonly IEnumerable<ILoadBalancerCreator> _loadBalancerCreators;
 | 
			
		||||
        private readonly Mock<IServiceDiscoveryProvider> _serviceProvider;
 | 
			
		||||
        private ServiceProviderConfiguration _serviceProviderConfig;
 | 
			
		||||
 | 
			
		||||
@@ -25,7 +29,13 @@ namespace Ocelot.UnitTests.LoadBalancer
 | 
			
		||||
        {
 | 
			
		||||
            _serviceProviderFactory = new Mock<IServiceDiscoveryProviderFactory>();
 | 
			
		||||
            _serviceProvider = new Mock<IServiceDiscoveryProvider>();
 | 
			
		||||
            _factory = new LoadBalancerFactory(_serviceProviderFactory.Object);
 | 
			
		||||
            _loadBalancerCreators = new ILoadBalancerCreator[]
 | 
			
		||||
            {
 | 
			
		||||
                new FakeLoadBalancerCreator<FakeLoadBalancerOne>(),
 | 
			
		||||
                new FakeLoadBalancerCreator<FakeLoadBalancerTwo>(),
 | 
			
		||||
                new FakeLoadBalancerCreator<FakeNoLoadBalancer>(nameof(NoLoadBalancer)),
 | 
			
		||||
            };
 | 
			
		||||
            _factory = new LoadBalancerFactory(_serviceProviderFactory.Object, _loadBalancerCreators);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
@@ -39,15 +49,15 @@ namespace Ocelot.UnitTests.LoadBalancer
 | 
			
		||||
                .And(x => GivenAServiceProviderConfig(new ServiceProviderConfigurationBuilder().Build()))
 | 
			
		||||
                .And(x => x.GivenTheServiceProviderFactoryReturns())
 | 
			
		||||
                .When(x => x.WhenIGetTheLoadBalancer())
 | 
			
		||||
                .Then(x => x.ThenTheLoadBalancerIsReturned<NoLoadBalancer>())
 | 
			
		||||
                .Then(x => x.ThenTheLoadBalancerIsReturned<FakeNoLoadBalancer>())
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_return_round_robin_load_balancer()
 | 
			
		||||
        public void should_return_matching_load_balancer()
 | 
			
		||||
        {
 | 
			
		||||
            var reRoute = new DownstreamReRouteBuilder()
 | 
			
		||||
                .WithLoadBalancerOptions(new LoadBalancerOptions("RoundRobin", "", 0))
 | 
			
		||||
                .WithLoadBalancerOptions(new LoadBalancerOptions("FakeLoadBalancerTwo", "", 0))
 | 
			
		||||
                .WithUpstreamHttpMethod(new List<string> { "Get" })
 | 
			
		||||
                .Build();
 | 
			
		||||
 | 
			
		||||
@@ -55,31 +65,15 @@ namespace Ocelot.UnitTests.LoadBalancer
 | 
			
		||||
                .And(x => GivenAServiceProviderConfig(new ServiceProviderConfigurationBuilder().Build()))
 | 
			
		||||
                .And(x => x.GivenTheServiceProviderFactoryReturns())
 | 
			
		||||
                .When(x => x.WhenIGetTheLoadBalancer())
 | 
			
		||||
                .Then(x => x.ThenTheLoadBalancerIsReturned<RoundRobin>())
 | 
			
		||||
                .Then(x => x.ThenTheLoadBalancerIsReturned<FakeLoadBalancerTwo>())
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_return_round_least_connection_balancer()
 | 
			
		||||
        {
 | 
			
		||||
            var reRoute = new DownstreamReRouteBuilder()
 | 
			
		||||
                .WithLoadBalancerOptions(new LoadBalancerOptions("LeastConnection", "", 0))
 | 
			
		||||
                .WithUpstreamHttpMethod(new List<string> { "Get" })
 | 
			
		||||
                .Build();
 | 
			
		||||
 | 
			
		||||
            this.Given(x => x.GivenAReRoute(reRoute))
 | 
			
		||||
                .And(x => GivenAServiceProviderConfig(new ServiceProviderConfigurationBuilder().Build()))
 | 
			
		||||
                .And(x => x.GivenTheServiceProviderFactoryReturns())
 | 
			
		||||
                .When(x => x.WhenIGetTheLoadBalancer())
 | 
			
		||||
                .Then(x => x.ThenTheLoadBalancerIsReturned<LeastConnection>())
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_call_service_provider()
 | 
			
		||||
        {
 | 
			
		||||
            var reRoute = new DownstreamReRouteBuilder()
 | 
			
		||||
                .WithLoadBalancerOptions(new LoadBalancerOptions("RoundRobin", "", 0))
 | 
			
		||||
                .WithLoadBalancerOptions(new LoadBalancerOptions("FakeLoadBalancerOne", "", 0))
 | 
			
		||||
                .WithUpstreamHttpMethod(new List<string> { "Get" })
 | 
			
		||||
                .Build();
 | 
			
		||||
 | 
			
		||||
@@ -91,22 +85,6 @@ namespace Ocelot.UnitTests.LoadBalancer
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_return_sticky_session()
 | 
			
		||||
        {
 | 
			
		||||
            var reRoute = new DownstreamReRouteBuilder()
 | 
			
		||||
                .WithLoadBalancerOptions(new LoadBalancerOptions("CookieStickySessions", "", 0))
 | 
			
		||||
                .WithUpstreamHttpMethod(new List<string> { "Get" })
 | 
			
		||||
                .Build();
 | 
			
		||||
 | 
			
		||||
            this.Given(x => x.GivenAReRoute(reRoute))
 | 
			
		||||
                .And(x => GivenAServiceProviderConfig(new ServiceProviderConfigurationBuilder().Build()))
 | 
			
		||||
                .And(x => x.GivenTheServiceProviderFactoryReturns())
 | 
			
		||||
                .When(x => x.WhenIGetTheLoadBalancer())
 | 
			
		||||
                .Then(x => x.ThenTheLoadBalancerIsReturned<CookieStickySessions>())
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void GivenAServiceProviderConfig(ServiceProviderConfiguration serviceProviderConfig)
 | 
			
		||||
        {
 | 
			
		||||
            _serviceProviderConfig = serviceProviderConfig;
 | 
			
		||||
@@ -139,5 +117,66 @@ namespace Ocelot.UnitTests.LoadBalancer
 | 
			
		||||
        {
 | 
			
		||||
            _result.Data.ShouldBeOfType<T>();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private class FakeLoadBalancerCreator<T> : ILoadBalancerCreator
 | 
			
		||||
            where T : ILoadBalancer, new()
 | 
			
		||||
        {
 | 
			
		||||
 | 
			
		||||
            public FakeLoadBalancerCreator()
 | 
			
		||||
            {
 | 
			
		||||
                Type = typeof(T).Name;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            public FakeLoadBalancerCreator(string type)
 | 
			
		||||
            {
 | 
			
		||||
                Type = type;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            public ILoadBalancer Create(DownstreamReRoute reRoute, IServiceDiscoveryProvider serviceProvider)
 | 
			
		||||
            {
 | 
			
		||||
                return new T();
 | 
			
		||||
            }
 | 
			
		||||
            
 | 
			
		||||
            public string Type { get; }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private class FakeLoadBalancerOne : ILoadBalancer
 | 
			
		||||
        {
 | 
			
		||||
            public Task<Response<ServiceHostAndPort>> Lease(DownstreamContext context)
 | 
			
		||||
            {
 | 
			
		||||
                throw new System.NotImplementedException();
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            public void Release(ServiceHostAndPort hostAndPort)
 | 
			
		||||
            {
 | 
			
		||||
                throw new System.NotImplementedException();
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private class FakeLoadBalancerTwo : ILoadBalancer
 | 
			
		||||
        {
 | 
			
		||||
            public Task<Response<ServiceHostAndPort>> Lease(DownstreamContext context)
 | 
			
		||||
            {
 | 
			
		||||
                throw new System.NotImplementedException();
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            public void Release(ServiceHostAndPort hostAndPort)
 | 
			
		||||
            {
 | 
			
		||||
                throw new System.NotImplementedException();
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private class FakeNoLoadBalancer : ILoadBalancer
 | 
			
		||||
        {
 | 
			
		||||
            public Task<Response<ServiceHostAndPort>> Lease(DownstreamContext context)
 | 
			
		||||
            {
 | 
			
		||||
                throw new System.NotImplementedException();
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            public void Release(ServiceHostAndPort hostAndPort)
 | 
			
		||||
            {
 | 
			
		||||
                throw new System.NotImplementedException();
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,72 @@
 | 
			
		||||
using Moq;
 | 
			
		||||
using Ocelot.Configuration;
 | 
			
		||||
using Ocelot.Configuration.Builder;
 | 
			
		||||
using Ocelot.LoadBalancer.LoadBalancers;
 | 
			
		||||
using Ocelot.ServiceDiscovery.Providers;
 | 
			
		||||
using Shouldly;
 | 
			
		||||
using TestStack.BDDfy;
 | 
			
		||||
using Xunit;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.UnitTests.LoadBalancer
 | 
			
		||||
{
 | 
			
		||||
    public class NoLoadBalancerCreatorTests
 | 
			
		||||
    {
 | 
			
		||||
        private readonly NoLoadBalancerCreator _creator;
 | 
			
		||||
        private readonly Mock<IServiceDiscoveryProvider> _serviceProvider;
 | 
			
		||||
        private DownstreamReRoute _reRoute;
 | 
			
		||||
        private ILoadBalancer _loadBalancer;
 | 
			
		||||
        private string _typeName;
 | 
			
		||||
 | 
			
		||||
        public NoLoadBalancerCreatorTests()
 | 
			
		||||
        {
 | 
			
		||||
            _creator = new NoLoadBalancerCreator();
 | 
			
		||||
            _serviceProvider = new Mock<IServiceDiscoveryProvider>();
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_return_expected_name()
 | 
			
		||||
        {
 | 
			
		||||
            var reRoute = new DownstreamReRouteBuilder()
 | 
			
		||||
                .Build();
 | 
			
		||||
 | 
			
		||||
            this.Given(x => x.GivenAReRoute(reRoute))
 | 
			
		||||
                .When(x => x.WhenIGetTheLoadBalancer())
 | 
			
		||||
                .Then(x => x.ThenTheLoadBalancerIsReturned<NoLoadBalancer>())
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
                
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_return_instance_of_expected_load_balancer_type()
 | 
			
		||||
        {
 | 
			
		||||
            this.When(x => x.WhenIGetTheLoadBalancerTypeName())
 | 
			
		||||
                .Then(x => x.ThenTheLoadBalancerTypeIs("NoLoadBalancer"))
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void GivenAReRoute(DownstreamReRoute reRoute)
 | 
			
		||||
        {
 | 
			
		||||
            _reRoute = reRoute;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void WhenIGetTheLoadBalancer()
 | 
			
		||||
        {
 | 
			
		||||
            _loadBalancer = _creator.Create(_reRoute, _serviceProvider.Object);
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        private void WhenIGetTheLoadBalancerTypeName()
 | 
			
		||||
        {
 | 
			
		||||
            _typeName = _creator.Type;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void ThenTheLoadBalancerIsReturned<T>()
 | 
			
		||||
            where T : ILoadBalancer
 | 
			
		||||
        {
 | 
			
		||||
            _loadBalancer.ShouldBeOfType<T>();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void ThenTheLoadBalancerTypeIs(string type)
 | 
			
		||||
        {
 | 
			
		||||
            _typeName.ShouldBe(type);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										72
									
								
								test/Ocelot.UnitTests/LoadBalancer/RoundRobinCreatorTests.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										72
									
								
								test/Ocelot.UnitTests/LoadBalancer/RoundRobinCreatorTests.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,72 @@
 | 
			
		||||
using Moq;
 | 
			
		||||
using Ocelot.Configuration;
 | 
			
		||||
using Ocelot.Configuration.Builder;
 | 
			
		||||
using Ocelot.LoadBalancer.LoadBalancers;
 | 
			
		||||
using Ocelot.ServiceDiscovery.Providers;
 | 
			
		||||
using Shouldly;
 | 
			
		||||
using TestStack.BDDfy;
 | 
			
		||||
using Xunit;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.UnitTests.LoadBalancer
 | 
			
		||||
{
 | 
			
		||||
    public class RoundRobinCreatorTests
 | 
			
		||||
    {
 | 
			
		||||
        private readonly RoundRobinCreator _creator;
 | 
			
		||||
        private readonly Mock<IServiceDiscoveryProvider> _serviceProvider;
 | 
			
		||||
        private DownstreamReRoute _reRoute;
 | 
			
		||||
        private ILoadBalancer _loadBalancer;
 | 
			
		||||
        private string _typeName;
 | 
			
		||||
 | 
			
		||||
        public RoundRobinCreatorTests()
 | 
			
		||||
        {
 | 
			
		||||
            _creator = new RoundRobinCreator();
 | 
			
		||||
            _serviceProvider = new Mock<IServiceDiscoveryProvider>();
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_return_expected_name()
 | 
			
		||||
        {
 | 
			
		||||
            var reRoute = new DownstreamReRouteBuilder()
 | 
			
		||||
                .Build();
 | 
			
		||||
 | 
			
		||||
            this.Given(x => x.GivenAReRoute(reRoute))
 | 
			
		||||
                .When(x => x.WhenIGetTheLoadBalancer())
 | 
			
		||||
                .Then(x => x.ThenTheLoadBalancerIsReturned<RoundRobin>())
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
                
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_return_instance_of_expected_load_balancer_type()
 | 
			
		||||
        {
 | 
			
		||||
            this.When(x => x.WhenIGetTheLoadBalancerTypeName())
 | 
			
		||||
                .Then(x => x.ThenTheLoadBalancerTypeIs("RoundRobin"))
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void GivenAReRoute(DownstreamReRoute reRoute)
 | 
			
		||||
        {
 | 
			
		||||
            _reRoute = reRoute;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void WhenIGetTheLoadBalancer()
 | 
			
		||||
        {
 | 
			
		||||
            _loadBalancer = _creator.Create(_reRoute, _serviceProvider.Object);
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        private void WhenIGetTheLoadBalancerTypeName()
 | 
			
		||||
        {
 | 
			
		||||
            _typeName = _creator.Type;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void ThenTheLoadBalancerIsReturned<T>()
 | 
			
		||||
            where T : ILoadBalancer
 | 
			
		||||
        {
 | 
			
		||||
            _loadBalancer.ShouldBeOfType<T>();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void ThenTheLoadBalancerTypeIs(string type)
 | 
			
		||||
        {
 | 
			
		||||
            _typeName.ShouldBe(type);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user