diff --git a/test/Ocelot.UnitTests/Configuration/ConsulFileConfigurationPollerTests.cs b/test/Ocelot.UnitTests/Configuration/ConsulFileConfigurationPollerTests.cs index 7393b631..8661240d 100644 --- a/test/Ocelot.UnitTests/Configuration/ConsulFileConfigurationPollerTests.cs +++ b/test/Ocelot.UnitTests/Configuration/ConsulFileConfigurationPollerTests.cs @@ -1,158 +1,174 @@ -using System; -using System.Collections.Generic; -using System.Threading; -using Moq; -using Ocelot.Configuration.File; -using Ocelot.Configuration.Repository; -using Ocelot.Configuration.Setter; -using Ocelot.Logging; -using Ocelot.Responses; -using Ocelot.UnitTests.Responder; -using TestStack.BDDfy; -using Xunit; -using Shouldly; -using static Ocelot.Infrastructure.Wait; - -namespace Ocelot.UnitTests.Configuration -{ - public class ConsulFileConfigurationPollerTests : IDisposable - { - private ConsulFileConfigurationPoller _poller; - private Mock _factory; - private Mock _repo; - private Mock _setter; - private FileConfiguration _fileConfig; - private Mock _config; - - public ConsulFileConfigurationPollerTests() - { - var logger = new Mock(); - _factory = new Mock(); - _factory.Setup(x => x.CreateLogger()).Returns(logger.Object); - _repo = new Mock(); - _setter = new Mock(); - _fileConfig = new FileConfiguration(); - _config = new Mock(); - _repo.Setup(x => x.Get()).ReturnsAsync(new OkResponse(_fileConfig)); - _config.Setup(x => x.Delay).Returns(100); - _poller = new ConsulFileConfigurationPoller(_factory.Object, _repo.Object, _setter.Object, _config.Object); - } - - public void Dispose() - { - _poller.Dispose(); - } - - [Fact] - public void should_start() - { - this.Given(x => ThenTheSetterIsCalled(_fileConfig, 1)) - .BDDfy(); - } - - [Fact] - public void should_call_setter_when_gets_new_config() - { - var newConfig = new FileConfiguration { - ReRoutes = new List - { - new FileReRoute - { - DownstreamHostAndPorts = new List - { - new FileHostAndPort - { - Host = "test" - } - }, - } - } - }; - - this.Given(x => WhenTheConfigIsChangedInConsul(newConfig, 0)) - .Then(x => ThenTheSetterIsCalled(newConfig, 1)) - .BDDfy(); - } - - [Fact] - public void should_not_poll_if_already_polling() - { - var newConfig = new FileConfiguration - { - ReRoutes = new List - { - new FileReRoute - { - DownstreamHostAndPorts = new List - { - new FileHostAndPort - { - Host = "test" - } - }, - } - } - }; - - this.Given(x => WhenTheConfigIsChangedInConsul(newConfig, 10)) - .Then(x => ThenTheSetterIsCalled(newConfig, 1)) - .BDDfy(); - } - - [Fact] - public void should_do_nothing_if_call_to_consul_fails() - { - var newConfig = new FileConfiguration - { - ReRoutes = new List - { - new FileReRoute - { - DownstreamHostAndPorts = new List - { - new FileHostAndPort - { - Host = "test" - } - }, - } - } - }; - - this.Given(x => WhenConsulErrors()) - .Then(x => ThenTheSetterIsCalled(newConfig, 0)) - .BDDfy(); - } - - private void WhenConsulErrors() - { - _repo - .Setup(x => x.Get()) - .ReturnsAsync(new ErrorResponse(new AnyError())); - } - - private void WhenTheConfigIsChangedInConsul(FileConfiguration newConfig, int delay) - { - _repo - .Setup(x => x.Get()) - .Callback(() => Thread.Sleep(delay)) - .ReturnsAsync(new OkResponse(newConfig)); - } - - private void ThenTheSetterIsCalled(FileConfiguration fileConfig, int times) - { - var result = WaitFor(2000).Until(() => { - try - { - _setter.Verify(x => x.Set(fileConfig), Times.Exactly(times)); - return true; - } - catch(Exception) - { - return false; - } - }); - result.ShouldBeTrue(); - } - } -} +using System; +using System.Collections.Generic; +using System.Threading; +using Moq; +using Ocelot.Configuration.File; +using Ocelot.Configuration.Repository; +using Ocelot.Configuration.Setter; +using Ocelot.Logging; +using Ocelot.Responses; +using Ocelot.UnitTests.Responder; +using TestStack.BDDfy; +using Xunit; +using Shouldly; +using static Ocelot.Infrastructure.Wait; + +namespace Ocelot.UnitTests.Configuration +{ + public class ConsulFileConfigurationPollerTests : IDisposable + { + private ConsulFileConfigurationPoller _poller; + private Mock _factory; + private Mock _repo; + private Mock _setter; + private FileConfiguration _fileConfig; + private Mock _config; + + public ConsulFileConfigurationPollerTests() + { + var logger = new Mock(); + _factory = new Mock(); + _factory.Setup(x => x.CreateLogger()).Returns(logger.Object); + _repo = new Mock(); + _setter = new Mock(); + _fileConfig = new FileConfiguration(); + _config = new Mock(); + _repo.Setup(x => x.Get()).ReturnsAsync(new OkResponse(_fileConfig)); + _config.Setup(x => x.Delay).Returns(100); + _poller = new ConsulFileConfigurationPoller(_factory.Object, _repo.Object, _setter.Object, _config.Object); + } + + public void Dispose() + { + _poller.Dispose(); + } + + [Fact] + public void should_start() + { + this.Given(x => ThenTheSetterIsCalled(_fileConfig, 1)) + .BDDfy(); + } + + [Fact] + public void should_call_setter_when_gets_new_config() + { + var newConfig = new FileConfiguration { + ReRoutes = new List + { + new FileReRoute + { + DownstreamHostAndPorts = new List + { + new FileHostAndPort + { + Host = "test" + } + }, + } + } + }; + + this.Given(x => WhenTheConfigIsChangedInConsul(newConfig, 0)) + .Then(x => ThenTheSetterIsCalledAtLeast(newConfig, 1)) + .BDDfy(); + } + + [Fact] + public void should_not_poll_if_already_polling() + { + var newConfig = new FileConfiguration + { + ReRoutes = new List + { + new FileReRoute + { + DownstreamHostAndPorts = new List + { + new FileHostAndPort + { + Host = "test" + } + }, + } + } + }; + + this.Given(x => WhenTheConfigIsChangedInConsul(newConfig, 10)) + .Then(x => ThenTheSetterIsCalled(newConfig, 1)) + .BDDfy(); + } + + [Fact] + public void should_do_nothing_if_call_to_consul_fails() + { + var newConfig = new FileConfiguration + { + ReRoutes = new List + { + new FileReRoute + { + DownstreamHostAndPorts = new List + { + new FileHostAndPort + { + Host = "test" + } + }, + } + } + }; + + this.Given(x => WhenConsulErrors()) + .Then(x => ThenTheSetterIsCalled(newConfig, 0)) + .BDDfy(); + } + + private void WhenConsulErrors() + { + _repo + .Setup(x => x.Get()) + .ReturnsAsync(new ErrorResponse(new AnyError())); + } + + private void WhenTheConfigIsChangedInConsul(FileConfiguration newConfig, int delay) + { + _repo + .Setup(x => x.Get()) + .Callback(() => Thread.Sleep(delay)) + .ReturnsAsync(new OkResponse(newConfig)); + } + + private void ThenTheSetterIsCalled(FileConfiguration fileConfig, int times) + { + var result = WaitFor(2000).Until(() => { + try + { + _setter.Verify(x => x.Set(fileConfig), Times.Exactly(times)); + return true; + } + catch(Exception) + { + return false; + } + }); + result.ShouldBeTrue(); + } + + private void ThenTheSetterIsCalledAtLeast(FileConfiguration fileConfig, int times) + { + var result = WaitFor(2000).Until(() => { + try + { + _setter.Verify(x => x.Set(fileConfig), Times.AtLeast(times)); + return true; + } + catch(Exception) + { + return false; + } + }); + result.ShouldBeTrue(); + } + } +}