mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-19 09:38:14 +08:00
* #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:
@ -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);
|
||||
|
@ -0,0 +1,8 @@
|
||||
namespace Ocelot.Configuration.Repository
|
||||
{
|
||||
public interface IConsulPollerConfiguration
|
||||
{
|
||||
int Delay { get; }
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
namespace Ocelot.Configuration.Repository
|
||||
{
|
||||
public class InMemoryConsulPollerConfiguration : IConsulPollerConfiguration
|
||||
{
|
||||
public int Delay => 1000;
|
||||
}
|
||||
}
|
@ -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)
|
||||
|
10
src/Ocelot/Infrastructure/Wait.cs
Normal file
10
src/Ocelot/Infrastructure/Wait.cs
Normal file
@ -0,0 +1,10 @@
|
||||
namespace Ocelot.Infrastructure
|
||||
{
|
||||
public class Wait
|
||||
{
|
||||
public static Waiter WaitFor(int milliSeconds)
|
||||
{
|
||||
return new Waiter(milliSeconds);
|
||||
}
|
||||
}
|
||||
}
|
47
src/Ocelot/Infrastructure/Waiter.cs
Normal file
47
src/Ocelot/Infrastructure/Waiter.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user