tests for error handling on load balancing middleware

This commit is contained in:
Tom Gardham-Pallister 2017-02-06 20:22:09 +00:00
parent c3e60ac08a
commit a4495b8fa9
2 changed files with 83 additions and 8 deletions

View File

@ -31,16 +31,18 @@ namespace Ocelot.LoadBalancer.Middleware
{ {
_logger.LogDebug("started calling load balancing middleware"); _logger.LogDebug("started calling load balancing middleware");
var loadBalancer = _loadBalancerHouse.Get(DownstreamRoute.ReRoute.LoadBalancerKey); var getLoadBalancer = _loadBalancerHouse.Get(DownstreamRoute.ReRoute.LoadBalancerKey);
if(loadBalancer.IsError) if(getLoadBalancer.IsError)
{ {
//set errors and return SetPipelineError(getLoadBalancer.Errors);
return;
} }
var hostAndPort = await loadBalancer.Data.Lease(); var hostAndPort = await getLoadBalancer.Data.Lease();
if(hostAndPort.IsError) if(hostAndPort.IsError)
{ {
//set errors and return SetPipelineError(hostAndPort.Errors);
return;
} }
SetHostAndPortForThisRequest(hostAndPort.Data); SetHostAndPortForThisRequest(hostAndPort.Data);
@ -51,11 +53,11 @@ namespace Ocelot.LoadBalancer.Middleware
{ {
await _next.Invoke(context); await _next.Invoke(context);
loadBalancer.Data.Release(hostAndPort.Data); getLoadBalancer.Data.Release(hostAndPort.Data);
} }
catch (Exception) catch (Exception)
{ {
loadBalancer.Data.Release(hostAndPort.Data); getLoadBalancer.Data.Release(hostAndPort.Data);
_logger.LogDebug("error calling next middleware, exception will be thrown to global handler"); _logger.LogDebug("error calling next middleware, exception will be thrown to global handler");
throw; throw;
} }

View File

@ -7,6 +7,7 @@ using Microsoft.Extensions.DependencyInjection;
using Moq; using Moq;
using Ocelot.Configuration.Builder; using Ocelot.Configuration.Builder;
using Ocelot.DownstreamRouteFinder; using Ocelot.DownstreamRouteFinder;
using Ocelot.Errors;
using Ocelot.Infrastructure.RequestData; using Ocelot.Infrastructure.RequestData;
using Ocelot.LoadBalancer.LoadBalancers; using Ocelot.LoadBalancer.LoadBalancers;
using Ocelot.LoadBalancer.Middleware; using Ocelot.LoadBalancer.Middleware;
@ -31,6 +32,8 @@ namespace Ocelot.UnitTests.LoadBalancer
private OkResponse<Ocelot.Request.Request> _request; private OkResponse<Ocelot.Request.Request> _request;
private OkResponse<string> _downstreamUrl; private OkResponse<string> _downstreamUrl;
private OkResponse<DownstreamRoute> _downstreamRoute; private OkResponse<DownstreamRoute> _downstreamRoute;
private ErrorResponse<ILoadBalancer> _getLoadBalancerHouseError;
private ErrorResponse<HostAndPort> _getHostAndPortError;
public LoadBalancerMiddlewareTests() public LoadBalancerMiddlewareTests()
{ {
@ -77,6 +80,45 @@ namespace Ocelot.UnitTests.LoadBalancer
.BDDfy(); .BDDfy();
} }
[Fact]
public void should_set_pipeline_error_if_cannot_get_load_balancer()
{
var downstreamRoute = new DownstreamRoute(new List<Ocelot.DownstreamRouteFinder.UrlMatcher.UrlPathPlaceholderNameAndValue>(),
new ReRouteBuilder()
.Build());
this.Given(x => x.GivenTheDownStreamUrlIs("any old string"))
.And(x => x.GivenTheDownStreamRouteIs(downstreamRoute))
.And(x => x.GivenTheLoadBalancerHouseReturnsAnError())
.When(x => x.WhenICallTheMiddleware())
.Then(x => x.ThenAnErrorStatingLoadBalancerCouldNotBeFoundIsSetOnPipeline())
.BDDfy();
}
[Fact]
public void should_set_pipeline_error_if_cannot_get_least()
{
var downstreamRoute = new DownstreamRoute(new List<Ocelot.DownstreamRouteFinder.UrlMatcher.UrlPathPlaceholderNameAndValue>(),
new ReRouteBuilder()
.Build());
this.Given(x => x.GivenTheDownStreamUrlIs("any old string"))
.And(x => x.GivenTheDownStreamRouteIs(downstreamRoute))
.And(x => x.GivenTheLoadBalancerHouseReturns())
.And(x => x.GivenTheLoadBalancerReturnsAnError())
.When(x => x.WhenICallTheMiddleware())
.Then(x => x.ThenAnErrorStatingHostAndPortCouldNotBeFoundIsSetOnPipeline())
.BDDfy();
}
private void GivenTheLoadBalancerReturnsAnError()
{
_getHostAndPortError = new ErrorResponse<HostAndPort>(new List<Error>() { new ServicesAreNullError($"services were null for bah") });
_loadBalancer
.Setup(x => x.Lease())
.ReturnsAsync(_getHostAndPortError);
}
private void GivenTheLoadBalancerReturns() private void GivenTheLoadBalancerReturns()
{ {
_hostAndPort = new HostAndPort("127.0.0.1", 80); _hostAndPort = new HostAndPort("127.0.0.1", 80);
@ -100,12 +142,43 @@ namespace Ocelot.UnitTests.LoadBalancer
.Returns(new OkResponse<ILoadBalancer>(_loadBalancer.Object)); .Returns(new OkResponse<ILoadBalancer>(_loadBalancer.Object));
} }
private void GivenTheLoadBalancerHouseReturnsAnError()
{
_getLoadBalancerHouseError = new ErrorResponse<ILoadBalancer>(new List<Ocelot.Errors.Error>()
{
new UnableToFindLoadBalancerError($"unabe to find load balancer for bah")
});
_loadBalancerHouse
.Setup(x => x.Get(It.IsAny<string>()))
.Returns(_getLoadBalancerHouseError);
}
private void ThenTheScopedDataRepositoryIsCalledCorrectly() private void ThenTheScopedDataRepositoryIsCalledCorrectly()
{ {
_scopedRepository _scopedRepository
.Verify(x => x.Add("HostAndPort", _hostAndPort), Times.Once()); .Verify(x => x.Add("HostAndPort", _hostAndPort), Times.Once());
} }
private void ThenAnErrorStatingLoadBalancerCouldNotBeFoundIsSetOnPipeline()
{
_scopedRepository
.Verify(x => x.Add("OcelotMiddlewareError", true), Times.Once);
_scopedRepository
.Verify(x => x.Add("OcelotMiddlewareErrors", _getLoadBalancerHouseError.Errors), Times.Once);
}
private void ThenAnErrorStatingHostAndPortCouldNotBeFoundIsSetOnPipeline()
{
_scopedRepository
.Verify(x => x.Add("OcelotMiddlewareError", true), Times.Once);
_scopedRepository
.Verify(x => x.Add("OcelotMiddlewareErrors", _getHostAndPortError.Errors), Times.Once);
}
private void WhenICallTheMiddleware() private void WhenICallTheMiddleware()
{ {
_result = _client.GetAsync(_url).Result; _result = _client.GetAsync(_url).Result;