Merge branch 'feature/fix-unstable-raft-tests' into develop

This commit is contained in:
Tom Gardham-Pallister 2018-05-09 18:41:11 +01:00
commit e32823c58b
15 changed files with 1930 additions and 1743 deletions

View File

@ -1,69 +1,76 @@
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Ocelot.Configuration.File; using Ocelot.Configuration.File;
using Ocelot.Configuration.Setter; using Ocelot.Configuration.Setter;
using Ocelot.Raft; using Ocelot.Raft;
using Rafty.Concensus; using Rafty.Concensus;
namespace Ocelot.Configuration namespace Ocelot.Configuration
{ {
using Repository; using Repository;
[Authorize] [Authorize]
[Route("configuration")] [Route("configuration")]
public class FileConfigurationController : Controller public class FileConfigurationController : Controller
{ {
private readonly IFileConfigurationRepository _repo; private readonly IFileConfigurationRepository _repo;
private readonly IFileConfigurationSetter _setter; private readonly IFileConfigurationSetter _setter;
private readonly IServiceProvider _provider; private readonly IServiceProvider _provider;
public FileConfigurationController(IFileConfigurationRepository repo, IFileConfigurationSetter setter, IServiceProvider provider) public FileConfigurationController(IFileConfigurationRepository repo, IFileConfigurationSetter setter, IServiceProvider provider)
{ {
_repo = repo; _repo = repo;
_setter = setter; _setter = setter;
_provider = provider; _provider = provider;
} }
[HttpGet] [HttpGet]
public async Task<IActionResult> Get() public async Task<IActionResult> Get()
{ {
var response = await _repo.Get(); var response = await _repo.Get();
if(response.IsError) if(response.IsError)
{ {
return new BadRequestObjectResult(response.Errors); return new BadRequestObjectResult(response.Errors);
} }
return new OkObjectResult(response.Data); return new OkObjectResult(response.Data);
} }
[HttpPost] [HttpPost]
public async Task<IActionResult> Post([FromBody]FileConfiguration fileConfiguration) public async Task<IActionResult> Post([FromBody]FileConfiguration fileConfiguration)
{ {
//todo - this code is a bit shit sort it out.. try
var test = _provider.GetService(typeof(INode)); {
if (test != null) //todo - this code is a bit shit sort it out..
{ var test = _provider.GetService(typeof(INode));
var node = (INode)test; if (test != null)
var result = node.Accept(new UpdateFileConfiguration(fileConfiguration)); {
if (result.GetType() == typeof(Rafty.Concensus.ErrorResponse<UpdateFileConfiguration>)) var node = (INode)test;
{ var result = node.Accept(new UpdateFileConfiguration(fileConfiguration));
return new BadRequestObjectResult("There was a problem. This error message sucks raise an issue in GitHub."); if (result.GetType() == typeof(Rafty.Concensus.ErrorResponse<UpdateFileConfiguration>))
} {
return new BadRequestObjectResult("There was a problem. This error message sucks raise an issue in GitHub.");
return new OkObjectResult(result.Command.Configuration); }
}
return new OkObjectResult(result.Command.Configuration);
var response = await _setter.Set(fileConfiguration); }
if (response.IsError) var response = await _setter.Set(fileConfiguration);
{
return new BadRequestObjectResult(response.Errors); if (response.IsError)
} {
return new BadRequestObjectResult(response.Errors);
return new OkObjectResult(fileConfiguration); }
}
} return new OkObjectResult(fileConfiguration);
} }
catch(Exception e)
{
return new BadRequestObjectResult($"{e.Message}:{e.StackTrace}");
}
}
}
}

View File

@ -40,7 +40,7 @@ namespace Ocelot.Configuration.Repository
_polling = true; _polling = true;
await Poll(); await Poll();
_polling = false; _polling = false;
}, null, 0, _config.Delay); }, null, _config.Delay, _config.Delay);
} }
private async Task Poll() private async Task Poll()

View File

@ -0,0 +1,15 @@
namespace Ocelot.Infrastructure
{
internal class DelayedMessage<T>
{
public DelayedMessage(T message, int delay)
{
Delay = delay;
Message = message;
}
public T Message { get; set; }
public int Delay { get; set; }
}
}

View File

@ -0,0 +1,10 @@
using System;
namespace Ocelot.Infrastructure
{
public interface IBus<T>
{
void Subscribe(Action<T> action);
void Publish(T message, int delay);
}
}

View File

@ -0,0 +1,47 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace Ocelot.Infrastructure
{
public class InMemoryBus<T> : IBus<T>
{
private readonly BlockingCollection<DelayedMessage<T>> _queue;
private readonly List<Action<T>> _subscriptions;
private Thread _processing;
public InMemoryBus()
{
_queue = new BlockingCollection<DelayedMessage<T>>();
_subscriptions = new List<Action<T>>();
_processing = new Thread(async () => await Process());
_processing.Start();
}
public void Subscribe(Action<T> action)
{
_subscriptions.Add(action);
}
public void Publish(T message, int delay)
{
var delayed = new DelayedMessage<T>(message, delay);
_queue.Add(delayed);
}
private async Task Process()
{
foreach(var delayedMessage in _queue.GetConsumingEnumerable())
{
await Task.Delay(delayedMessage.Delay);
foreach (var subscription in _subscriptions)
{
subscription(delayedMessage.Message);
}
}
}
}
}

View File

@ -3,61 +3,64 @@ namespace Ocelot.LoadBalancer.LoadBalancers
using System; using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Ocelot.Infrastructure;
using Ocelot.Middleware; using Ocelot.Middleware;
using Responses; using Responses;
using Values; using Values;
public class CookieStickySessions : ILoadBalancer, IDisposable public class CookieStickySessions : ILoadBalancer
{ {
private readonly int _expiryInMs; private readonly int _keyExpiryInMs;
private readonly string _key; private readonly string _key;
private readonly ILoadBalancer _loadBalancer; private readonly ILoadBalancer _loadBalancer;
private readonly ConcurrentDictionary<string, StickySession> _stored; private readonly ConcurrentDictionary<string, StickySession> _stored;
private readonly Timer _timer; private readonly IBus<StickySession> _bus;
private bool _expiring; private readonly object _lock = new object();
public CookieStickySessions(ILoadBalancer loadBalancer, string key, int expiryInMs) public CookieStickySessions(ILoadBalancer loadBalancer, string key, int keyExpiryInMs, IBus<StickySession> bus)
{ {
_bus = bus;
_key = key; _key = key;
_expiryInMs = expiryInMs; _keyExpiryInMs = keyExpiryInMs;
_loadBalancer = loadBalancer; _loadBalancer = loadBalancer;
_stored = new ConcurrentDictionary<string, StickySession>(); _stored = new ConcurrentDictionary<string, StickySession>();
_timer = new Timer(x => _bus.Subscribe(ss =>
{ {
if (_expiring) //todo - get test coverage for this.
if (_stored.TryGetValue(ss.Key, out var stickySession))
{ {
return; lock (_lock)
{
if (stickySession.Expiry < DateTime.UtcNow)
{
_stored.Remove(stickySession.Key, out _);
_loadBalancer.Release(stickySession.HostAndPort);
}
}
} }
});
_expiring = true;
Expire();
_expiring = false;
}, null, 0, 50);
}
public void Dispose()
{
_timer?.Dispose();
} }
public async Task<Response<ServiceHostAndPort>> Lease(DownstreamContext context) public async Task<Response<ServiceHostAndPort>> Lease(DownstreamContext context)
{ {
var value = context.HttpContext.Request.Cookies[_key]; var key = context.HttpContext.Request.Cookies[_key];
if (!string.IsNullOrEmpty(value) && _stored.ContainsKey(value)) lock (_lock)
{ {
var cached = _stored[value]; if (!string.IsNullOrEmpty(key) && _stored.ContainsKey(key))
{
var cached = _stored[key];
var updated = new StickySession(cached.HostAndPort, DateTime.UtcNow.AddMilliseconds(_expiryInMs)); var updated = new StickySession(cached.HostAndPort, DateTime.UtcNow.AddMilliseconds(_keyExpiryInMs), key);
_stored[value] = updated; _stored[key] = updated;
return new OkResponse<ServiceHostAndPort>(updated.HostAndPort); _bus.Publish(updated, _keyExpiryInMs);
return new OkResponse<ServiceHostAndPort>(updated.HostAndPort);
}
} }
var next = await _loadBalancer.Lease(context); var next = await _loadBalancer.Lease(context);
@ -67,9 +70,14 @@ namespace Ocelot.LoadBalancer.LoadBalancers
return new ErrorResponse<ServiceHostAndPort>(next.Errors); return new ErrorResponse<ServiceHostAndPort>(next.Errors);
} }
if (!string.IsNullOrEmpty(value) && !_stored.ContainsKey(value)) lock (_lock)
{ {
_stored[value] = new StickySession(next.Data, DateTime.UtcNow.AddMilliseconds(_expiryInMs)); if (!string.IsNullOrEmpty(key) && !_stored.ContainsKey(key))
{
var ss = new StickySession(next.Data, DateTime.UtcNow.AddMilliseconds(_keyExpiryInMs), key);
_stored[key] = ss;
_bus.Publish(ss, _keyExpiryInMs);
}
} }
return new OkResponse<ServiceHostAndPort>(next.Data); return new OkResponse<ServiceHostAndPort>(next.Data);
@ -78,16 +86,5 @@ namespace Ocelot.LoadBalancer.LoadBalancers
public void Release(ServiceHostAndPort hostAndPort) public void Release(ServiceHostAndPort hostAndPort)
{ {
} }
private void Expire()
{
var expired = _stored.Where(x => x.Value.Expiry < DateTime.UtcNow);
foreach (var expire in expired)
{
_stored.Remove(expire.Key, out _);
_loadBalancer.Release(expire.Value.HostAndPort);
}
}
} }
} }

View File

@ -1,5 +1,6 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using Ocelot.Configuration; using Ocelot.Configuration;
using Ocelot.Infrastructure;
using Ocelot.ServiceDiscovery; using Ocelot.ServiceDiscovery;
namespace Ocelot.LoadBalancer.LoadBalancers namespace Ocelot.LoadBalancer.LoadBalancers
@ -25,7 +26,8 @@ namespace Ocelot.LoadBalancer.LoadBalancers
return new LeastConnection(async () => await serviceProvider.Get(), reRoute.ServiceName); return new LeastConnection(async () => await serviceProvider.Get(), reRoute.ServiceName);
case nameof(CookieStickySessions): case nameof(CookieStickySessions):
var loadBalancer = new RoundRobin(async () => await serviceProvider.Get()); var loadBalancer = new RoundRobin(async () => await serviceProvider.Get());
return new CookieStickySessions(loadBalancer, reRoute.LoadBalancerOptions.Key, reRoute.LoadBalancerOptions.ExpiryInMs); var bus = new InMemoryBus<StickySession>();
return new CookieStickySessions(loadBalancer, reRoute.LoadBalancerOptions.Key, reRoute.LoadBalancerOptions.ExpiryInMs, bus);
default: default:
return new NoLoadBalancer(await serviceProvider.Get()); return new NoLoadBalancer(await serviceProvider.Get());
} }

View File

@ -5,14 +5,17 @@ namespace Ocelot.LoadBalancer.LoadBalancers
{ {
public class StickySession public class StickySession
{ {
public StickySession(ServiceHostAndPort hostAndPort, DateTime expiry) public StickySession(ServiceHostAndPort hostAndPort, DateTime expiry, string key)
{ {
HostAndPort = hostAndPort; HostAndPort = hostAndPort;
Expiry = expiry; Expiry = expiry;
Key = key;
} }
public ServiceHostAndPort HostAndPort { get; } public ServiceHostAndPort HostAndPort { get; }
public DateTime Expiry { get; } public DateTime Expiry { get; }
public string Key {get;}
} }
} }

View File

@ -1,128 +1,132 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Net.Http; using System.Net.Http;
using Newtonsoft.Json; using Newtonsoft.Json;
using Ocelot.Configuration; using Ocelot.Configuration;
using Ocelot.Middleware; using Ocelot.Middleware;
using Rafty.Concensus; using Rafty.Concensus;
using Rafty.FiniteStateMachine; using Rafty.FiniteStateMachine;
namespace Ocelot.Raft namespace Ocelot.Raft
{ {
[ExcludeFromCoverage] [ExcludeFromCoverage]
public class HttpPeer : IPeer public class HttpPeer : IPeer
{ {
private readonly string _hostAndPort; private readonly string _hostAndPort;
private readonly HttpClient _httpClient; private readonly HttpClient _httpClient;
private readonly JsonSerializerSettings _jsonSerializerSettings; private readonly JsonSerializerSettings _jsonSerializerSettings;
private readonly string _baseSchemeUrlAndPort; private readonly string _baseSchemeUrlAndPort;
private BearerToken _token; private BearerToken _token;
private readonly IInternalConfiguration _config; private readonly IInternalConfiguration _config;
private readonly IIdentityServerConfiguration _identityServerConfiguration; private readonly IIdentityServerConfiguration _identityServerConfiguration;
public HttpPeer(string hostAndPort, HttpClient httpClient, IBaseUrlFinder finder, IInternalConfiguration config, IIdentityServerConfiguration identityServerConfiguration) public HttpPeer(string hostAndPort, HttpClient httpClient, IBaseUrlFinder finder, IInternalConfiguration config, IIdentityServerConfiguration identityServerConfiguration)
{ {
_identityServerConfiguration = identityServerConfiguration; _identityServerConfiguration = identityServerConfiguration;
_config = config; _config = config;
Id = hostAndPort; Id = hostAndPort;
_hostAndPort = hostAndPort; _hostAndPort = hostAndPort;
_httpClient = httpClient; _httpClient = httpClient;
_jsonSerializerSettings = new JsonSerializerSettings() { _jsonSerializerSettings = new JsonSerializerSettings() {
TypeNameHandling = TypeNameHandling.All TypeNameHandling = TypeNameHandling.All
}; };
_baseSchemeUrlAndPort = finder.Find(); _baseSchemeUrlAndPort = finder.Find();
} }
public string Id {get; private set;} public string Id {get; private set;}
public RequestVoteResponse Request(RequestVote requestVote) public RequestVoteResponse Request(RequestVote requestVote)
{ {
if(_token == null) if(_token == null)
{ {
SetToken(); SetToken();
} }
var json = JsonConvert.SerializeObject(requestVote, _jsonSerializerSettings); var json = JsonConvert.SerializeObject(requestVote, _jsonSerializerSettings);
var content = new StringContent(json); var content = new StringContent(json);
content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
var response = _httpClient.PostAsync($"{_hostAndPort}/administration/raft/requestvote", content).GetAwaiter().GetResult(); var response = _httpClient.PostAsync($"{_hostAndPort}/administration/raft/requestvote", content).GetAwaiter().GetResult();
if(response.IsSuccessStatusCode) if(response.IsSuccessStatusCode)
{ {
return JsonConvert.DeserializeObject<RequestVoteResponse>(response.Content.ReadAsStringAsync().GetAwaiter().GetResult(), _jsonSerializerSettings); return JsonConvert.DeserializeObject<RequestVoteResponse>(response.Content.ReadAsStringAsync().GetAwaiter().GetResult(), _jsonSerializerSettings);
} }
else else
{ {
return new RequestVoteResponse(false, requestVote.Term); return new RequestVoteResponse(false, requestVote.Term);
} }
} }
public AppendEntriesResponse Request(AppendEntries appendEntries) public AppendEntriesResponse Request(AppendEntries appendEntries)
{ {
try try
{ {
if(_token == null) if(_token == null)
{ {
SetToken(); SetToken();
} }
var json = JsonConvert.SerializeObject(appendEntries, _jsonSerializerSettings); var json = JsonConvert.SerializeObject(appendEntries, _jsonSerializerSettings);
var content = new StringContent(json); var content = new StringContent(json);
content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
var response = _httpClient.PostAsync($"{_hostAndPort}/administration/raft/appendEntries", content).GetAwaiter().GetResult(); var response = _httpClient.PostAsync($"{_hostAndPort}/administration/raft/appendEntries", content).GetAwaiter().GetResult();
if(response.IsSuccessStatusCode) if(response.IsSuccessStatusCode)
{ {
return JsonConvert.DeserializeObject<AppendEntriesResponse>(response.Content.ReadAsStringAsync().GetAwaiter().GetResult(),_jsonSerializerSettings); return JsonConvert.DeserializeObject<AppendEntriesResponse>(response.Content.ReadAsStringAsync().GetAwaiter().GetResult(),_jsonSerializerSettings);
} }
else else
{ {
return new AppendEntriesResponse(appendEntries.Term, false); return new AppendEntriesResponse(appendEntries.Term, false);
} }
} }
catch(Exception ex) catch(Exception ex)
{ {
Console.WriteLine(ex); Console.WriteLine(ex);
return new AppendEntriesResponse(appendEntries.Term, false); return new AppendEntriesResponse(appendEntries.Term, false);
} }
} }
public Response<T> Request<T>(T command) public Response<T> Request<T>(T command)
where T : ICommand where T : ICommand
{ {
if(_token == null) Console.WriteLine("SENDING REQUEST....");
{ if(_token == null)
SetToken(); {
} SetToken();
}
var json = JsonConvert.SerializeObject(command, _jsonSerializerSettings);
var content = new StringContent(json); var json = JsonConvert.SerializeObject(command, _jsonSerializerSettings);
content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); var content = new StringContent(json);
var response = _httpClient.PostAsync($"{_hostAndPort}/administration/raft/command", content).GetAwaiter().GetResult(); content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
if(response.IsSuccessStatusCode) var response = _httpClient.PostAsync($"{_hostAndPort}/administration/raft/command", content).GetAwaiter().GetResult();
{ if(response.IsSuccessStatusCode)
return JsonConvert.DeserializeObject<OkResponse<T>>(response.Content.ReadAsStringAsync().GetAwaiter().GetResult(), _jsonSerializerSettings); {
} Console.WriteLine("REQUEST OK....");
else var okResponse = JsonConvert.DeserializeObject<OkResponse<ICommand>>(response.Content.ReadAsStringAsync().GetAwaiter().GetResult(), _jsonSerializerSettings);
{ return new OkResponse<T>((T)okResponse.Command);
return new ErrorResponse<T>(response.Content.ReadAsStringAsync().GetAwaiter().GetResult(), command); }
} else
} {
Console.WriteLine("REQUEST NOT OK....");
private void SetToken() return new ErrorResponse<T>(response.Content.ReadAsStringAsync().GetAwaiter().GetResult(), command);
{ }
var tokenUrl = $"{_baseSchemeUrlAndPort}{_config.AdministrationPath}/connect/token"; }
var formData = new List<KeyValuePair<string, string>>
{ private void SetToken()
new KeyValuePair<string, string>("client_id", _identityServerConfiguration.ApiName), {
new KeyValuePair<string, string>("client_secret", _identityServerConfiguration.ApiSecret), var tokenUrl = $"{_baseSchemeUrlAndPort}{_config.AdministrationPath}/connect/token";
new KeyValuePair<string, string>("scope", _identityServerConfiguration.ApiName), var formData = new List<KeyValuePair<string, string>>
new KeyValuePair<string, string>("grant_type", "client_credentials") {
}; new KeyValuePair<string, string>("client_id", _identityServerConfiguration.ApiName),
var content = new FormUrlEncodedContent(formData); new KeyValuePair<string, string>("client_secret", _identityServerConfiguration.ApiSecret),
var response = _httpClient.PostAsync(tokenUrl, content).GetAwaiter().GetResult(); new KeyValuePair<string, string>("scope", _identityServerConfiguration.ApiName),
var responseContent = response.Content.ReadAsStringAsync().GetAwaiter().GetResult(); new KeyValuePair<string, string>("grant_type", "client_credentials")
response.EnsureSuccessStatusCode(); };
_token = JsonConvert.DeserializeObject<BearerToken>(responseContent); var content = new FormUrlEncodedContent(formData);
_httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(_token.TokenType, _token.AccessToken); var response = _httpClient.PostAsync(tokenUrl, content).GetAwaiter().GetResult();
} var responseContent = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
} response.EnsureSuccessStatusCode();
} _token = JsonConvert.DeserializeObject<BearerToken>(responseContent);
_httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(_token.TokenType, _token.AccessToken);
}
}
}

File diff suppressed because it is too large Load Diff

View File

@ -76,16 +76,16 @@ namespace Ocelot.Benchmarks
response.EnsureSuccessStatusCode(); response.EnsureSuccessStatusCode();
} }
// * Summary* /* * Summary*
// BenchmarkDotNet = v0.10.13, OS = macOS 10.12.6 (16G1212) [Darwin 16.7.0] BenchmarkDotNet = v0.10.13, OS = macOS 10.12.6 (16G1212) [Darwin 16.7.0]
// Intel Core i5-4278U CPU 2.60GHz(Haswell), 1 CPU, 4 logical cores and 2 physical cores Intel Core i5-4278U CPU 2.60GHz(Haswell), 1 CPU, 4 logical cores and 2 physical cores
//.NET Core SDK = 2.1.4 .NET Core SDK = 2.1.4
// [Host] : .NET Core 2.0.6 (CoreCLR 4.6.0.0, CoreFX 4.6.26212.01), 64bit RyuJIT [Host] : .NET Core 2.0.6 (CoreCLR 4.6.0.0, CoreFX 4.6.26212.01), 64bit RyuJIT
// DefaultJob : .NET Core 2.0.6 (CoreCLR 4.6.0.0, CoreFX 4.6.26212.01), 64bit RyuJIT DefaultJob : .NET Core 2.0.6 (CoreCLR 4.6.0.0, CoreFX 4.6.26212.01), 64bit RyuJIT
// Method | Mean | Error | StdDev | StdErr | Min | Q1 | Median | Q3 | Max | Op/s | Scaled | Gen 0 | Gen 1 | Allocated | Method | Mean | Error | StdDev | StdErr | Min | Q1 | Median | Q3 | Max | Op/s | Scaled | Gen 0 | Gen 1 | Allocated |
// --------- |---------:|----------:|----------:|----------:|---------:|---------:|---------:|---------:|---------:|------:|-------:|--------:|-------:|----------:| --------- |---------:|----------:|----------:|----------:|---------:|---------:|---------:|---------:|---------:|------:|-------:|--------:|-------:|----------:|
// Baseline | 2.102 ms | 0.0292 ms | 0.0273 ms | 0.0070 ms | 2.063 ms | 2.080 ms | 2.093 ms | 2.122 ms | 2.152 ms | 475.8 | 1.00 | 31.2500 | 3.9063 | 1.63 KB | Baseline | 2.102 ms | 0.0292 ms | 0.0273 ms | 0.0070 ms | 2.063 ms | 2.080 ms | 2.093 ms | 2.122 ms | 2.152 ms | 475.8 | 1.00 | 31.2500 | 3.9063 | 1.63 KB |*/
private void GivenOcelotIsRunning(string url) private void GivenOcelotIsRunning(string url)
{ {

View File

@ -1,393 +1,450 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Net.Http; using System.Net.Http;
using System.Net.Http.Headers; using System.Net.Http.Headers;
using System.Threading; using System.Threading;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json; using Newtonsoft.Json;
using Ocelot.Configuration.File; using Ocelot.Configuration.File;
using Ocelot.Raft; using Ocelot.Raft;
using Rafty.Concensus; using Rafty.Concensus;
using Rafty.Infrastructure; using Rafty.Infrastructure;
using Shouldly; using Shouldly;
using Xunit; using Xunit;
using static Rafty.Infrastructure.Wait; using static Rafty.Infrastructure.Wait;
using Microsoft.Data.Sqlite; using Microsoft.Data.Sqlite;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Ocelot.DependencyInjection; using Ocelot.DependencyInjection;
using Ocelot.Middleware; using Ocelot.Middleware;
namespace Ocelot.IntegrationTests namespace Ocelot.IntegrationTests
{ {
public class RaftTests : IDisposable using Xunit.Abstractions;
{
private readonly List<IWebHost> _builders; public class RaftTests : IDisposable
private readonly List<IWebHostBuilder> _webHostBuilders; {
private readonly List<Thread> _threads; private readonly List<IWebHost> _builders;
private FilePeers _peers; private readonly List<IWebHostBuilder> _webHostBuilders;
private readonly HttpClient _httpClient; private readonly List<Thread> _threads;
private readonly HttpClient _httpClientForAssertions; private FilePeers _peers;
private BearerToken _token; private readonly HttpClient _httpClient;
private HttpResponseMessage _response; private readonly HttpClient _httpClientForAssertions;
private static readonly object _lock = new object(); private BearerToken _token;
private HttpResponseMessage _response;
public RaftTests() private static readonly object _lock = new object();
{ private ITestOutputHelper _output;
_httpClientForAssertions = new HttpClient();
_httpClient = new HttpClient(); public RaftTests(ITestOutputHelper output)
var ocelotBaseUrl = "http://localhost:5000"; {
_httpClient.BaseAddress = new Uri(ocelotBaseUrl); _output = output;
_webHostBuilders = new List<IWebHostBuilder>(); _httpClientForAssertions = new HttpClient();
_builders = new List<IWebHost>(); _httpClient = new HttpClient();
_threads = new List<Thread>(); var ocelotBaseUrl = "http://localhost:5000";
} _httpClient.BaseAddress = new Uri(ocelotBaseUrl);
_webHostBuilders = new List<IWebHostBuilder>();
public void Dispose() _builders = new List<IWebHost>();
{ _threads = new List<Thread>();
foreach (var builder in _builders) }
{
builder?.Dispose(); [Fact]
} public void should_persist_command_to_five_servers()
{
foreach (var peer in _peers.Peers) var configuration = new FileConfiguration
{ {
File.Delete(peer.HostAndPort.Replace("/","").Replace(":","")); GlobalConfiguration = new FileGlobalConfiguration
File.Delete($"{peer.HostAndPort.Replace("/","").Replace(":","")}.db"); {
} }
} };
[Fact(Skip = "This tests is flakey at the moment so ignoring will be fixed long term see https://github.com/TomPallister/Ocelot/issues/245")] var updatedConfiguration = new FileConfiguration
public void should_persist_command_to_five_servers() {
{ GlobalConfiguration = new FileGlobalConfiguration
var configuration = new FileConfiguration {
{ },
GlobalConfiguration = new FileGlobalConfiguration ReRoutes = new List<FileReRoute>()
{ {
} new FileReRoute()
}; {
DownstreamHostAndPorts = new List<FileHostAndPort>
var updatedConfiguration = new FileConfiguration {
{ new FileHostAndPort
GlobalConfiguration = new FileGlobalConfiguration {
{ Host = "127.0.0.1",
}, Port = 80,
ReRoutes = new List<FileReRoute>() }
{ },
new FileReRoute() DownstreamScheme = "http",
{ DownstreamPathTemplate = "/geoffrey",
DownstreamHostAndPorts = new List<FileHostAndPort> UpstreamHttpMethod = new List<string> { "get" },
{ UpstreamPathTemplate = "/"
new FileHostAndPort },
{ new FileReRoute()
Host = "127.0.0.1", {
Port = 80, DownstreamHostAndPorts = new List<FileHostAndPort>
} {
}, new FileHostAndPort
DownstreamScheme = "http", {
DownstreamPathTemplate = "/geoffrey", Host = "123.123.123",
UpstreamHttpMethod = new List<string> { "get" }, Port = 443,
UpstreamPathTemplate = "/" }
}, },
new FileReRoute() DownstreamScheme = "https",
{ DownstreamPathTemplate = "/blooper/{productId}",
DownstreamHostAndPorts = new List<FileHostAndPort> UpstreamHttpMethod = new List<string> { "post" },
{ UpstreamPathTemplate = "/test"
new FileHostAndPort }
{ }
Host = "123.123.123", };
Port = 443,
} var command = new UpdateFileConfiguration(updatedConfiguration);
}, GivenThereIsAConfiguration(configuration);
DownstreamScheme = "https", GivenFiveServersAreRunning();
DownstreamPathTemplate = "/blooper/{productId}", GivenIHaveAnOcelotToken("/administration");
UpstreamHttpMethod = new List<string> { "post" }, WhenISendACommandIntoTheCluster(command);
UpstreamPathTemplate = "/test" Thread.Sleep(5000);
} ThenTheCommandIsReplicatedToAllStateMachines(command);
} }
};
[Fact]
var command = new UpdateFileConfiguration(updatedConfiguration); public void should_persist_command_to_five_servers_when_using_administration_api()
GivenThereIsAConfiguration(configuration); {
GivenFiveServersAreRunning(); var configuration = new FileConfiguration
GivenALeaderIsElected(); {
GivenIHaveAnOcelotToken("/administration"); };
WhenISendACommandIntoTheCluster(command);
ThenTheCommandIsReplicatedToAllStateMachines(command); var updatedConfiguration = new FileConfiguration
} {
ReRoutes = new List<FileReRoute>()
[Fact(Skip = "This tests is flakey at the moment so ignoring will be fixed long term see https://github.com/TomPallister/Ocelot/issues/245")] {
public void should_persist_command_to_five_servers_when_using_administration_api() new FileReRoute()
{ {
var configuration = new FileConfiguration DownstreamHostAndPorts = new List<FileHostAndPort>
{ {
}; new FileHostAndPort
{
var updatedConfiguration = new FileConfiguration Host = "127.0.0.1",
{ Port = 80,
ReRoutes = new List<FileReRoute>() }
{ },
new FileReRoute() DownstreamScheme = "http",
{ DownstreamPathTemplate = "/geoffrey",
DownstreamHostAndPorts = new List<FileHostAndPort> UpstreamHttpMethod = new List<string> { "get" },
{ UpstreamPathTemplate = "/"
new FileHostAndPort },
{ new FileReRoute()
Host = "127.0.0.1", {
Port = 80, DownstreamHostAndPorts = new List<FileHostAndPort>
} {
}, new FileHostAndPort
DownstreamScheme = "http", {
DownstreamPathTemplate = "/geoffrey", Host = "123.123.123",
UpstreamHttpMethod = new List<string> { "get" }, Port = 443,
UpstreamPathTemplate = "/" }
}, },
new FileReRoute() DownstreamScheme = "https",
{ DownstreamPathTemplate = "/blooper/{productId}",
DownstreamHostAndPorts = new List<FileHostAndPort> UpstreamHttpMethod = new List<string> { "post" },
{ UpstreamPathTemplate = "/test"
new FileHostAndPort }
{ }
Host = "123.123.123", };
Port = 443,
} var command = new UpdateFileConfiguration(updatedConfiguration);
}, GivenThereIsAConfiguration(configuration);
DownstreamScheme = "https", GivenFiveServersAreRunning();
DownstreamPathTemplate = "/blooper/{productId}", GivenIHaveAnOcelotToken("/administration");
UpstreamHttpMethod = new List<string> { "post" }, GivenIHaveAddedATokenToMyRequest();
UpstreamPathTemplate = "/test" WhenIPostOnTheApiGateway("/administration/configuration", updatedConfiguration);
} ThenTheCommandIsReplicatedToAllStateMachines(command);
} }
};
private void WhenISendACommandIntoTheCluster(UpdateFileConfiguration command)
var command = new UpdateFileConfiguration(updatedConfiguration); {
GivenThereIsAConfiguration(configuration); bool SendCommand()
GivenFiveServersAreRunning(); {
GivenALeaderIsElected(); try
GivenIHaveAnOcelotToken("/administration"); {
GivenIHaveAddedATokenToMyRequest(); var p = _peers.Peers.First();
WhenIPostOnTheApiGateway("/administration/configuration", updatedConfiguration); var json = JsonConvert.SerializeObject(command, new JsonSerializerSettings()
ThenTheCommandIsReplicatedToAllStateMachines(command); {
} TypeNameHandling = TypeNameHandling.All
});
private void WhenISendACommandIntoTheCluster(UpdateFileConfiguration command) var httpContent = new StringContent(json);
{ httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
var p = _peers.Peers.First(); using (var httpClient = new HttpClient())
var json = JsonConvert.SerializeObject(command,new JsonSerializerSettings() { {
TypeNameHandling = TypeNameHandling.All httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _token.AccessToken);
}); var response = httpClient.PostAsync($"{p.HostAndPort}/administration/raft/command", httpContent).GetAwaiter().GetResult();
var httpContent = new StringContent(json); response.EnsureSuccessStatusCode();
httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); var content = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
using(var httpClient = new HttpClient())
{ var errorResult = JsonConvert.DeserializeObject<ErrorResponse<UpdateFileConfiguration>>(content);
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _token.AccessToken);
var response = httpClient.PostAsync($"{p.HostAndPort}/administration/raft/command", httpContent).GetAwaiter().GetResult(); if (!string.IsNullOrEmpty(errorResult.Error))
response.EnsureSuccessStatusCode(); {
var content = response.Content.ReadAsStringAsync().GetAwaiter().GetResult(); return false;
var result = JsonConvert.DeserializeObject<OkResponse<UpdateFileConfiguration>>(content); }
result.Command.Configuration.ReRoutes.Count.ShouldBe(2);
} var okResult = JsonConvert.DeserializeObject<OkResponse<UpdateFileConfiguration>>(content);
//dirty sleep to make sure command replicated... if (okResult.Command.Configuration.ReRoutes.Count == 2)
var stopwatch = Stopwatch.StartNew(); {
while(stopwatch.ElapsedMilliseconds < 10000) return true;
{ }
} }
}
return false;
private void ThenTheCommandIsReplicatedToAllStateMachines(UpdateFileConfiguration expecteds) }
{ catch (Exception e)
//dirty sleep to give a chance to replicate... {
var stopwatch = Stopwatch.StartNew(); Console.WriteLine(e);
while(stopwatch.ElapsedMilliseconds < 2000) return false;
{ }
} }
bool CommandCalledOnAllStateMachines() var commandSent = WaitFor(20000).Until(() => SendCommand());
{ commandSent.ShouldBeTrue();
try }
{
var passed = 0; private void ThenTheCommandIsReplicatedToAllStateMachines(UpdateFileConfiguration expecteds)
foreach (var peer in _peers.Peers) {
{ bool CommandCalledOnAllStateMachines()
var path = $"{peer.HostAndPort.Replace("/","").Replace(":","")}.db"; {
using(var connection = new SqliteConnection($"Data Source={path};")) try
{ {
connection.Open(); var passed = 0;
var sql = @"select count(id) from logs"; foreach (var peer in _peers.Peers)
using(var command = new SqliteCommand(sql, connection)) {
{ var path = $"{peer.HostAndPort.Replace("/","").Replace(":","")}.db";
var index = Convert.ToInt32(command.ExecuteScalar()); using(var connection = new SqliteConnection($"Data Source={path};"))
index.ShouldBe(1); {
} connection.Open();
} var sql = @"select count(id) from logs";
using(var command = new SqliteCommand(sql, connection))
_httpClientForAssertions.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _token.AccessToken); {
var result = _httpClientForAssertions.GetAsync($"{peer.HostAndPort}/administration/configuration").Result; var index = Convert.ToInt32(command.ExecuteScalar());
var json = result.Content.ReadAsStringAsync().Result; index.ShouldBe(1);
var response = JsonConvert.DeserializeObject<FileConfiguration>(json, new JsonSerializerSettings{TypeNameHandling = TypeNameHandling.All}); }
response.GlobalConfiguration.RequestIdKey.ShouldBe(expecteds.Configuration.GlobalConfiguration.RequestIdKey); }
response.GlobalConfiguration.ServiceDiscoveryProvider.Host.ShouldBe(expecteds.Configuration.GlobalConfiguration.ServiceDiscoveryProvider.Host);
response.GlobalConfiguration.ServiceDiscoveryProvider.Port.ShouldBe(expecteds.Configuration.GlobalConfiguration.ServiceDiscoveryProvider.Port); _httpClientForAssertions.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _token.AccessToken);
var result = _httpClientForAssertions.GetAsync($"{peer.HostAndPort}/administration/configuration").Result;
for (var i = 0; i < response.ReRoutes.Count; i++) var json = result.Content.ReadAsStringAsync().Result;
{ var response = JsonConvert.DeserializeObject<FileConfiguration>(json, new JsonSerializerSettings{TypeNameHandling = TypeNameHandling.All});
for (var j = 0; j < response.ReRoutes[i].DownstreamHostAndPorts.Count; j++) response.GlobalConfiguration.RequestIdKey.ShouldBe(expecteds.Configuration.GlobalConfiguration.RequestIdKey);
{ response.GlobalConfiguration.ServiceDiscoveryProvider.Host.ShouldBe(expecteds.Configuration.GlobalConfiguration.ServiceDiscoveryProvider.Host);
var res = response.ReRoutes[i].DownstreamHostAndPorts[j]; response.GlobalConfiguration.ServiceDiscoveryProvider.Port.ShouldBe(expecteds.Configuration.GlobalConfiguration.ServiceDiscoveryProvider.Port);
var expected = expecteds.Configuration.ReRoutes[i].DownstreamHostAndPorts[j];
res.Host.ShouldBe(expected.Host); for (var i = 0; i < response.ReRoutes.Count; i++)
res.Port.ShouldBe(expected.Port); {
} for (var j = 0; j < response.ReRoutes[i].DownstreamHostAndPorts.Count; j++)
{
response.ReRoutes[i].DownstreamPathTemplate.ShouldBe(expecteds.Configuration.ReRoutes[i].DownstreamPathTemplate); var res = response.ReRoutes[i].DownstreamHostAndPorts[j];
response.ReRoutes[i].DownstreamScheme.ShouldBe(expecteds.Configuration.ReRoutes[i].DownstreamScheme); var expected = expecteds.Configuration.ReRoutes[i].DownstreamHostAndPorts[j];
response.ReRoutes[i].UpstreamPathTemplate.ShouldBe(expecteds.Configuration.ReRoutes[i].UpstreamPathTemplate); res.Host.ShouldBe(expected.Host);
response.ReRoutes[i].UpstreamHttpMethod.ShouldBe(expecteds.Configuration.ReRoutes[i].UpstreamHttpMethod); res.Port.ShouldBe(expected.Port);
} }
passed++; response.ReRoutes[i].DownstreamPathTemplate.ShouldBe(expecteds.Configuration.ReRoutes[i].DownstreamPathTemplate);
} response.ReRoutes[i].DownstreamScheme.ShouldBe(expecteds.Configuration.ReRoutes[i].DownstreamScheme);
response.ReRoutes[i].UpstreamPathTemplate.ShouldBe(expecteds.Configuration.ReRoutes[i].UpstreamPathTemplate);
return passed == 5; response.ReRoutes[i].UpstreamHttpMethod.ShouldBe(expecteds.Configuration.ReRoutes[i].UpstreamHttpMethod);
} }
catch(Exception e)
{ passed++;
Console.WriteLine(e); }
return false;
} return passed == 5;
} }
catch(Exception e)
var commandOnAllStateMachines = WaitFor(20000).Until(() => CommandCalledOnAllStateMachines()); {
commandOnAllStateMachines.ShouldBeTrue(); //_output.WriteLine($"{e.Message}, {e.StackTrace}");
} //Console.WriteLine(e);
return false;
private void WhenIPostOnTheApiGateway(string url, FileConfiguration updatedConfiguration) }
{ }
var json = JsonConvert.SerializeObject(updatedConfiguration);
var content = new StringContent(json); var commandOnAllStateMachines = WaitFor(20000).Until(() => CommandCalledOnAllStateMachines());
content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); commandOnAllStateMachines.ShouldBeTrue();
_response = _httpClient.PostAsync(url, content).Result; }
}
private void WhenIPostOnTheApiGateway(string url, FileConfiguration updatedConfiguration)
private void GivenIHaveAddedATokenToMyRequest() {
{ bool SendCommand()
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _token.AccessToken); {
} var json = JsonConvert.SerializeObject(updatedConfiguration);
var content = new StringContent(json);
private void GivenIHaveAnOcelotToken(string adminPath) content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
{ _response = _httpClient.PostAsync(url, content).Result;
var tokenUrl = $"{adminPath}/connect/token"; var responseContent = _response.Content.ReadAsStringAsync().Result;
var formData = new List<KeyValuePair<string, string>>
{ if(responseContent == "There was a problem. This error message sucks raise an issue in GitHub.")
new KeyValuePair<string, string>("client_id", "admin"), {
new KeyValuePair<string, string>("client_secret", "secret"), return false;
new KeyValuePair<string, string>("scope", "admin"), }
new KeyValuePair<string, string>("grant_type", "client_credentials")
}; if(string.IsNullOrEmpty(responseContent))
var content = new FormUrlEncodedContent(formData); {
return false;
var response = _httpClient.PostAsync(tokenUrl, content).Result; }
var responseContent = response.Content.ReadAsStringAsync().Result;
response.EnsureSuccessStatusCode(); return _response.IsSuccessStatusCode;
_token = JsonConvert.DeserializeObject<BearerToken>(responseContent); }
var configPath = $"{adminPath}/.well-known/openid-configuration";
response = _httpClient.GetAsync(configPath).Result; var commandSent = WaitFor(20000).Until(() => SendCommand());
response.EnsureSuccessStatusCode(); commandSent.ShouldBeTrue();
} }
private void GivenThereIsAConfiguration(FileConfiguration fileConfiguration) private void GivenIHaveAddedATokenToMyRequest()
{ {
var configurationPath = $"{Directory.GetCurrentDirectory()}/ocelot.json"; _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _token.AccessToken);
}
var jsonConfiguration = JsonConvert.SerializeObject(fileConfiguration);
private void GivenIHaveAnOcelotToken(string adminPath)
if (File.Exists(configurationPath)) {
{ bool AddToken()
File.Delete(configurationPath); {
} try
{
File.WriteAllText(configurationPath, jsonConfiguration); var tokenUrl = $"{adminPath}/connect/token";
var formData = new List<KeyValuePair<string, string>>
var text = File.ReadAllText(configurationPath); {
new KeyValuePair<string, string>("client_id", "admin"),
configurationPath = $"{AppContext.BaseDirectory}/ocelot.json"; new KeyValuePair<string, string>("client_secret", "secret"),
new KeyValuePair<string, string>("scope", "admin"),
if (File.Exists(configurationPath)) new KeyValuePair<string, string>("grant_type", "client_credentials")
{ };
File.Delete(configurationPath); var content = new FormUrlEncodedContent(formData);
}
var response = _httpClient.PostAsync(tokenUrl, content).Result;
File.WriteAllText(configurationPath, jsonConfiguration); var responseContent = response.Content.ReadAsStringAsync().Result;
if(!response.IsSuccessStatusCode)
text = File.ReadAllText(configurationPath); {
} return false;
}
private void GivenAServerIsRunning(string url)
{ _token = JsonConvert.DeserializeObject<BearerToken>(responseContent);
lock(_lock) var configPath = $"{adminPath}/.well-known/openid-configuration";
{ response = _httpClient.GetAsync(configPath).Result;
IWebHostBuilder webHostBuilder = new WebHostBuilder(); return response.IsSuccessStatusCode;
webHostBuilder.UseUrls(url) }
.UseKestrel() catch(Exception)
.UseContentRoot(Directory.GetCurrentDirectory()) {
.ConfigureAppConfiguration((hostingContext, config) => return false;
{ }
config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath); }
var env = hostingContext.HostingEnvironment;
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) var addToken = WaitFor(20000).Until(() => AddToken());
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true); addToken.ShouldBeTrue();
config.AddJsonFile("ocelot.json"); }
config.AddJsonFile("peers.json", optional: true, reloadOnChange: true);
#pragma warning disable CS0618 private void GivenThereIsAConfiguration(FileConfiguration fileConfiguration)
config.AddOcelotBaseUrl(url); {
#pragma warning restore CS0618 var configurationPath = $"{Directory.GetCurrentDirectory()}/ocelot.json";
config.AddEnvironmentVariables();
}) var jsonConfiguration = JsonConvert.SerializeObject(fileConfiguration);
.ConfigureServices(x =>
{ if (File.Exists(configurationPath))
x.AddSingleton(new NodeId(url)); {
x File.Delete(configurationPath);
.AddOcelot() }
.AddAdministration("/administration", "secret")
.AddRafty(); File.WriteAllText(configurationPath, jsonConfiguration);
})
.Configure(app => var text = File.ReadAllText(configurationPath);
{
app.UseOcelot().Wait(); configurationPath = $"{AppContext.BaseDirectory}/ocelot.json";
});
if (File.Exists(configurationPath))
var builder = webHostBuilder.Build(); {
builder.Start(); File.Delete(configurationPath);
}
_webHostBuilders.Add(webHostBuilder);
_builders.Add(builder); File.WriteAllText(configurationPath, jsonConfiguration);
}
} text = File.ReadAllText(configurationPath);
}
private void GivenFiveServersAreRunning()
{ private void GivenAServerIsRunning(string url)
var bytes = File.ReadAllText("peers.json"); {
_peers = JsonConvert.DeserializeObject<FilePeers>(bytes); lock(_lock)
{
foreach (var peer in _peers.Peers) IWebHostBuilder webHostBuilder = new WebHostBuilder();
{ webHostBuilder.UseUrls(url)
var thread = new Thread(() => GivenAServerIsRunning(peer.HostAndPort)); .UseKestrel()
thread.Start(); .UseContentRoot(Directory.GetCurrentDirectory())
_threads.Add(thread); .ConfigureAppConfiguration((hostingContext, config) =>
} {
} config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath);
var env = hostingContext.HostingEnvironment;
private void GivenALeaderIsElected() config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
{ .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
//dirty sleep to make sure we have a leader config.AddJsonFile("ocelot.json");
var stopwatch = Stopwatch.StartNew(); config.AddJsonFile("peers.json", optional: true, reloadOnChange: true);
while(stopwatch.ElapsedMilliseconds < 20000) #pragma warning disable CS0618
{ config.AddOcelotBaseUrl(url);
} #pragma warning restore CS0618
} config.AddEnvironmentVariables();
} })
} .ConfigureServices(x =>
{
x.AddSingleton(new NodeId(url));
x
.AddOcelot()
.AddAdministration("/administration", "secret")
.AddRafty();
})
.Configure(app =>
{
app.UseOcelot().Wait();
});
var builder = webHostBuilder.Build();
builder.Start();
_webHostBuilders.Add(webHostBuilder);
_builders.Add(builder);
}
}
private void GivenFiveServersAreRunning()
{
var bytes = File.ReadAllText("peers.json");
_peers = JsonConvert.DeserializeObject<FilePeers>(bytes);
foreach (var peer in _peers.Peers)
{
File.Delete(peer.HostAndPort.Replace("/", "").Replace(":", ""));
File.Delete($"{peer.HostAndPort.Replace("/", "").Replace(":", "")}.db");
var thread = new Thread(() => GivenAServerIsRunning(peer.HostAndPort));
thread.Start();
_threads.Add(thread);
}
}
public void Dispose()
{
foreach (var builder in _builders)
{
builder?.Dispose();
}
foreach (var peer in _peers.Peers)
{
try
{
File.Delete(peer.HostAndPort.Replace("/", "").Replace(":", ""));
File.Delete($"{peer.HostAndPort.Replace("/", "").Replace(":", "")}.db");
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
}
}
}

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 readonly ConsulFileConfigurationPoller _poller;
private Mock<IOcelotLoggerFactory> _factory; private Mock<IOcelotLoggerFactory> _factory;
private Mock<IFileConfigurationRepository> _repo; private readonly Mock<IFileConfigurationRepository> _repo;
private Mock<IFileConfigurationSetter> _setter; private readonly Mock<IFileConfigurationSetter> _setter;
private FileConfiguration _fileConfig; private readonly 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();
}
}
}

View File

@ -0,0 +1,40 @@
using System.Threading.Tasks;
using Ocelot.Infrastructure;
using Shouldly;
using Xunit;
namespace Ocelot.UnitTests.Infrastructure
{
public class InMemoryBusTests
{
private readonly InMemoryBus<object> _bus;
public InMemoryBusTests()
{
_bus = new InMemoryBus<object>();
}
[Fact]
public async Task should_publish_with_delay()
{
var called = false;
_bus.Subscribe(x => {
called = true;
});
_bus.Publish(new object(), 1);
await Task.Delay(10);
called.ShouldBeTrue();
}
[Fact]
public void should_not_be_publish_yet_as_no_delay_in_caller()
{
var called = false;
_bus.Subscribe(x => {
called = true;
});
_bus.Publish(new object(), 1);
called.ShouldBeFalse();
}
}
}

View File

@ -1,5 +1,6 @@
namespace Ocelot.UnitTests.LoadBalancer namespace Ocelot.UnitTests.LoadBalancer
{ {
using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using Ocelot.LoadBalancer.LoadBalancers; using Ocelot.LoadBalancer.LoadBalancers;
using Ocelot.Responses; using Ocelot.Responses;
@ -10,28 +11,43 @@ namespace Ocelot.UnitTests.LoadBalancer
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections; using System.Collections;
using System.Threading;
using Ocelot.Middleware; using Ocelot.Middleware;
using Ocelot.UnitTests.Responder; using Ocelot.UnitTests.Responder;
using TestStack.BDDfy; using TestStack.BDDfy;
using Ocelot.Infrastructure;
public class CookieStickySessionsTests public class CookieStickySessionsTests
{ {
private readonly CookieStickySessions _stickySessions; private readonly CookieStickySessions _stickySessions;
private readonly Mock<ILoadBalancer> _loadBalancer; private readonly Mock<ILoadBalancer> _loadBalancer;
private readonly int _defaultExpiryInMs;
private DownstreamContext _downstreamContext; private DownstreamContext _downstreamContext;
private Response<ServiceHostAndPort> _result; private Response<ServiceHostAndPort> _result;
private Response<ServiceHostAndPort> _firstHostAndPort; private Response<ServiceHostAndPort> _firstHostAndPort;
private Response<ServiceHostAndPort> _secondHostAndPort; private Response<ServiceHostAndPort> _secondHostAndPort;
private readonly FakeBus<StickySession> _bus;
public CookieStickySessionsTests() public CookieStickySessionsTests()
{ {
_bus = new FakeBus<StickySession>();
_loadBalancer = new Mock<ILoadBalancer>(); _loadBalancer = new Mock<ILoadBalancer>();
const int defaultExpiryInMs = 100; _defaultExpiryInMs = 0;
_stickySessions = new CookieStickySessions(_loadBalancer.Object, "sessionid", defaultExpiryInMs); _stickySessions = new CookieStickySessions(_loadBalancer.Object, "sessionid", _defaultExpiryInMs, _bus);
_downstreamContext = new DownstreamContext(new DefaultHttpContext()); _downstreamContext = new DownstreamContext(new DefaultHttpContext());
} }
[Fact]
public void should_expire_sticky_session()
{
this.Given(_ => GivenTheLoadBalancerReturns())
.And(_ => GivenTheDownstreamRequestHasSessionId("321"))
.And(_ => GivenIHackAMessageInWithAPastExpiry())
.And(_ => WhenILease())
.When(_ => WhenTheMessagesAreProcessed())
.Then(_ => ThenTheLoadBalancerIsCalled())
.BDDfy();
}
[Fact] [Fact]
public void should_return_host_and_port() public void should_return_host_and_port()
{ {
@ -48,6 +64,7 @@ namespace Ocelot.UnitTests.LoadBalancer
.And(_ => GivenTheDownstreamRequestHasSessionId("321")) .And(_ => GivenTheDownstreamRequestHasSessionId("321"))
.When(_ => WhenILeaseTwiceInARow()) .When(_ => WhenILeaseTwiceInARow())
.Then(_ => ThenTheFirstAndSecondResponseAreTheSame()) .Then(_ => ThenTheFirstAndSecondResponseAreTheSame())
.And(_ => ThenTheStickySessionWillTimeout())
.BDDfy(); .BDDfy();
} }
@ -69,92 +86,26 @@ namespace Ocelot.UnitTests.LoadBalancer
.BDDfy(); .BDDfy();
} }
[Fact]
public void should_expire_sticky_session()
{
this.Given(_ => GivenTheLoadBalancerReturnsSequence())
.When(_ => WhenTheStickySessionExpires())
.Then(_ => ThenANewHostAndPortIsReturned())
.BDDfy();
}
[Fact]
public void should_refresh_sticky_session()
{
this.Given(_ => GivenTheLoadBalancerReturnsSequence())
.When(_ => WhenIMakeRequestsToKeepRefreshingTheSession())
.Then(_ => ThenTheSessionIsRefreshed())
.BDDfy();
}
[Fact]
public void should_dispose()
{
_stickySessions.Dispose();
}
[Fact] [Fact]
public void should_release() public void should_release()
{ {
_stickySessions.Release(new ServiceHostAndPort("", 0)); _stickySessions.Release(new ServiceHostAndPort("", 0));
} }
private async Task ThenTheSessionIsRefreshed() private void ThenTheLoadBalancerIsCalled()
{ {
var postExpireHostAndPort = await _stickySessions.Lease(_downstreamContext); _loadBalancer.Verify(x => x.Release(It.IsAny<ServiceHostAndPort>()), Times.Once);
postExpireHostAndPort.Data.DownstreamHost.ShouldBe("one");
postExpireHostAndPort.Data.DownstreamPort.ShouldBe(80);
_loadBalancer
.Verify(x => x.Lease(It.IsAny<DownstreamContext>()), Times.Once);
} }
private async Task WhenIMakeRequestsToKeepRefreshingTheSession() private void WhenTheMessagesAreProcessed()
{ {
var context = new DefaultHttpContext(); _bus.Process();
var cookies = new FakeCookies();
cookies.AddCookie("sessionid", "321");
context.Request.Cookies = cookies;
_downstreamContext = new DownstreamContext(context);
var firstHostAndPort = await _stickySessions.Lease(_downstreamContext);
firstHostAndPort.Data.DownstreamHost.ShouldBe("one");
firstHostAndPort.Data.DownstreamPort.ShouldBe(80);
Thread.Sleep(80);
var secondHostAndPort = await _stickySessions.Lease(_downstreamContext);
secondHostAndPort.Data.DownstreamHost.ShouldBe("one");
secondHostAndPort.Data.DownstreamPort.ShouldBe(80);
Thread.Sleep(80);
} }
private async Task ThenANewHostAndPortIsReturned() private void GivenIHackAMessageInWithAPastExpiry()
{ {
var postExpireHostAndPort = await _stickySessions.Lease(_downstreamContext); var hostAndPort = new ServiceHostAndPort("999", 999);
postExpireHostAndPort.Data.DownstreamHost.ShouldBe("two"); _bus.Publish(new StickySession(hostAndPort, DateTime.UtcNow.AddDays(-1), "321"), 0);
postExpireHostAndPort.Data.DownstreamPort.ShouldBe(80);
}
private async Task WhenTheStickySessionExpires()
{
var context = new DefaultHttpContext();
var cookies = new FakeCookies();
cookies.AddCookie("sessionid", "321");
context.Request.Cookies = cookies;
_downstreamContext = new DownstreamContext(context);
var firstHostAndPort = await _stickySessions.Lease(_downstreamContext);
var secondHostAndPort = await _stickySessions.Lease(_downstreamContext);
firstHostAndPort.Data.DownstreamHost.ShouldBe("one");
firstHostAndPort.Data.DownstreamPort.ShouldBe(80);
secondHostAndPort.Data.DownstreamHost.ShouldBe("one");
secondHostAndPort.Data.DownstreamPort.ShouldBe(80);
Thread.Sleep(150);
} }
private void ThenAnErrorIsReturned() private void ThenAnErrorIsReturned()
@ -236,9 +187,14 @@ namespace Ocelot.UnitTests.LoadBalancer
{ {
_result.Data.ShouldNotBeNull(); _result.Data.ShouldNotBeNull();
} }
private void ThenTheStickySessionWillTimeout()
{
_bus.Messages.Count.ShouldBe(2);
}
} }
class FakeCookies : IRequestCookieCollection internal class FakeCookies : IRequestCookieCollection
{ {
private readonly Dictionary<string, string> _cookies = new Dictionary<string, string>(); private readonly Dictionary<string, string> _cookies = new Dictionary<string, string>();
@ -273,4 +229,37 @@ namespace Ocelot.UnitTests.LoadBalancer
return _cookies.GetEnumerator(); return _cookies.GetEnumerator();
} }
} }
internal class FakeBus<T> : IBus<T>
{
public FakeBus()
{
Messages = new List<T>();
Subscriptions = new List<Action<T>>();
}
public List<T> Messages { get; }
public List<Action<T>> Subscriptions { get; }
public void Subscribe(Action<T> action)
{
Subscriptions.Add(action);
}
public void Publish(T message, int delay)
{
Messages.Add(message);
}
public void Process()
{
foreach (var message in Messages)
{
foreach (var subscription in Subscriptions)
{
subscription(message);
}
}
}
}
} }