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