mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-19 15:28:16 +08:00
playing around with lb factory
This commit is contained in:
95
test/Ocelot.UnitTests/LoadBalancerFactoryTests.cs
Normal file
95
test/Ocelot.UnitTests/LoadBalancerFactoryTests.cs
Normal file
@ -0,0 +1,95 @@
|
||||
using System;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.Responses;
|
||||
using Ocelot.Values;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests
|
||||
{
|
||||
public class LoadBalancerFactoryTests
|
||||
{
|
||||
private ReRoute _reRoute;
|
||||
private LoadBalancerFactory _factory;
|
||||
private ILoadBalancer _result;
|
||||
|
||||
public LoadBalancerFactoryTests()
|
||||
{
|
||||
_factory = new LoadBalancerFactory();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_no_load_balancer()
|
||||
{
|
||||
var reRoute = new ReRouteBuilder()
|
||||
.Build();
|
||||
|
||||
this.Given(x => x.GivenAReRoute(reRoute))
|
||||
.When(x => x.WhenIGetTheLoadBalancer())
|
||||
.Then(x => x.ThenTheLoadBalancerIsReturned<NoLoadBalancer>())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_round_robin_load_balancer()
|
||||
{
|
||||
var reRoute = new ReRouteBuilder()
|
||||
.WithLoadBalancer("RoundRobin")
|
||||
.Build();
|
||||
|
||||
this.Given(x => x.GivenAReRoute(reRoute))
|
||||
.When(x => x.WhenIGetTheLoadBalancer())
|
||||
.Then(x => x.ThenTheLoadBalancerIsReturned<RoundRobinLoadBalancer>())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenAReRoute(ReRoute reRoute)
|
||||
{
|
||||
_reRoute = reRoute;
|
||||
}
|
||||
|
||||
private void WhenIGetTheLoadBalancer()
|
||||
{
|
||||
_result = _factory.Get(_reRoute);
|
||||
}
|
||||
|
||||
private void ThenTheLoadBalancerIsReturned<T>()
|
||||
{
|
||||
_result.ShouldBeOfType<T>();
|
||||
}
|
||||
}
|
||||
|
||||
public class NoLoadBalancer : ILoadBalancer
|
||||
{
|
||||
Response<HostAndPort> ILoadBalancer.Lease()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
Response ILoadBalancer.Release(HostAndPort hostAndPort)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
public interface ILoadBalancerFactory
|
||||
{
|
||||
ILoadBalancer Get(ReRoute reRoute);
|
||||
}
|
||||
|
||||
public class LoadBalancerFactory : ILoadBalancerFactory
|
||||
{
|
||||
public ILoadBalancer Get(ReRoute reRoute)
|
||||
{
|
||||
switch (reRoute.LoadBalancer)
|
||||
{
|
||||
case "RoundRobin":
|
||||
return new RoundRobinLoadBalancer(null);
|
||||
default:
|
||||
return new NoLoadBalancer();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -10,7 +10,7 @@ namespace Ocelot.UnitTests
|
||||
{
|
||||
public class RoundRobinTests
|
||||
{
|
||||
private readonly RoundRobin _roundRobin;
|
||||
private readonly RoundRobinLoadBalancer _roundRobin;
|
||||
private readonly List<HostAndPort> _hostAndPorts;
|
||||
private Response<HostAndPort> _hostAndPort;
|
||||
|
||||
@ -23,7 +23,7 @@ namespace Ocelot.UnitTests
|
||||
new HostAndPort("127.0.0.1", 5001)
|
||||
};
|
||||
|
||||
_roundRobin = new RoundRobin(_hostAndPorts);
|
||||
_roundRobin = new RoundRobinLoadBalancer(_hostAndPorts);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@ -71,12 +71,12 @@ namespace Ocelot.UnitTests
|
||||
Response Release(HostAndPort hostAndPort);
|
||||
}
|
||||
|
||||
public class RoundRobin : ILoadBalancer
|
||||
public class RoundRobinLoadBalancer : ILoadBalancer
|
||||
{
|
||||
private readonly List<HostAndPort> _hostAndPorts;
|
||||
private int _last;
|
||||
|
||||
public RoundRobin(List<HostAndPort> hostAndPorts)
|
||||
public RoundRobinLoadBalancer(List<HostAndPort> hostAndPorts)
|
||||
{
|
||||
_hostAndPorts = hostAndPorts;
|
||||
}
|
||||
|
Reference in New Issue
Block a user