test should be at least once

This commit is contained in:
Tom Gardham-Pallister 2018-05-06 18:43:24 +01:00
parent 5ed8257a58
commit a2c7fda4c9

View File

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