started implementing the consul service provider

This commit is contained in:
TomPallister
2017-02-04 13:16:31 +00:00
parent 7900aa3f49
commit c46dcc05b8
41 changed files with 282 additions and 140 deletions

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Ocelot.LoadBalancer.LoadBalancers;
using Ocelot.Responses;
using Ocelot.Values;
@@ -24,7 +25,7 @@ namespace Ocelot.UnitTests.LoadBalancer
var availableServices = new List<Service>
{
new Service(serviceName, hostAndPort)
new Service(serviceName, hostAndPort, string.Empty, string.Empty, new string[0])
};
this.Given(x => x.GivenAHostAndPort(hostAndPort))
@@ -41,23 +42,23 @@ namespace Ocelot.UnitTests.LoadBalancer
var availableServices = new List<Service>
{
new Service(serviceName, new HostAndPort("127.0.0.1", 80)),
new Service(serviceName, new HostAndPort("127.0.0.2", 80)),
new Service(serviceName, new HostAndPort("127.0.0.3", 80))
new Service(serviceName, new HostAndPort("127.0.0.1", 80), string.Empty, string.Empty, new string[0]),
new Service(serviceName, new HostAndPort("127.0.0.2", 80), string.Empty, string.Empty, new string[0]),
new Service(serviceName, new HostAndPort("127.0.0.3", 80), string.Empty, string.Empty, new string[0])
};
_services = availableServices;
_leastConnection = new LeastConnectionLoadBalancer(() => _services, serviceName);
_leastConnection = new LeastConnectionLoadBalancer(() => Task.FromResult(_services), serviceName);
var response = _leastConnection.Lease();
var response = _leastConnection.Lease().Result;
response.Data.DownstreamHost.ShouldBe(availableServices[0].HostAndPort.DownstreamHost);
response = _leastConnection.Lease();
response = _leastConnection.Lease().Result;
response.Data.DownstreamHost.ShouldBe(availableServices[1].HostAndPort.DownstreamHost);
response = _leastConnection.Lease();
response = _leastConnection.Lease().Result;
response.Data.DownstreamHost.ShouldBe(availableServices[2].HostAndPort.DownstreamHost);
}
@@ -69,26 +70,26 @@ namespace Ocelot.UnitTests.LoadBalancer
var availableServices = new List<Service>
{
new Service(serviceName, new HostAndPort("127.0.0.1", 80)),
new Service(serviceName, new HostAndPort("127.0.0.2", 80)),
new Service(serviceName, new HostAndPort("127.0.0.1", 80), string.Empty, string.Empty, new string[0]),
new Service(serviceName, new HostAndPort("127.0.0.2", 80), string.Empty, string.Empty, new string[0]),
};
_services = availableServices;
_leastConnection = new LeastConnectionLoadBalancer(() => _services, serviceName);
_leastConnection = new LeastConnectionLoadBalancer(() => Task.FromResult(_services), serviceName);
var response = _leastConnection.Lease();
var response = _leastConnection.Lease().Result;
response.Data.DownstreamHost.ShouldBe(availableServices[0].HostAndPort.DownstreamHost);
response = _leastConnection.Lease();
response = _leastConnection.Lease().Result;
response.Data.DownstreamHost.ShouldBe(availableServices[1].HostAndPort.DownstreamHost);
response = _leastConnection.Lease();
response = _leastConnection.Lease().Result;
response.Data.DownstreamHost.ShouldBe(availableServices[0].HostAndPort.DownstreamHost);
response = _leastConnection.Lease();
response = _leastConnection.Lease().Result;
response.Data.DownstreamHost.ShouldBe(availableServices[1].HostAndPort.DownstreamHost);
}
@@ -100,33 +101,33 @@ namespace Ocelot.UnitTests.LoadBalancer
var availableServices = new List<Service>
{
new Service(serviceName, new HostAndPort("127.0.0.1", 80)),
new Service(serviceName, new HostAndPort("127.0.0.2", 80)),
new Service(serviceName, new HostAndPort("127.0.0.1", 80), string.Empty, string.Empty, new string[0]),
new Service(serviceName, new HostAndPort("127.0.0.2", 80), string.Empty, string.Empty, new string[0]),
};
_services = availableServices;
_leastConnection = new LeastConnectionLoadBalancer(() => _services, serviceName);
_leastConnection = new LeastConnectionLoadBalancer(() => Task.FromResult(_services), serviceName);
var response = _leastConnection.Lease();
var response = _leastConnection.Lease().Result;
response.Data.DownstreamHost.ShouldBe(availableServices[0].HostAndPort.DownstreamHost);
response = _leastConnection.Lease();
response = _leastConnection.Lease().Result;
response.Data.DownstreamHost.ShouldBe(availableServices[1].HostAndPort.DownstreamHost);
response = _leastConnection.Lease();
response = _leastConnection.Lease().Result;
response.Data.DownstreamHost.ShouldBe(availableServices[0].HostAndPort.DownstreamHost);
response = _leastConnection.Lease();
response = _leastConnection.Lease().Result;
response.Data.DownstreamHost.ShouldBe(availableServices[1].HostAndPort.DownstreamHost);
//release this so 2 should have 1 connection and we should get 2 back as our next host and port
_leastConnection.Release(availableServices[1].HostAndPort);
response = _leastConnection.Lease();
response = _leastConnection.Lease().Result;
response.Data.DownstreamHost.ShouldBe(availableServices[1].HostAndPort.DownstreamHost);
}
@@ -172,7 +173,7 @@ namespace Ocelot.UnitTests.LoadBalancer
private void GivenTheLoadBalancerStarts(List<Service> services, string serviceName)
{
_services = services;
_leastConnection = new LeastConnectionLoadBalancer(() => _services, serviceName);
_leastConnection = new LeastConnectionLoadBalancer(() => Task.FromResult(_services), serviceName);
}
private void WhenTheLoadBalancerStarts(List<Service> services, string serviceName)
@@ -187,7 +188,7 @@ namespace Ocelot.UnitTests.LoadBalancer
private void WhenIGetTheNextHostAndPort()
{
_result = _leastConnection.Lease();
_result = _leastConnection.Lease().Result;
}
private void ThenTheNextHostAndPortIsReturned()

View File

@@ -99,7 +99,7 @@ namespace Ocelot.UnitTests.LoadBalancer
private void WhenIGetTheLoadBalancer()
{
_result = _factory.Get(_reRoute);
_result = _factory.Get(_reRoute).Result;
}
private void ThenTheLoadBalancerIsReturned<T>()

View File

@@ -1,4 +1,5 @@
using System;
using System.Threading.Tasks;
using Ocelot.LoadBalancer.LoadBalancers;
using Ocelot.Responses;
using Ocelot.Values;
@@ -108,7 +109,7 @@ namespace Ocelot.UnitTests.LoadBalancer
class FakeLoadBalancer : ILoadBalancer
{
public Response<HostAndPort> Lease()
public Task<Response<HostAndPort>> Lease()
{
throw new NotImplementedException();
}
@@ -121,7 +122,7 @@ namespace Ocelot.UnitTests.LoadBalancer
class FakeRoundRobinLoadBalancer : ILoadBalancer
{
public Response<HostAndPort> Lease()
public Task<Response<HostAndPort>> Lease()
{
throw new NotImplementedException();
}

View File

@@ -82,7 +82,7 @@ namespace Ocelot.UnitTests.LoadBalancer
_hostAndPort = new HostAndPort("127.0.0.1", 80);
_loadBalancer
.Setup(x => x.Lease())
.Returns(new OkResponse<HostAndPort>(_hostAndPort));
.ReturnsAsync(new OkResponse<HostAndPort>(_hostAndPort));
}
private void GivenTheDownStreamRouteIs(DownstreamRoute downstreamRoute)

View File

@@ -21,7 +21,7 @@ namespace Ocelot.UnitTests.LoadBalancer
var services = new List<Service>
{
new Service("product", hostAndPort)
new Service("product", hostAndPort, string.Empty, string.Empty, new string[0])
};
this.Given(x => x.GivenServices(services))
.When(x => x.WhenIGetTheNextHostAndPort())
@@ -37,7 +37,7 @@ namespace Ocelot.UnitTests.LoadBalancer
private void WhenIGetTheNextHostAndPort()
{
_loadBalancer = new NoLoadBalancer(_services);
_result = _loadBalancer.Lease();
_result = _loadBalancer.Lease().Result;
}
private void ThenTheHostAndPortIs(HostAndPort expected)

View File

@@ -19,9 +19,9 @@ namespace Ocelot.UnitTests.LoadBalancer
{
_services = new List<Service>
{
new Service("product", new HostAndPort("127.0.0.1", 5000)),
new Service("product", new HostAndPort("127.0.0.1", 5001)),
new Service("product", new HostAndPort("127.0.0.1", 5001))
new Service("product", new HostAndPort("127.0.0.1", 5000), string.Empty, string.Empty, new string[0]),
new Service("product", new HostAndPort("127.0.0.1", 5001), string.Empty, string.Empty, new string[0]),
new Service("product", new HostAndPort("127.0.0.1", 5001), string.Empty, string.Empty, new string[0])
};
_roundRobin = new RoundRobinLoadBalancer(_services);
@@ -46,18 +46,18 @@ namespace Ocelot.UnitTests.LoadBalancer
while (stopWatch.ElapsedMilliseconds < 1000)
{
var address = _roundRobin.Lease();
var address = _roundRobin.Lease().Result;
address.Data.ShouldBe(_services[0].HostAndPort);
address = _roundRobin.Lease();
address = _roundRobin.Lease().Result;
address.Data.ShouldBe(_services[1].HostAndPort);
address = _roundRobin.Lease();
address = _roundRobin.Lease().Result;
address.Data.ShouldBe(_services[2].HostAndPort);
}
}
private void GivenIGetTheNextAddress()
{
_hostAndPort = _roundRobin.Lease();
_hostAndPort = _roundRobin.Lease().Result;
}
private void ThenTheNextAddressIndexIs(int index)