mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-19 20:48:15 +08:00
more error handling and docs
This commit is contained in:
@ -1,20 +1,21 @@
|
||||
using Moq;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.LoadBalancer.LoadBalancers;
|
||||
using Ocelot.ServiceDiscovery.Providers;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.LoadBalancer
|
||||
namespace Ocelot.UnitTests.LoadBalancer
|
||||
{
|
||||
using Moq;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.LoadBalancer.LoadBalancers;
|
||||
using Ocelot.ServiceDiscovery.Providers;
|
||||
using Ocelot.Responses;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
public class CookieStickySessionsCreatorTests
|
||||
{
|
||||
private readonly CookieStickySessionsCreator _creator;
|
||||
private readonly Mock<IServiceDiscoveryProvider> _serviceProvider;
|
||||
private DownstreamReRoute _reRoute;
|
||||
private ILoadBalancer _loadBalancer;
|
||||
private Response<ILoadBalancer> _loadBalancer;
|
||||
private string _typeName;
|
||||
|
||||
public CookieStickySessionsCreatorTests()
|
||||
@ -62,7 +63,7 @@ namespace Ocelot.UnitTests.LoadBalancer
|
||||
private void ThenTheLoadBalancerIsReturned<T>()
|
||||
where T : ILoadBalancer
|
||||
{
|
||||
_loadBalancer.ShouldBeOfType<T>();
|
||||
_loadBalancer.Data.ShouldBeOfType<T>();
|
||||
}
|
||||
|
||||
private void ThenTheLoadBalancerTypeIs(string type)
|
||||
|
@ -16,11 +16,11 @@ namespace Ocelot.UnitTests.LoadBalancer
|
||||
{
|
||||
public class DelegateInvokingLoadBalancerCreatorTests
|
||||
{
|
||||
private readonly DelegateInvokingLoadBalancerCreator<FakeLoadBalancer> _creator;
|
||||
private readonly Func<DownstreamReRoute, IServiceDiscoveryProvider, ILoadBalancer> _creatorFunc;
|
||||
private DelegateInvokingLoadBalancerCreator<FakeLoadBalancer> _creator;
|
||||
private Func<DownstreamReRoute, IServiceDiscoveryProvider, ILoadBalancer> _creatorFunc;
|
||||
private readonly Mock<IServiceDiscoveryProvider> _serviceProvider;
|
||||
private DownstreamReRoute _reRoute;
|
||||
private ILoadBalancer _loadBalancer;
|
||||
private Response<ILoadBalancer> _loadBalancer;
|
||||
private string _typeName;
|
||||
|
||||
public DelegateInvokingLoadBalancerCreatorTests()
|
||||
@ -51,6 +51,31 @@ namespace Ocelot.UnitTests.LoadBalancer
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_error()
|
||||
{
|
||||
var reRoute = new DownstreamReRouteBuilder()
|
||||
.Build();
|
||||
|
||||
this.Given(x => x.GivenAReRoute(reRoute))
|
||||
.And(x => x.GivenTheCreatorFuncThrows())
|
||||
.When(x => x.WhenIGetTheLoadBalancer())
|
||||
.Then(x => x.ThenAnErrorIsReturned())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheCreatorFuncThrows()
|
||||
{
|
||||
_creatorFunc = (reRoute, serviceDiscoveryProvider) => throw new Exception();
|
||||
|
||||
_creator = new DelegateInvokingLoadBalancerCreator<FakeLoadBalancer>(_creatorFunc);
|
||||
}
|
||||
|
||||
private void ThenAnErrorIsReturned()
|
||||
{
|
||||
_loadBalancer.IsError.ShouldBeTrue();
|
||||
}
|
||||
|
||||
private void GivenAReRoute(DownstreamReRoute reRoute)
|
||||
{
|
||||
_reRoute = reRoute;
|
||||
@ -69,7 +94,7 @@ namespace Ocelot.UnitTests.LoadBalancer
|
||||
private void ThenTheLoadBalancerIsReturned<T>()
|
||||
where T : ILoadBalancer
|
||||
{
|
||||
_loadBalancer.ShouldBeOfType<T>();
|
||||
_loadBalancer.Data.ShouldBeOfType<T>();
|
||||
}
|
||||
|
||||
private void ThenTheLoadBalancerTypeIs(string type)
|
||||
|
@ -1,20 +1,21 @@
|
||||
using Moq;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.LoadBalancer.LoadBalancers;
|
||||
using Ocelot.ServiceDiscovery.Providers;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.LoadBalancer
|
||||
namespace Ocelot.UnitTests.LoadBalancer
|
||||
{
|
||||
using Moq;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.LoadBalancer.LoadBalancers;
|
||||
using Ocelot.Responses;
|
||||
using Ocelot.ServiceDiscovery.Providers;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
public class LeastConnectionCreatorTests
|
||||
{
|
||||
private readonly LeastConnectionCreator _creator;
|
||||
private readonly Mock<IServiceDiscoveryProvider> _serviceProvider;
|
||||
private DownstreamReRoute _reRoute;
|
||||
private ILoadBalancer _loadBalancer;
|
||||
private Response<ILoadBalancer> _loadBalancer;
|
||||
private string _typeName;
|
||||
|
||||
public LeastConnectionCreatorTests()
|
||||
@ -62,7 +63,7 @@ namespace Ocelot.UnitTests.LoadBalancer
|
||||
private void ThenTheLoadBalancerIsReturned<T>()
|
||||
where T : ILoadBalancer
|
||||
{
|
||||
_loadBalancer.ShouldBeOfType<T>();
|
||||
_loadBalancer.Data.ShouldBeOfType<T>();
|
||||
}
|
||||
|
||||
private void ThenTheLoadBalancerTypeIs(string type)
|
||||
|
@ -16,6 +16,8 @@ using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.LoadBalancer
|
||||
{
|
||||
using System;
|
||||
|
||||
public class LoadBalancerFactoryTests
|
||||
{
|
||||
private DownstreamReRoute _reRoute;
|
||||
@ -35,6 +37,7 @@ namespace Ocelot.UnitTests.LoadBalancer
|
||||
new FakeLoadBalancerCreator<FakeLoadBalancerOne>(),
|
||||
new FakeLoadBalancerCreator<FakeLoadBalancerTwo>(),
|
||||
new FakeLoadBalancerCreator<FakeNoLoadBalancer>(nameof(NoLoadBalancer)),
|
||||
new BrokenLoadBalancerCreator<BrokenLoadBalancer>(),
|
||||
};
|
||||
_factory = new LoadBalancerFactory(_serviceProviderFactory.Object, _loadBalancerCreators);
|
||||
}
|
||||
@ -87,6 +90,22 @@ namespace Ocelot.UnitTests.LoadBalancer
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_error_response_if_creator_errors()
|
||||
{
|
||||
var reRoute = new DownstreamReRouteBuilder()
|
||||
.WithLoadBalancerOptions(new LoadBalancerOptions("BrokenLoadBalancer", "", 0))
|
||||
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
||||
.Build();
|
||||
|
||||
this.Given(x => x.GivenAReRoute(reRoute))
|
||||
.And(x => GivenAServiceProviderConfig(new ServiceProviderConfigurationBuilder().Build()))
|
||||
.And(x => x.GivenTheServiceProviderFactoryReturns())
|
||||
.When(x => x.WhenIGetTheLoadBalancer())
|
||||
.Then(x => x.ThenAnErrorResponseIsReturned())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_call_service_provider()
|
||||
{
|
||||
@ -183,14 +202,30 @@ namespace Ocelot.UnitTests.LoadBalancer
|
||||
Type = type;
|
||||
}
|
||||
|
||||
public ILoadBalancer Create(DownstreamReRoute reRoute, IServiceDiscoveryProvider serviceProvider)
|
||||
public Response<ILoadBalancer> Create(DownstreamReRoute reRoute, IServiceDiscoveryProvider serviceProvider)
|
||||
{
|
||||
return new T();
|
||||
return new OkResponse<ILoadBalancer>(new T());
|
||||
}
|
||||
|
||||
public string Type { get; }
|
||||
}
|
||||
|
||||
private class BrokenLoadBalancerCreator<T> : ILoadBalancerCreator
|
||||
where T : ILoadBalancer, new()
|
||||
{
|
||||
public BrokenLoadBalancerCreator()
|
||||
{
|
||||
Type = typeof(T).Name;
|
||||
}
|
||||
|
||||
public Response<ILoadBalancer> Create(DownstreamReRoute reRoute, IServiceDiscoveryProvider serviceProvider)
|
||||
{
|
||||
return new ErrorResponse<ILoadBalancer>(new ErrorInvokingLoadBalancerCreator(new Exception()));
|
||||
}
|
||||
|
||||
public string Type { get; }
|
||||
}
|
||||
|
||||
private class FakeLoadBalancerOne : ILoadBalancer
|
||||
{
|
||||
public Task<Response<ServiceHostAndPort>> Lease(DownstreamContext context)
|
||||
@ -229,5 +264,19 @@ namespace Ocelot.UnitTests.LoadBalancer
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
private class BrokenLoadBalancer : ILoadBalancer
|
||||
{
|
||||
public Task<Response<ServiceHostAndPort>> Lease(DownstreamContext context)
|
||||
{
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
|
||||
public void Release(ServiceHostAndPort hostAndPort)
|
||||
{
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -1,20 +1,21 @@
|
||||
using Moq;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.LoadBalancer.LoadBalancers;
|
||||
using Ocelot.ServiceDiscovery.Providers;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.LoadBalancer
|
||||
namespace Ocelot.UnitTests.LoadBalancer
|
||||
{
|
||||
using Moq;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.LoadBalancer.LoadBalancers;
|
||||
using Ocelot.Responses;
|
||||
using Ocelot.ServiceDiscovery.Providers;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
public class NoLoadBalancerCreatorTests
|
||||
{
|
||||
private readonly NoLoadBalancerCreator _creator;
|
||||
private readonly Mock<IServiceDiscoveryProvider> _serviceProvider;
|
||||
private DownstreamReRoute _reRoute;
|
||||
private ILoadBalancer _loadBalancer;
|
||||
private Response<ILoadBalancer> _loadBalancer;
|
||||
private string _typeName;
|
||||
|
||||
public NoLoadBalancerCreatorTests()
|
||||
@ -61,7 +62,7 @@ namespace Ocelot.UnitTests.LoadBalancer
|
||||
private void ThenTheLoadBalancerIsReturned<T>()
|
||||
where T : ILoadBalancer
|
||||
{
|
||||
_loadBalancer.ShouldBeOfType<T>();
|
||||
_loadBalancer.Data.ShouldBeOfType<T>();
|
||||
}
|
||||
|
||||
private void ThenTheLoadBalancerTypeIs(string type)
|
||||
|
@ -1,20 +1,21 @@
|
||||
using Moq;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.LoadBalancer.LoadBalancers;
|
||||
using Ocelot.ServiceDiscovery.Providers;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.LoadBalancer
|
||||
namespace Ocelot.UnitTests.LoadBalancer
|
||||
{
|
||||
using Moq;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.LoadBalancer.LoadBalancers;
|
||||
using Ocelot.Responses;
|
||||
using Ocelot.ServiceDiscovery.Providers;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
public class RoundRobinCreatorTests
|
||||
{
|
||||
private readonly RoundRobinCreator _creator;
|
||||
private readonly Mock<IServiceDiscoveryProvider> _serviceProvider;
|
||||
private DownstreamReRoute _reRoute;
|
||||
private ILoadBalancer _loadBalancer;
|
||||
private Response<ILoadBalancer> _loadBalancer;
|
||||
private string _typeName;
|
||||
|
||||
public RoundRobinCreatorTests()
|
||||
@ -61,7 +62,7 @@ namespace Ocelot.UnitTests.LoadBalancer
|
||||
private void ThenTheLoadBalancerIsReturned<T>()
|
||||
where T : ILoadBalancer
|
||||
{
|
||||
_loadBalancer.ShouldBeOfType<T>();
|
||||
_loadBalancer.Data.ShouldBeOfType<T>();
|
||||
}
|
||||
|
||||
private void ThenTheLoadBalancerTypeIs(string type)
|
||||
|
@ -48,6 +48,7 @@ namespace Ocelot.UnitTests.Responder
|
||||
[Theory]
|
||||
[InlineData(OcelotErrorCode.UnableToCompleteRequestError)]
|
||||
[InlineData(OcelotErrorCode.CouldNotFindLoadBalancerCreator)]
|
||||
[InlineData(OcelotErrorCode.ErrorInvokingLoadBalancerCreator)]
|
||||
public void should_return_internal_server_error(OcelotErrorCode errorCode)
|
||||
{
|
||||
ShouldMapErrorToStatusCode(errorCode, HttpStatusCode.InternalServerError);
|
||||
@ -133,7 +134,7 @@ namespace Ocelot.UnitTests.Responder
|
||||
// If this test fails then it's because the number of error codes has changed.
|
||||
// You should make the appropriate changes to the test cases here to ensure
|
||||
// they cover all the error codes, and then modify this assertion.
|
||||
Enum.GetNames(typeof(OcelotErrorCode)).Length.ShouldBe(40, "Looks like the number of error codes has changed. Do you need to modify ErrorsToHttpStatusCodeMapper?");
|
||||
Enum.GetNames(typeof(OcelotErrorCode)).Length.ShouldBe(41, "Looks like the number of error codes has changed. Do you need to modify ErrorsToHttpStatusCodeMapper?");
|
||||
}
|
||||
|
||||
private void ShouldMapErrorToStatusCode(OcelotErrorCode errorCode, HttpStatusCode expectedHttpStatusCode)
|
||||
|
Reference in New Issue
Block a user