moving load balancer creation into its own class

This commit is contained in:
Tom Gardham-Pallister 2017-11-07 08:05:41 +00:00
parent 1d1a68ff95
commit bf0a31f8de
3 changed files with 82 additions and 1 deletions

View 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);
}
}
}

View File

@ -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();
}
}
}