mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-04-22 14:02:49 +08:00
moving load balancer creation into its own class
This commit is contained in:
parent
1d1a68ff95
commit
bf0a31f8de
24
src/Ocelot/LoadBalancer/LoadBalancerCreator.cs
Normal file
24
src/Ocelot/LoadBalancer/LoadBalancerCreator.cs
Normal file
@ -0,0 +1,24 @@
|
||||
using System.Threading.Tasks;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.LoadBalancer.LoadBalancers;
|
||||
|
||||
namespace Ocelot.LoadBalancer
|
||||
{
|
||||
public class LoadBalancerCreator
|
||||
{
|
||||
private readonly ILoadBalancerHouse _loadBalancerHouse;
|
||||
private readonly ILoadBalancerFactory _loadBalanceFactory;
|
||||
|
||||
public LoadBalancerCreator(ILoadBalancerHouse loadBalancerHouse, ILoadBalancerFactory loadBalancerFactory)
|
||||
{
|
||||
_loadBalancerHouse = loadBalancerHouse;
|
||||
_loadBalanceFactory = loadBalancerFactory;
|
||||
}
|
||||
|
||||
public async Task SetupLoadBalancer(ReRoute reRoute)
|
||||
{
|
||||
var loadBalancer = await _loadBalanceFactory.Get(reRoute);
|
||||
_loadBalancerHouse.Add(reRoute.ReRouteKey, loadBalancer);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
using Xunit;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Ocelot.LoadBalancer;
|
||||
using Ocelot.LoadBalancer.LoadBalancers;
|
||||
using Moq;
|
||||
using Ocelot.Configuration;
|
||||
using System.Collections.Generic;
|
||||
using Ocelot.Values;
|
||||
using Ocelot.Configuration.Builder;
|
||||
|
||||
namespace Ocelot.UnitTests.LoadBalancer
|
||||
{
|
||||
public class LoadBalancerCreatorTests
|
||||
{
|
||||
private LoadBalancerCreator _creator;
|
||||
private ILoadBalancerHouse _house;
|
||||
private Mock<ILoadBalancerFactory> _factory;
|
||||
private ReRoute _reRoute;
|
||||
|
||||
public LoadBalancerCreatorTests()
|
||||
{
|
||||
_house = new LoadBalancerHouse();
|
||||
_factory = new Mock<ILoadBalancerFactory>();
|
||||
_creator = new LoadBalancerCreator(_house, _factory.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_create_load_balancer()
|
||||
{
|
||||
var reRoute = new ReRouteBuilder().WithLoadBalancerKey("Test").Build();
|
||||
this.Given(x => GivenTheFollowingReRoute(reRoute))
|
||||
.When(x => WhenICallTheCreator())
|
||||
.Then(x => x.ThenTheLoadBalancerIsCreated())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheFollowingReRoute(ReRoute reRoute)
|
||||
{
|
||||
_reRoute = reRoute;
|
||||
_factory
|
||||
.Setup(x => x.Get(It.IsAny<ReRoute>()))
|
||||
.ReturnsAsync(new NoLoadBalancer(new List<Service>()));
|
||||
}
|
||||
|
||||
private void WhenICallTheCreator()
|
||||
{
|
||||
_creator.SetupLoadBalancer(_reRoute).Wait();
|
||||
}
|
||||
|
||||
private void ThenTheLoadBalancerIsCreated()
|
||||
{
|
||||
var lb = _house.Get(_reRoute.ReRouteKey);
|
||||
lb.ShouldNotBeNull();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user