#268 fix flakey acceptance test (#282)

* #268 added waiter to test, altho i wasn't able to replicate flakeyness with wait anyway! Hopefully this will be solid now!

* #268 fixed a warning

* #268 more code coverage
This commit is contained in:
Tom Pallister
2018-03-17 12:54:17 +00:00
committed by GitHub
parent ed11f3024c
commit 8a2f76d0c5
9 changed files with 168 additions and 143 deletions

View File

@ -17,10 +17,16 @@ namespace Ocelot.Configuration.Repository
private string _previousAsJson;
private readonly Timer _timer;
private bool _polling;
private readonly IConsulPollerConfiguration _config;
public ConsulFileConfigurationPoller(IOcelotLoggerFactory factory, IFileConfigurationRepository repo, IFileConfigurationSetter setter)
public ConsulFileConfigurationPoller(
IOcelotLoggerFactory factory,
IFileConfigurationRepository repo,
IFileConfigurationSetter setter,
IConsulPollerConfiguration config)
{
_setter = setter;
_config = config;
_logger = factory.CreateLogger<ConsulFileConfigurationPoller>();
_repo = repo;
_previousAsJson = "";
@ -30,11 +36,11 @@ namespace Ocelot.Configuration.Repository
{
return;
}
_polling = true;
await Poll();
_polling = false;
}, null, 0, 1000);
}, null, 0, _config.Delay);
}
private async Task Poll()
@ -63,8 +69,7 @@ namespace Ocelot.Configuration.Repository
/// <summary>
/// We could do object comparison here but performance isnt really a problem. This might be an issue one day!
/// </summary>
/// <param name="config"></param>
/// <returns></returns>
/// <returns>hash of the config</returns>
private string ToJson(FileConfiguration config)
{
var currentHash = JsonConvert.SerializeObject(config);

View File

@ -0,0 +1,8 @@
namespace Ocelot.Configuration.Repository
{
public interface IConsulPollerConfiguration
{
int Delay { get; }
}
}

View File

@ -0,0 +1,7 @@
namespace Ocelot.Configuration.Repository
{
public class InMemoryConsulPollerConfiguration : IConsulPollerConfiguration
{
public int Delay => 1000;
}
}

View File

@ -148,6 +148,7 @@ namespace Ocelot.DependencyInjection
// We add this here so that we can always inject something into the factory for IoC..
_services.AddSingleton<IServiceTracer, FakeServiceTracer>();
_services.TryAddSingleton<IConsulPollerConfiguration, InMemoryConsulPollerConfiguration>();
}
public IOcelotAdministrationBuilder AddAdministration(string path, string secret)

View File

@ -0,0 +1,10 @@
namespace Ocelot.Infrastructure
{
public class Wait
{
public static Waiter WaitFor(int milliSeconds)
{
return new Waiter(milliSeconds);
}
}
}

View File

@ -0,0 +1,47 @@
using System;
using System.Diagnostics;
namespace Ocelot.Infrastructure
{
public class Waiter
{
private readonly int _milliSeconds;
public Waiter(int milliSeconds)
{
_milliSeconds = milliSeconds;
}
public bool Until(Func<bool> condition)
{
var stopwatch = Stopwatch.StartNew();
var passed = false;
while (stopwatch.ElapsedMilliseconds < _milliSeconds)
{
if (condition.Invoke())
{
passed = true;
break;
}
}
return passed;
}
public bool Until<T>(Func<bool> condition)
{
var stopwatch = Stopwatch.StartNew();
var passed = false;
while (stopwatch.ElapsedMilliseconds < _milliSeconds)
{
if (condition.Invoke())
{
passed = true;
break;
}
}
return passed;
}
}
}