mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-04-22 06:22:50 +08:00
plugged new lb creator into tests
This commit is contained in:
parent
bf0a31f8de
commit
ab14df9127
@ -9,6 +9,7 @@ using Ocelot.Configuration.Builder;
|
|||||||
using Ocelot.Configuration.File;
|
using Ocelot.Configuration.File;
|
||||||
using Ocelot.Configuration.Parser;
|
using Ocelot.Configuration.Parser;
|
||||||
using Ocelot.Configuration.Validator;
|
using Ocelot.Configuration.Validator;
|
||||||
|
using Ocelot.LoadBalancer;
|
||||||
using Ocelot.LoadBalancer.LoadBalancers;
|
using Ocelot.LoadBalancer.LoadBalancers;
|
||||||
using Ocelot.Logging;
|
using Ocelot.Logging;
|
||||||
using Ocelot.Requester.QoS;
|
using Ocelot.Requester.QoS;
|
||||||
@ -25,8 +26,7 @@ namespace Ocelot.Configuration.Creator
|
|||||||
private readonly IOptions<FileConfiguration> _options;
|
private readonly IOptions<FileConfiguration> _options;
|
||||||
private readonly IConfigurationValidator _configurationValidator;
|
private readonly IConfigurationValidator _configurationValidator;
|
||||||
private readonly IOcelotLogger _logger;
|
private readonly IOcelotLogger _logger;
|
||||||
private readonly ILoadBalancerFactory _loadBalanceFactory;
|
private readonly ILoadBalancerCreator _lbCreator;
|
||||||
private readonly ILoadBalancerHouse _loadBalancerHouse;
|
|
||||||
private readonly IQoSProviderFactory _qoSProviderFactory;
|
private readonly IQoSProviderFactory _qoSProviderFactory;
|
||||||
private readonly IQosProviderHouse _qosProviderHouse;
|
private readonly IQosProviderHouse _qosProviderHouse;
|
||||||
private readonly IClaimsToThingCreator _claimsToThingCreator;
|
private readonly IClaimsToThingCreator _claimsToThingCreator;
|
||||||
@ -44,8 +44,7 @@ namespace Ocelot.Configuration.Creator
|
|||||||
IOptions<FileConfiguration> options,
|
IOptions<FileConfiguration> options,
|
||||||
IConfigurationValidator configurationValidator,
|
IConfigurationValidator configurationValidator,
|
||||||
IOcelotLoggerFactory loggerFactory,
|
IOcelotLoggerFactory loggerFactory,
|
||||||
ILoadBalancerFactory loadBalancerFactory,
|
ILoadBalancerCreator lbCreator,
|
||||||
ILoadBalancerHouse loadBalancerHouse,
|
|
||||||
IQoSProviderFactory qoSProviderFactory,
|
IQoSProviderFactory qoSProviderFactory,
|
||||||
IQosProviderHouse qosProviderHouse,
|
IQosProviderHouse qosProviderHouse,
|
||||||
IClaimsToThingCreator claimsToThingCreator,
|
IClaimsToThingCreator claimsToThingCreator,
|
||||||
@ -60,13 +59,12 @@ namespace Ocelot.Configuration.Creator
|
|||||||
IHttpHandlerOptionsCreator httpHandlerOptionsCreator
|
IHttpHandlerOptionsCreator httpHandlerOptionsCreator
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
|
_lbCreator = lbCreator;
|
||||||
_regionCreator = regionCreator;
|
_regionCreator = regionCreator;
|
||||||
_rateLimitOptionsCreator = rateLimitOptionsCreator;
|
_rateLimitOptionsCreator = rateLimitOptionsCreator;
|
||||||
_requestIdKeyCreator = requestIdKeyCreator;
|
_requestIdKeyCreator = requestIdKeyCreator;
|
||||||
_upstreamTemplatePatternCreator = upstreamTemplatePatternCreator;
|
_upstreamTemplatePatternCreator = upstreamTemplatePatternCreator;
|
||||||
_authOptionsCreator = authOptionsCreator;
|
_authOptionsCreator = authOptionsCreator;
|
||||||
_loadBalanceFactory = loadBalancerFactory;
|
|
||||||
_loadBalancerHouse = loadBalancerHouse;
|
|
||||||
_qoSProviderFactory = qoSProviderFactory;
|
_qoSProviderFactory = qoSProviderFactory;
|
||||||
_qosProviderHouse = qosProviderHouse;
|
_qosProviderHouse = qosProviderHouse;
|
||||||
_options = options;
|
_options = options;
|
||||||
@ -176,7 +174,7 @@ namespace Ocelot.Configuration.Creator
|
|||||||
.WithHttpHandlerOptions(httpHandlerOptions)
|
.WithHttpHandlerOptions(httpHandlerOptions)
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
await SetupLoadBalancer(reRoute);
|
await _lbCreator.SetupLoadBalancer(reRoute);
|
||||||
SetupQosProvider(reRoute);
|
SetupQosProvider(reRoute);
|
||||||
return reRoute;
|
return reRoute;
|
||||||
}
|
}
|
||||||
@ -188,12 +186,6 @@ namespace Ocelot.Configuration.Creator
|
|||||||
return loadBalancerKey;
|
return loadBalancerKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task SetupLoadBalancer(ReRoute reRoute)
|
|
||||||
{
|
|
||||||
var loadBalancer = await _loadBalanceFactory.Get(reRoute);
|
|
||||||
_loadBalancerHouse.Add(reRoute.ReRouteKey, loadBalancer);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void SetupQosProvider(ReRoute reRoute)
|
private void SetupQosProvider(ReRoute reRoute)
|
||||||
{
|
{
|
||||||
var loadBalancer = _qoSProviderFactory.Get(reRoute);
|
var loadBalancer = _qoSProviderFactory.Get(reRoute);
|
||||||
|
@ -46,6 +46,7 @@ using Microsoft.AspNetCore.Builder;
|
|||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Ocelot.Configuration;
|
using Ocelot.Configuration;
|
||||||
using FileConfigurationProvider = Ocelot.Configuration.Provider.FileConfigurationProvider;
|
using FileConfigurationProvider = Ocelot.Configuration.Provider.FileConfigurationProvider;
|
||||||
|
using Ocelot.LoadBalancer;
|
||||||
|
|
||||||
namespace Ocelot.DependencyInjection
|
namespace Ocelot.DependencyInjection
|
||||||
{
|
{
|
||||||
@ -93,6 +94,7 @@ namespace Ocelot.DependencyInjection
|
|||||||
.AddJsonFormatters();
|
.AddJsonFormatters();
|
||||||
|
|
||||||
services.AddLogging();
|
services.AddLogging();
|
||||||
|
services.TryAddSingleton<ILoadBalancerCreator, LoadBalancerCreator>();
|
||||||
services.TryAddSingleton<IRegionCreator, RegionCreator>();
|
services.TryAddSingleton<IRegionCreator, RegionCreator>();
|
||||||
services.TryAddSingleton<IFileConfigurationRepository, FileConfigurationRepository>();
|
services.TryAddSingleton<IFileConfigurationRepository, FileConfigurationRepository>();
|
||||||
services.TryAddSingleton<IFileConfigurationSetter, FileConfigurationSetter>();
|
services.TryAddSingleton<IFileConfigurationSetter, FileConfigurationSetter>();
|
||||||
|
10
src/Ocelot/LoadBalancer/ILoadBalancerCreator.cs
Normal file
10
src/Ocelot/LoadBalancer/ILoadBalancerCreator.cs
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
using System.Threading.Tasks;
|
||||||
|
using Ocelot.Configuration;
|
||||||
|
|
||||||
|
namespace Ocelot.LoadBalancer
|
||||||
|
{
|
||||||
|
public interface ILoadBalancerCreator
|
||||||
|
{
|
||||||
|
Task SetupLoadBalancer(ReRoute reRoute);
|
||||||
|
}
|
||||||
|
}
|
@ -4,7 +4,7 @@ using Ocelot.LoadBalancer.LoadBalancers;
|
|||||||
|
|
||||||
namespace Ocelot.LoadBalancer
|
namespace Ocelot.LoadBalancer
|
||||||
{
|
{
|
||||||
public class LoadBalancerCreator
|
public class LoadBalancerCreator : ILoadBalancerCreator
|
||||||
{
|
{
|
||||||
private readonly ILoadBalancerHouse _loadBalancerHouse;
|
private readonly ILoadBalancerHouse _loadBalancerHouse;
|
||||||
private readonly ILoadBalancerFactory _loadBalanceFactory;
|
private readonly ILoadBalancerFactory _loadBalanceFactory;
|
||||||
|
@ -19,7 +19,7 @@ using Xunit;
|
|||||||
namespace Ocelot.UnitTests.Configuration
|
namespace Ocelot.UnitTests.Configuration
|
||||||
{
|
{
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
|
using Ocelot.LoadBalancer;
|
||||||
using Ocelot.UnitTests.TestData;
|
using Ocelot.UnitTests.TestData;
|
||||||
|
|
||||||
public class FileConfigurationCreatorTests
|
public class FileConfigurationCreatorTests
|
||||||
@ -30,8 +30,6 @@ namespace Ocelot.UnitTests.Configuration
|
|||||||
private FileConfiguration _fileConfiguration;
|
private FileConfiguration _fileConfiguration;
|
||||||
private readonly Mock<IOcelotLoggerFactory> _logger;
|
private readonly Mock<IOcelotLoggerFactory> _logger;
|
||||||
private readonly FileOcelotConfigurationCreator _ocelotConfigurationCreator;
|
private readonly FileOcelotConfigurationCreator _ocelotConfigurationCreator;
|
||||||
private readonly Mock<ILoadBalancerFactory> _loadBalancerFactory;
|
|
||||||
private readonly Mock<ILoadBalancerHouse> _loadBalancerHouse;
|
|
||||||
private readonly Mock<ILoadBalancer> _loadBalancer;
|
private readonly Mock<ILoadBalancer> _loadBalancer;
|
||||||
private readonly Mock<IQoSProviderFactory> _qosProviderFactory;
|
private readonly Mock<IQoSProviderFactory> _qosProviderFactory;
|
||||||
private readonly Mock<IQosProviderHouse> _qosProviderHouse;
|
private readonly Mock<IQosProviderHouse> _qosProviderHouse;
|
||||||
@ -46,6 +44,7 @@ namespace Ocelot.UnitTests.Configuration
|
|||||||
private Mock<IRateLimitOptionsCreator> _rateLimitOptions;
|
private Mock<IRateLimitOptionsCreator> _rateLimitOptions;
|
||||||
private Mock<IRegionCreator> _regionCreator;
|
private Mock<IRegionCreator> _regionCreator;
|
||||||
private Mock<IHttpHandlerOptionsCreator> _httpHandlerOptionsCreator;
|
private Mock<IHttpHandlerOptionsCreator> _httpHandlerOptionsCreator;
|
||||||
|
private Mock<ILoadBalancerCreator> _lbCreator;
|
||||||
|
|
||||||
public FileConfigurationCreatorTests()
|
public FileConfigurationCreatorTests()
|
||||||
{
|
{
|
||||||
@ -55,8 +54,6 @@ namespace Ocelot.UnitTests.Configuration
|
|||||||
_logger = new Mock<IOcelotLoggerFactory>();
|
_logger = new Mock<IOcelotLoggerFactory>();
|
||||||
_validator = new Mock<IConfigurationValidator>();
|
_validator = new Mock<IConfigurationValidator>();
|
||||||
_fileConfig = new Mock<IOptions<FileConfiguration>>();
|
_fileConfig = new Mock<IOptions<FileConfiguration>>();
|
||||||
_loadBalancerFactory = new Mock<ILoadBalancerFactory>();
|
|
||||||
_loadBalancerHouse = new Mock<ILoadBalancerHouse>();
|
|
||||||
_loadBalancer = new Mock<ILoadBalancer>();
|
_loadBalancer = new Mock<ILoadBalancer>();
|
||||||
_claimsToThingCreator = new Mock<IClaimsToThingCreator>();
|
_claimsToThingCreator = new Mock<IClaimsToThingCreator>();
|
||||||
_authOptionsCreator = new Mock<IAuthenticationOptionsCreator>();
|
_authOptionsCreator = new Mock<IAuthenticationOptionsCreator>();
|
||||||
@ -68,10 +65,11 @@ namespace Ocelot.UnitTests.Configuration
|
|||||||
_rateLimitOptions = new Mock<IRateLimitOptionsCreator>();
|
_rateLimitOptions = new Mock<IRateLimitOptionsCreator>();
|
||||||
_regionCreator = new Mock<IRegionCreator>();
|
_regionCreator = new Mock<IRegionCreator>();
|
||||||
_httpHandlerOptionsCreator = new Mock<IHttpHandlerOptionsCreator>();
|
_httpHandlerOptionsCreator = new Mock<IHttpHandlerOptionsCreator>();
|
||||||
|
_lbCreator = new Mock<ILoadBalancerCreator>();
|
||||||
|
|
||||||
_ocelotConfigurationCreator = new FileOcelotConfigurationCreator(
|
_ocelotConfigurationCreator = new FileOcelotConfigurationCreator(
|
||||||
_fileConfig.Object, _validator.Object, _logger.Object,
|
_fileConfig.Object, _validator.Object, _logger.Object,
|
||||||
_loadBalancerFactory.Object, _loadBalancerHouse.Object,
|
_lbCreator.Object,
|
||||||
_qosProviderFactory.Object, _qosProviderHouse.Object, _claimsToThingCreator.Object,
|
_qosProviderFactory.Object, _qosProviderHouse.Object, _claimsToThingCreator.Object,
|
||||||
_authOptionsCreator.Object, _upstreamTemplatePatternCreator.Object, _requestIdKeyCreator.Object,
|
_authOptionsCreator.Object, _upstreamTemplatePatternCreator.Object, _requestIdKeyCreator.Object,
|
||||||
_serviceProviderConfigCreator.Object, _qosOptionsCreator.Object, _fileReRouteOptionsCreator.Object,
|
_serviceProviderConfigCreator.Object, _qosOptionsCreator.Object, _fileReRouteOptionsCreator.Object,
|
||||||
@ -212,10 +210,8 @@ namespace Ocelot.UnitTests.Configuration
|
|||||||
}))
|
}))
|
||||||
.And(x => x.GivenTheConfigIsValid())
|
.And(x => x.GivenTheConfigIsValid())
|
||||||
.And(x => x.GivenTheFollowingOptionsAreReturned(reRouteOptions))
|
.And(x => x.GivenTheFollowingOptionsAreReturned(reRouteOptions))
|
||||||
.And(x => x.GivenTheLoadBalancerFactoryReturns())
|
|
||||||
.When(x => x.WhenICreateTheConfig())
|
.When(x => x.WhenICreateTheConfig())
|
||||||
.Then(x => x.TheLoadBalancerFactoryIsCalledCorrectly())
|
.And(x => x.ThenTheLoadBalancerCreatorIsCalledCorrectly())
|
||||||
.And(x => x.ThenTheLoadBalancerHouseIsCalledCorrectly())
|
|
||||||
.BDDfy();
|
.BDDfy();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -516,7 +512,6 @@ namespace Ocelot.UnitTests.Configuration
|
|||||||
.And(x => x.GivenTheAuthOptionsCreatorReturns(authenticationOptions))
|
.And(x => x.GivenTheAuthOptionsCreatorReturns(authenticationOptions))
|
||||||
.And(x => x.GivenTheFollowingOptionsAreReturned(reRouteOptions))
|
.And(x => x.GivenTheFollowingOptionsAreReturned(reRouteOptions))
|
||||||
.And(x => x.GivenTheClaimsToThingCreatorReturns(new List<ClaimToThing> { new ClaimToThing("CustomerId", "CustomerId", "", 0) }))
|
.And(x => x.GivenTheClaimsToThingCreatorReturns(new List<ClaimToThing> { new ClaimToThing("CustomerId", "CustomerId", "", 0) }))
|
||||||
.And(x => x.GivenTheLoadBalancerFactoryReturns())
|
|
||||||
.When(x => x.WhenICreateTheConfig())
|
.When(x => x.WhenICreateTheConfig())
|
||||||
.Then(x => x.ThenTheReRoutesAre(expected))
|
.Then(x => x.ThenTheReRoutesAre(expected))
|
||||||
.And(x => x.ThenTheAuthenticationOptionsAre(expected))
|
.And(x => x.ThenTheAuthenticationOptionsAre(expected))
|
||||||
@ -550,7 +545,6 @@ namespace Ocelot.UnitTests.Configuration
|
|||||||
.And(x => x.GivenTheConfigIsValid())
|
.And(x => x.GivenTheConfigIsValid())
|
||||||
.And(x => x.GivenTheFollowingOptionsAreReturned(reRouteOptions))
|
.And(x => x.GivenTheFollowingOptionsAreReturned(reRouteOptions))
|
||||||
.And(x => x.GivenTheAuthOptionsCreatorReturns(authenticationOptions))
|
.And(x => x.GivenTheAuthOptionsCreatorReturns(authenticationOptions))
|
||||||
.And(x => x.GivenTheLoadBalancerFactoryReturns())
|
|
||||||
.When(x => x.WhenICreateTheConfig())
|
.When(x => x.WhenICreateTheConfig())
|
||||||
.Then(x => x.ThenTheReRoutesAre(expected))
|
.Then(x => x.ThenTheReRoutesAre(expected))
|
||||||
.And(x => x.ThenTheAuthenticationOptionsAre(expected))
|
.And(x => x.ThenTheAuthenticationOptionsAre(expected))
|
||||||
@ -634,23 +628,9 @@ namespace Ocelot.UnitTests.Configuration
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void GivenTheLoadBalancerFactoryReturns()
|
private void ThenTheLoadBalancerCreatorIsCalledCorrectly()
|
||||||
{
|
{
|
||||||
_loadBalancerFactory
|
_lbCreator.Verify(x => x.SetupLoadBalancer(It.IsAny<ReRoute>()), Times.Once);
|
||||||
.Setup(x => x.Get(It.IsAny<ReRoute>()))
|
|
||||||
.ReturnsAsync(_loadBalancer.Object);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void TheLoadBalancerFactoryIsCalledCorrectly()
|
|
||||||
{
|
|
||||||
_loadBalancerFactory
|
|
||||||
.Verify(x => x.Get(It.IsAny<ReRoute>()), Times.Once);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void ThenTheLoadBalancerHouseIsCalledCorrectly()
|
|
||||||
{
|
|
||||||
_loadBalancerHouse
|
|
||||||
.Verify(x => x.Add(It.IsAny<string>(), _loadBalancer.Object), Times.Once);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void GivenTheQosProviderFactoryReturns()
|
private void GivenTheQosProviderFactoryReturns()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user