hacked together load balancing reroutes in fileconfig (#211)

* hacked together load balancing reroutes in fileconfig

* some renaming and refactoring

* more renames

* hacked away the old config json

* test for issue 213

* renamed key

* dont share ports

* oops

* updated docs

* mvoed docs around

* port being used
This commit is contained in:
Tom Pallister
2018-01-31 20:34:55 +00:00
committed by GitHub
parent f572d1b0ca
commit 3ac9b3bd87
440 changed files with 29740 additions and 28464 deletions

View File

@ -1,52 +1,52 @@
using System.Collections.Generic;
using Ocelot.ServiceDiscovery;
using Ocelot.Values;
using Shouldly;
using TestStack.BDDfy;
using Xunit;
namespace Ocelot.UnitTests.ServiceDiscovery
{
public class ConfigurationServiceProviderTests
{
private ConfigurationServiceProvider _serviceProvider;
private List<Service> _result;
private List<Service> _expected;
[Fact]
public void should_return_services()
{
var hostAndPort = new HostAndPort("127.0.0.1", 80);
var services = new List<Service>
{
new Service("product", hostAndPort, string.Empty, string.Empty, new string[0])
};
this.Given(x => x.GivenServices(services))
.When(x => x.WhenIGetTheService())
.Then(x => x.ThenTheFollowingIsReturned(services))
.BDDfy();
}
private void GivenServices(List<Service> services)
{
_expected = services;
}
private void WhenIGetTheService()
{
_serviceProvider = new ConfigurationServiceProvider(_expected);
_result = _serviceProvider.Get().Result;
}
private void ThenTheFollowingIsReturned(List<Service> services)
{
_result[0].HostAndPort.DownstreamHost.ShouldBe(services[0].HostAndPort.DownstreamHost);
_result[0].HostAndPort.DownstreamPort.ShouldBe(services[0].HostAndPort.DownstreamPort);
_result[0].Name.ShouldBe(services[0].Name);
}
}
}
using System.Collections.Generic;
using Ocelot.ServiceDiscovery;
using Ocelot.Values;
using Shouldly;
using TestStack.BDDfy;
using Xunit;
namespace Ocelot.UnitTests.ServiceDiscovery
{
public class ConfigurationServiceProviderTests
{
private ConfigurationServiceProvider _serviceProvider;
private List<Service> _result;
private List<Service> _expected;
[Fact]
public void should_return_services()
{
var hostAndPort = new ServiceHostAndPort("127.0.0.1", 80);
var services = new List<Service>
{
new Service("product", hostAndPort, string.Empty, string.Empty, new string[0])
};
this.Given(x => x.GivenServices(services))
.When(x => x.WhenIGetTheService())
.Then(x => x.ThenTheFollowingIsReturned(services))
.BDDfy();
}
private void GivenServices(List<Service> services)
{
_expected = services;
}
private void WhenIGetTheService()
{
_serviceProvider = new ConfigurationServiceProvider(_expected);
_result = _serviceProvider.Get().Result;
}
private void ThenTheFollowingIsReturned(List<Service> services)
{
_result[0].HostAndPort.DownstreamHost.ShouldBe(services[0].HostAndPort.DownstreamHost);
_result[0].HostAndPort.DownstreamPort.ShouldBe(services[0].HostAndPort.DownstreamPort);
_result[0].Name.ShouldBe(services[0].Name);
}
}
}

View File

@ -1,69 +1,107 @@
using Ocelot.Configuration;
using Ocelot.Configuration.Builder;
using Ocelot.ServiceDiscovery;
using Shouldly;
using TestStack.BDDfy;
using Xunit;
namespace Ocelot.UnitTests.ServiceDiscovery
{
public class ServiceProviderFactoryTests
{
private ServiceProviderConfiguration _serviceConfig;
private IServiceDiscoveryProvider _result;
private readonly ServiceDiscoveryProviderFactory _factory;
private ReRoute _reRoute;
public ServiceProviderFactoryTests()
{
_factory = new ServiceDiscoveryProviderFactory();
}
[Fact]
public void should_return_no_service_provider()
{
var serviceConfig = new ServiceProviderConfigurationBuilder()
.Build();
var reRoute = new ReRouteBuilder().Build();
this.Given(x => x.GivenTheReRoute(serviceConfig, reRoute))
.When(x => x.WhenIGetTheServiceProvider())
.Then(x => x.ThenTheServiceProviderIs<ConfigurationServiceProvider>())
.BDDfy();
}
[Fact]
public void should_return_consul_service_provider()
{
var reRoute = new ReRouteBuilder()
.WithServiceName("product")
.WithUseServiceDiscovery(true)
.Build();
var serviceConfig = new ServiceProviderConfigurationBuilder()
.Build();
this.Given(x => x.GivenTheReRoute(serviceConfig, reRoute))
.When(x => x.WhenIGetTheServiceProvider())
.Then(x => x.ThenTheServiceProviderIs<ConsulServiceDiscoveryProvider>())
.BDDfy();
}
private void GivenTheReRoute(ServiceProviderConfiguration serviceConfig, ReRoute reRoute)
{
_serviceConfig = serviceConfig;
_reRoute = reRoute;
}
private void WhenIGetTheServiceProvider()
{
_result = _factory.Get(_serviceConfig, _reRoute);
}
private void ThenTheServiceProviderIs<T>()
{
_result.ShouldBeOfType<T>();
}
}
using System;
using System.Collections.Generic;
using Ocelot.Configuration;
using Ocelot.Configuration.Builder;
using Ocelot.ServiceDiscovery;
using Shouldly;
using TestStack.BDDfy;
using Xunit;
namespace Ocelot.UnitTests.ServiceDiscovery
{
public class ServiceProviderFactoryTests
{
private ServiceProviderConfiguration _serviceConfig;
private IServiceDiscoveryProvider _result;
private readonly ServiceDiscoveryProviderFactory _factory;
private ReRoute _reRoute;
public ServiceProviderFactoryTests()
{
_factory = new ServiceDiscoveryProviderFactory();
}
[Fact]
public void should_return_no_service_provider()
{
var serviceConfig = new ServiceProviderConfigurationBuilder()
.Build();
var reRoute = new ReRouteBuilder().Build();
this.Given(x => x.GivenTheReRoute(serviceConfig, reRoute))
.When(x => x.WhenIGetTheServiceProvider())
.Then(x => x.ThenTheServiceProviderIs<ConfigurationServiceProvider>())
.BDDfy();
}
[Fact]
public void should_return_list_of_configuration_services()
{
var serviceConfig = new ServiceProviderConfigurationBuilder()
.Build();
var downstreamAddresses = new List<DownstreamHostAndPort>()
{
new DownstreamHostAndPort("asdf.com", 80),
new DownstreamHostAndPort("abc.com", 80)
};
var reRoute = new ReRouteBuilder().WithDownstreamAddresses(downstreamAddresses).Build();
this.Given(x => x.GivenTheReRoute(serviceConfig, reRoute))
.When(x => x.WhenIGetTheServiceProvider())
.Then(x => x.ThenTheServiceProviderIs<ConfigurationServiceProvider>())
.Then(x => ThenTheFollowingServicesAreReturned(downstreamAddresses))
.BDDfy();
}
private void ThenTheFollowingServicesAreReturned(List<DownstreamHostAndPort> downstreamAddresses)
{
var result = (ConfigurationServiceProvider)_result;
var services = result.Get().Result;
for (int i = 0; i < services.Count; i++)
{
var service = services[i];
var downstreamAddress = downstreamAddresses[i];
service.HostAndPort.DownstreamHost.ShouldBe(downstreamAddress.Host);
service.HostAndPort.DownstreamPort.ShouldBe(downstreamAddress.Port);
}
}
[Fact]
public void should_return_consul_service_provider()
{
var reRoute = new ReRouteBuilder()
.WithServiceName("product")
.WithUseServiceDiscovery(true)
.Build();
var serviceConfig = new ServiceProviderConfigurationBuilder()
.Build();
this.Given(x => x.GivenTheReRoute(serviceConfig, reRoute))
.When(x => x.WhenIGetTheServiceProvider())
.Then(x => x.ThenTheServiceProviderIs<ConsulServiceDiscoveryProvider>())
.BDDfy();
}
private void GivenTheReRoute(ServiceProviderConfiguration serviceConfig, ReRoute reRoute)
{
_serviceConfig = serviceConfig;
_reRoute = reRoute;
}
private void WhenIGetTheServiceProvider()
{
_result = _factory.Get(_serviceConfig, _reRoute);
}
private void ThenTheServiceProviderIs<T>()
{
_result.ShouldBeOfType<T>();
}
}
}

View File

@ -1,138 +1,138 @@
using System.Collections.Generic;
using Ocelot.Values;
using Shouldly;
using TestStack.BDDfy;
using Xunit;
// nothing in use
namespace Ocelot.UnitTests.ServiceDiscovery
{
public class ServiceRegistryTests
{
private Service _service;
private List<Service> _services;
private ServiceRegistry _serviceRegistry;
private ServiceRepository _serviceRepository;
public ServiceRegistryTests()
{
_serviceRepository = new ServiceRepository();
_serviceRegistry = new ServiceRegistry(_serviceRepository);
}
[Fact]
public void should_register_service()
{
this.Given(x => x.GivenAServiceToRegister("product", "localhost:5000", 80))
.When(x => x.WhenIRegisterTheService())
.Then(x => x.ThenTheServiceIsRegistered())
.BDDfy();
}
[Fact]
public void should_lookup_service()
{
this.Given(x => x.GivenAServiceIsRegistered("product", "localhost:600", 80))
.When(x => x.WhenILookupTheService("product"))
.Then(x => x.ThenTheServiceDetailsAreReturned())
.BDDfy();
}
private void ThenTheServiceDetailsAreReturned()
{
_services[0].HostAndPort.DownstreamHost.ShouldBe(_service.HostAndPort.DownstreamHost);
_services[0].HostAndPort.DownstreamPort.ShouldBe(_service.HostAndPort.DownstreamPort);
_services[0].Name.ShouldBe(_service.Name);
}
private void WhenILookupTheService(string name)
{
_services = _serviceRegistry.Lookup(name);
}
private void GivenAServiceIsRegistered(string name, string address, int 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), string.Empty, string.Empty, new string[0]);
}
private void WhenIRegisterTheService()
{
_serviceRegistry.Register(_service);
}
private void ThenTheServiceIsRegistered()
{
var serviceNameAndAddress = _serviceRepository.Get(_service.Name);
serviceNameAndAddress[0].HostAndPort.DownstreamHost.ShouldBe(_service.HostAndPort.DownstreamHost);
serviceNameAndAddress[0].HostAndPort.DownstreamPort.ShouldBe(_service.HostAndPort.DownstreamPort);
serviceNameAndAddress[0].Name.ShouldBe(_service.Name);
}
}
public interface IServiceRegistry
{
void Register(Service serviceNameAndAddress);
List<Service> Lookup(string name);
}
public class ServiceRegistry : IServiceRegistry
{
private readonly IServiceRepository _repository;
public ServiceRegistry(IServiceRepository repository)
{
_repository = repository;
}
public void Register(Service serviceNameAndAddress)
{
_repository.Set(serviceNameAndAddress);
}
public List<Service> Lookup(string name)
{
return _repository.Get(name);
}
}
public interface IServiceRepository
{
List<Service> Get(string serviceName);
void Set(Service serviceNameAndAddress);
}
public class ServiceRepository : IServiceRepository
{
private Dictionary<string, List<Service>> _registeredServices;
public ServiceRepository()
{
_registeredServices = new Dictionary<string, List<Service>>();
}
public List<Service> Get(string serviceName)
{
return _registeredServices[serviceName];
}
public void Set(Service serviceNameAndAddress)
{
List<Service> services;
if(_registeredServices.TryGetValue(serviceNameAndAddress.Name, out services))
{
services.Add(serviceNameAndAddress);
_registeredServices[serviceNameAndAddress.Name] = services;
}
else
{
_registeredServices[serviceNameAndAddress.Name] = new List<Service>(){ serviceNameAndAddress };
}
}
}
}
using System.Collections.Generic;
using Ocelot.Values;
using Shouldly;
using TestStack.BDDfy;
using Xunit;
// nothing in use
namespace Ocelot.UnitTests.ServiceDiscovery
{
public class ServiceRegistryTests
{
private Service _service;
private List<Service> _services;
private ServiceRegistry _serviceRegistry;
private ServiceRepository _serviceRepository;
public ServiceRegistryTests()
{
_serviceRepository = new ServiceRepository();
_serviceRegistry = new ServiceRegistry(_serviceRepository);
}
[Fact]
public void should_register_service()
{
this.Given(x => x.GivenAServiceToRegister("product", "localhost:5000", 80))
.When(x => x.WhenIRegisterTheService())
.Then(x => x.ThenTheServiceIsRegistered())
.BDDfy();
}
[Fact]
public void should_lookup_service()
{
this.Given(x => x.GivenAServiceIsRegistered("product", "localhost:600", 80))
.When(x => x.WhenILookupTheService("product"))
.Then(x => x.ThenTheServiceDetailsAreReturned())
.BDDfy();
}
private void ThenTheServiceDetailsAreReturned()
{
_services[0].HostAndPort.DownstreamHost.ShouldBe(_service.HostAndPort.DownstreamHost);
_services[0].HostAndPort.DownstreamPort.ShouldBe(_service.HostAndPort.DownstreamPort);
_services[0].Name.ShouldBe(_service.Name);
}
private void WhenILookupTheService(string name)
{
_services = _serviceRegistry.Lookup(name);
}
private void GivenAServiceIsRegistered(string name, string address, int port)
{
_service = new Service(name, new ServiceHostAndPort(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 ServiceHostAndPort(address, port), string.Empty, string.Empty, new string[0]);
}
private void WhenIRegisterTheService()
{
_serviceRegistry.Register(_service);
}
private void ThenTheServiceIsRegistered()
{
var serviceNameAndAddress = _serviceRepository.Get(_service.Name);
serviceNameAndAddress[0].HostAndPort.DownstreamHost.ShouldBe(_service.HostAndPort.DownstreamHost);
serviceNameAndAddress[0].HostAndPort.DownstreamPort.ShouldBe(_service.HostAndPort.DownstreamPort);
serviceNameAndAddress[0].Name.ShouldBe(_service.Name);
}
}
public interface IServiceRegistry
{
void Register(Service serviceNameAndAddress);
List<Service> Lookup(string name);
}
public class ServiceRegistry : IServiceRegistry
{
private readonly IServiceRepository _repository;
public ServiceRegistry(IServiceRepository repository)
{
_repository = repository;
}
public void Register(Service serviceNameAndAddress)
{
_repository.Set(serviceNameAndAddress);
}
public List<Service> Lookup(string name)
{
return _repository.Get(name);
}
}
public interface IServiceRepository
{
List<Service> Get(string serviceName);
void Set(Service serviceNameAndAddress);
}
public class ServiceRepository : IServiceRepository
{
private Dictionary<string, List<Service>> _registeredServices;
public ServiceRepository()
{
_registeredServices = new Dictionary<string, List<Service>>();
}
public List<Service> Get(string serviceName)
{
return _registeredServices[serviceName];
}
public void Set(Service serviceNameAndAddress)
{
List<Service> services;
if(_registeredServices.TryGetValue(serviceNameAndAddress.Name, out services))
{
services.Add(serviceNameAndAddress);
_registeredServices[serviceNameAndAddress.Name] = services;
}
else
{
_registeredServices[serviceNameAndAddress.Name] = new List<Service>(){ serviceNameAndAddress };
}
}
}
}