started adding loadbalancers

This commit is contained in:
TomPallister 2017-01-22 20:22:04 +00:00
parent 044b609ea9
commit 6bf2d4677c
3 changed files with 83 additions and 2 deletions

View File

@ -1 +1 @@
{"ReRoutes":[{"DownstreamTemplate":"http://localhost:41879/","UpstreamTemplate":"/","UpstreamHttpMethod":"Get","AuthenticationOptions":{"Provider":null,"ProviderRootUrl":null,"ScopeName":null,"RequireHttps":false,"AdditionalScopes":[],"ScopeSecret":null},"AddHeadersToRequest":{},"AddClaimsToRequest":{},"RouteClaimsRequirement":{},"AddQueriesToRequest":{},"RequestIdKey":null,"FileCacheOptions":{"TtlSeconds":0},"ReRouteIsCaseSensitive":false}],"GlobalConfiguration":{"RequestIdKey":null}} {"ReRoutes":[{"DownstreamPathTemplate":"/","UpstreamTemplate":"/","UpstreamHttpMethod":"Get","AuthenticationOptions":{"Provider":null,"ProviderRootUrl":null,"ScopeName":null,"RequireHttps":false,"AdditionalScopes":[],"ScopeSecret":null},"AddHeadersToRequest":{},"AddClaimsToRequest":{},"RouteClaimsRequirement":{},"AddQueriesToRequest":{},"RequestIdKey":null,"FileCacheOptions":{"TtlSeconds":0},"ReRouteIsCaseSensitive":false,"ServiceName":null,"DownstreamScheme":"http","DownstreamHost":"localhost","DownstreamPort":41879}],"GlobalConfiguration":{"RequestIdKey":null,"ServiceDiscoveryProvider":{"Provider":null,"Address":null}}}

View File

@ -0,0 +1,81 @@
using System.Collections.Generic;
using System.Diagnostics;
using Ocelot.Values;
using Shouldly;
using Xunit;
namespace Ocelot.UnitTests
{
public class RoundRobinTests
{
private readonly RoundRobin _roundRobin;
private readonly List<HostAndPort> _hostAndPorts;
public RoundRobinTests()
{
_hostAndPorts = new List<HostAndPort>
{
new HostAndPort("127.0.0.1", 5000),
new HostAndPort("127.0.0.1", 5001),
new HostAndPort("127.0.0.1", 5001)
};
_roundRobin = new RoundRobin(_hostAndPorts);
}
[Fact]
public void should_get_next_address()
{
var address = _roundRobin.Next();
address.ShouldBe(_hostAndPorts[0]);
address = _roundRobin.Next();
address.ShouldBe(_hostAndPorts[1]);
address = _roundRobin.Next();
address.ShouldBe(_hostAndPorts[2]);
}
[Fact]
public void should_go_back_to_first_address_after_finished_last()
{
var stopWatch = Stopwatch.StartNew();
while (stopWatch.ElapsedMilliseconds < 1000)
{
var address = _roundRobin.Next();
address.ShouldBe(_hostAndPorts[0]);
address = _roundRobin.Next();
address.ShouldBe(_hostAndPorts[1]);
address = _roundRobin.Next();
address.ShouldBe(_hostAndPorts[2]);
}
}
}
public interface ILoadBalancer
{
HostAndPort Next();
}
public class RoundRobin : ILoadBalancer
{
private readonly List<HostAndPort> _hostAndPorts;
private int _last;
public RoundRobin(List<HostAndPort> hostAndPorts)
{
_hostAndPorts = hostAndPorts;
}
public HostAndPort Next()
{
if (_last >= _hostAndPorts.Count)
{
_last = 0;
}
var next = _hostAndPorts[_last];
_last++;
return next;
}
}
}