mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-19 10:18:17 +08:00
Fixed issue where qos was being created for each request so circuit breaker was never stopping traffic going to downstream service.
This commit is contained in:
@ -13,6 +13,7 @@ using Ocelot.Logging;
|
||||
using Ocelot.QueryStrings.Middleware;
|
||||
using Ocelot.Requester;
|
||||
using Ocelot.Requester.Middleware;
|
||||
using Ocelot.Requester.QoS;
|
||||
using Ocelot.Responder;
|
||||
using Ocelot.Responses;
|
||||
using TestStack.BDDfy;
|
||||
@ -61,7 +62,7 @@ namespace Ocelot.UnitTests.Requester
|
||||
[Fact]
|
||||
public void should_call_scoped_data_repository_correctly()
|
||||
{
|
||||
this.Given(x => x.GivenTheRequestIs(new Ocelot.Request.Request(new HttpRequestMessage(),new CookieContainer(),true, new Ocelot.Configuration.QoSOptions(3, 8, 5000, Polly.Timeout.TimeoutStrategy.Pessimistic))))
|
||||
this.Given(x => x.GivenTheRequestIs(new Ocelot.Request.Request(new HttpRequestMessage(),new CookieContainer(),true, new NoQoSProvider())))
|
||||
.And(x => x.GivenTheRequesterReturns(new HttpResponseMessage()))
|
||||
.And(x => x.GivenTheScopedRepoReturns())
|
||||
.When(x => x.WhenICallTheMiddleware())
|
||||
|
80
test/Ocelot.UnitTests/Requester/QoSProviderFactoryTests.cs
Normal file
80
test/Ocelot.UnitTests/Requester/QoSProviderFactoryTests.cs
Normal file
@ -0,0 +1,80 @@
|
||||
using Moq;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.Logging;
|
||||
using Ocelot.Requester.QoS;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Requester
|
||||
{
|
||||
public class QoSProviderFactoryTests
|
||||
{
|
||||
private readonly IQoSProviderFactory _factory;
|
||||
private ReRoute _reRoute;
|
||||
private IQoSProvider _result;
|
||||
private Mock<IOcelotLoggerFactory> _loggerFactory;
|
||||
private Mock<IOcelotLogger> _logger;
|
||||
|
||||
public QoSProviderFactoryTests()
|
||||
{
|
||||
_logger = new Mock<IOcelotLogger>();
|
||||
_loggerFactory = new Mock<IOcelotLoggerFactory>();
|
||||
_loggerFactory
|
||||
.Setup(x => x.CreateLogger<PollyQoSProvider>())
|
||||
.Returns(_logger.Object);
|
||||
_factory = new QoSProviderFactory(_loggerFactory.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_no_qos_provider()
|
||||
{
|
||||
var reRoute = new ReRouteBuilder()
|
||||
.WithUpstreamHttpMethod("get")
|
||||
.WithIsQos(false)
|
||||
.Build();
|
||||
|
||||
this.Given(x => x.GivenAReRoute(reRoute))
|
||||
.When(x => x.WhenIGetTheQoSProvider())
|
||||
.Then(x => x.ThenTheQoSProviderIsReturned<NoQoSProvider>())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_polly_qos_provider()
|
||||
{
|
||||
var qosOptions = new QoSOptionsBuilder()
|
||||
.WithTimeoutValue(100)
|
||||
.WithDurationOfBreak(100)
|
||||
.WithExceptionsAllowedBeforeBreaking(100)
|
||||
.Build();
|
||||
|
||||
var reRoute = new ReRouteBuilder()
|
||||
.WithUpstreamHttpMethod("get")
|
||||
.WithIsQos(true)
|
||||
.WithQosOptions(qosOptions)
|
||||
.Build();
|
||||
|
||||
this.Given(x => x.GivenAReRoute(reRoute))
|
||||
.When(x => x.WhenIGetTheQoSProvider())
|
||||
.Then(x => x.ThenTheQoSProviderIsReturned<PollyQoSProvider>())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenAReRoute(ReRoute reRoute)
|
||||
{
|
||||
_reRoute = reRoute;
|
||||
}
|
||||
|
||||
private void WhenIGetTheQoSProvider()
|
||||
{
|
||||
_result = _factory.Get(_reRoute);
|
||||
}
|
||||
|
||||
private void ThenTheQoSProviderIsReturned<T>()
|
||||
{
|
||||
_result.ShouldBeOfType<T>();
|
||||
}
|
||||
}
|
||||
}
|
117
test/Ocelot.UnitTests/Requester/QosProviderHouseTests.cs
Normal file
117
test/Ocelot.UnitTests/Requester/QosProviderHouseTests.cs
Normal file
@ -0,0 +1,117 @@
|
||||
using Ocelot.Requester.QoS;
|
||||
using Ocelot.Responses;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Requester
|
||||
{
|
||||
public class QosProviderHouseTests
|
||||
{
|
||||
private IQoSProvider _qoSProvider;
|
||||
private readonly QosProviderHouse _qosProviderHouse;
|
||||
private Response _addResult;
|
||||
private Response<IQoSProvider> _getResult;
|
||||
private string _key;
|
||||
|
||||
public QosProviderHouseTests()
|
||||
{
|
||||
_qosProviderHouse = new QosProviderHouse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_store_qos_provider()
|
||||
{
|
||||
var key = "test";
|
||||
|
||||
this.Given(x => x.GivenThereIsAQoSProvider(key, new FakeQoSProvider()))
|
||||
.When(x => x.WhenIAddTheQoSProvider())
|
||||
.Then(x => x.ThenItIsAdded())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_get_qos_provider()
|
||||
{
|
||||
var key = "test";
|
||||
|
||||
this.Given(x => x.GivenThereIsAQoSProvider(key, new FakeQoSProvider()))
|
||||
.When(x => x.WhenWeGetTheQoSProvider(key))
|
||||
.Then(x => x.ThenItIsReturned())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_store_qos_providers_by_key()
|
||||
{
|
||||
var key = "test";
|
||||
var keyTwo = "testTwo";
|
||||
|
||||
this.Given(x => x.GivenThereIsAQoSProvider(key, new FakeQoSProvider()))
|
||||
.And(x => x.GivenThereIsAQoSProvider(keyTwo, new FakePollyQoSProvider()))
|
||||
.When(x => x.WhenWeGetTheQoSProvider(key))
|
||||
.Then(x => x.ThenTheQoSProviderIs<FakeQoSProvider>())
|
||||
.When(x => x.WhenWeGetTheQoSProvider(keyTwo))
|
||||
.Then(x => x.ThenTheQoSProviderIs<FakePollyQoSProvider>())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_error_if_no_qos_provider_with_key()
|
||||
{
|
||||
this.When(x => x.WhenWeGetTheQoSProvider("test"))
|
||||
.Then(x => x.ThenAnErrorIsReturned())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void ThenAnErrorIsReturned()
|
||||
{
|
||||
_getResult.IsError.ShouldBeTrue();
|
||||
_getResult.Errors[0].ShouldBeOfType<UnableToFindQoSProviderError>();
|
||||
}
|
||||
|
||||
private void ThenTheQoSProviderIs<T>()
|
||||
{
|
||||
_getResult.Data.ShouldBeOfType<T>();
|
||||
}
|
||||
|
||||
private void ThenItIsAdded()
|
||||
{
|
||||
_addResult.IsError.ShouldBe(false);
|
||||
_addResult.ShouldBeOfType<OkResponse>();
|
||||
}
|
||||
|
||||
private void WhenIAddTheQoSProvider()
|
||||
{
|
||||
_addResult = _qosProviderHouse.Add(_key, _qoSProvider);
|
||||
}
|
||||
|
||||
|
||||
private void GivenThereIsAQoSProvider(string key, IQoSProvider qoSProvider)
|
||||
{
|
||||
_key = key;
|
||||
_qoSProvider = qoSProvider;
|
||||
WhenIAddTheQoSProvider();
|
||||
}
|
||||
|
||||
private void WhenWeGetTheQoSProvider(string key)
|
||||
{
|
||||
_getResult = _qosProviderHouse.Get(key);
|
||||
}
|
||||
|
||||
private void ThenItIsReturned()
|
||||
{
|
||||
_getResult.Data.ShouldBe(_qoSProvider);
|
||||
}
|
||||
|
||||
class FakeQoSProvider : IQoSProvider
|
||||
{
|
||||
public CircuitBreaker CircuitBreaker { get; }
|
||||
}
|
||||
|
||||
class FakePollyQoSProvider : IQoSProvider
|
||||
{
|
||||
public CircuitBreaker CircuitBreaker { get; }
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user