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

@ -151,7 +151,7 @@ namespace Ocelot.UnitTests.Configuration
ServiceDiscoveryProvider = new FileServiceDiscoveryProvider
{
Provider = "consul",
Address = "127.0.0.1"
Host = "127.0.0.1"
}
}
}))
@ -579,7 +579,7 @@ namespace Ocelot.UnitTests.Configuration
private void WhenICreateTheConfig()
{
_config = _ocelotConfigurationCreator.Create();
_config = _ocelotConfigurationCreator.Create().Result;
}
private void ThenTheReRoutesAre(List<ReRoute> expectedReRoutes)
@ -617,7 +617,7 @@ namespace Ocelot.UnitTests.Configuration
{
_loadBalancerFactory
.Setup(x => x.Get(It.IsAny<ReRoute>()))
.Returns(_loadBalancer.Object);
.ReturnsAsync(_loadBalancer.Object);
}
private void TheLoadBalancerFactoryIsCalledCorrectly()

View File

@ -81,7 +81,7 @@ namespace Ocelot.UnitTests.Configuration
{
_creator
.Setup(x => x.Create())
.Returns(config);
.ReturnsAsync(config);
}
private void GivenTheRepoReturns(Response<IOcelotConfiguration> config)
@ -93,7 +93,7 @@ namespace Ocelot.UnitTests.Configuration
private void WhenIGetTheConfig()
{
_result = _ocelotConfigurationProvider.Get();
_result = _ocelotConfigurationProvider.Get().Result;
}
private void TheFollowingIsReturned(Response<IOcelotConfiguration> expected)

View File

@ -84,7 +84,7 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder
_downstreamRoute = new OkResponse<DownstreamRoute>(downstreamRoute);
_downstreamRouteFinder
.Setup(x => x.FindDownstreamRoute(It.IsAny<string>(), It.IsAny<string>()))
.Returns(_downstreamRoute);
.ReturnsAsync(_downstreamRoute);
}
public void Dispose()

View File

@ -159,7 +159,7 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder
_reRoutesConfig = reRoutesConfig;
_mockConfig
.Setup(x => x.Get())
.Returns(new OkResponse<IOcelotConfiguration>(new OcelotConfiguration(_reRoutesConfig)));
.ReturnsAsync(new OkResponse<IOcelotConfiguration>(new OcelotConfiguration(_reRoutesConfig)));
}
private void GivenThereIsAnUpstreamUrlPath(string upstreamUrlPath)
@ -169,7 +169,7 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder
private void WhenICallTheFinder()
{
_result = _downstreamRouteFinder.FindDownstreamRoute(_upstreamUrlPath, _upstreamHttpMethod);
_result = _downstreamRouteFinder.FindDownstreamRoute(_upstreamUrlPath, _upstreamHttpMethod).Result;
}
private void ThenTheFollowingIsReturned(DownstreamRoute expected)

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)

View File

@ -21,7 +21,7 @@ namespace Ocelot.UnitTests.ServiceDiscovery
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))
@ -38,7 +38,7 @@ namespace Ocelot.UnitTests.ServiceDiscovery
private void WhenIGetTheService()
{
_serviceProvider = new ConfigurationServiceProvider(_expected);
_result = _serviceProvider.Get();
_result = _serviceProvider.Get().Result;
}
private void ThenTheFollowingIsReturned(List<Service> services)

View File

@ -1,3 +1,4 @@
using Ocelot.Configuration;
using Ocelot.ServiceDiscovery;
using Shouldly;
using TestStack.BDDfy;
@ -19,7 +20,7 @@ namespace Ocelot.UnitTests.ServiceDiscovery
[Fact]
public void should_return_no_service_provider()
{
var serviceConfig = new ServiceProviderConfiguraion("product", "127.0.0.1", 80, false, "Does not matter");
var serviceConfig = new ServiceProviderConfiguraion("product", "127.0.0.1", 80, false, "Does not matter", string.Empty, 0);
this.Given(x => x.GivenTheReRoute(serviceConfig))
.When(x => x.WhenIGetTheServiceProvider())
@ -30,7 +31,7 @@ namespace Ocelot.UnitTests.ServiceDiscovery
[Fact]
public void should_return_consul_service_provider()
{
var serviceConfig = new ServiceProviderConfiguraion("product", string.Empty, 0, true, "Consul");
var serviceConfig = new ServiceProviderConfiguraion("product", string.Empty, 0, true, "Consul", string.Empty, 0);
this.Given(x => x.GivenTheReRoute(serviceConfig))
.When(x => x.WhenIGetTheServiceProvider())

View File

@ -50,13 +50,13 @@ namespace Ocelot.UnitTests.ServiceDiscovery
private void GivenAServiceIsRegistered(string name, string address, int port)
{
_service = new Service(name, new HostAndPort(address, port));
_service = new Service(name, new HostAndPort(address, port), string.Empty, string.Empty, new string[0]);
_serviceRepository.Set(_service);
}
private void GivenAServiceToRegister(string name, string address, int port)
{
_service = new Service(name, new HostAndPort(address, port));
_service = new Service(name, new HostAndPort(address, port), string.Empty, string.Empty, new string[0]);
}
private void WhenIRegisterTheService()