From 6bf2d4677cfa2ccd44f0b72ea4df9de11b06204f Mon Sep 17 00:00:00 2001 From: TomPallister Date: Sun, 22 Jan 2017 20:22:04 +0000 Subject: [PATCH] started adding loadbalancers --- push-to-nuget.bat | 2 +- .../Ocelot.AcceptanceTests/configuration.json | 2 +- test/Ocelot.UnitTests/RoundRobinTests.cs | 81 +++++++++++++++++++ 3 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 test/Ocelot.UnitTests/RoundRobinTests.cs diff --git a/push-to-nuget.bat b/push-to-nuget.bat index 7200d09c..73d3bf7e 100644 --- a/push-to-nuget.bat +++ b/push-to-nuget.bat @@ -4,7 +4,7 @@ echo Packing Ocelot Version %1 nuget pack .\Ocelot.nuspec -version %1 echo Publishing Ocelot - nuget push Ocelot.%1.nupkg -ApiKey %2 -Source https://www.nuget.org/api/v2/package +nuget push Ocelot.%1.nupkg -ApiKey %2 -Source https://www.nuget.org/api/v2/package diff --git a/test/Ocelot.AcceptanceTests/configuration.json b/test/Ocelot.AcceptanceTests/configuration.json index 984f851c..f28abefe 100755 --- a/test/Ocelot.AcceptanceTests/configuration.json +++ b/test/Ocelot.AcceptanceTests/configuration.json @@ -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}} \ No newline at end of file +{"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}}} \ No newline at end of file diff --git a/test/Ocelot.UnitTests/RoundRobinTests.cs b/test/Ocelot.UnitTests/RoundRobinTests.cs new file mode 100644 index 00000000..82689b62 --- /dev/null +++ b/test/Ocelot.UnitTests/RoundRobinTests.cs @@ -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 _hostAndPorts; + + public RoundRobinTests() + { + _hostAndPorts = new List + { + 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 _hostAndPorts; + private int _last; + + public RoundRobin(List hostAndPorts) + { + _hostAndPorts = hostAndPorts; + } + + public HostAndPort Next() + { + if (_last >= _hostAndPorts.Count) + { + _last = 0; + } + + var next = _hostAndPorts[_last]; + _last++; + return next; + } + } +}