#245 hacked these tests around and they are now passing 5 runs in a row on my mac, lets see on build server

This commit is contained in:
Tom Gardham-Pallister 2018-05-01 22:07:56 +01:00
parent 6793278597
commit 003fff8b24
3 changed files with 636 additions and 590 deletions

View File

@ -42,28 +42,35 @@ namespace Ocelot.Configuration
[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)
{ {
var node = (INode)test; //todo - this code is a bit shit sort it out..
var result = node.Accept(new UpdateFileConfiguration(fileConfiguration)); var test = _provider.GetService(typeof(INode));
if (result.GetType() == typeof(Rafty.Concensus.ErrorResponse<UpdateFileConfiguration>)) if (test != null)
{ {
return new BadRequestObjectResult("There was a problem. This error message sucks raise an issue in GitHub."); var node = (INode)test;
var result = node.Accept(new UpdateFileConfiguration(fileConfiguration));
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)
{
return new BadRequestObjectResult(response.Errors);
}
return new OkObjectResult(fileConfiguration);
} }
catch(Exception e)
var response = await _setter.Set(fileConfiguration);
if (response.IsError)
{ {
return new BadRequestObjectResult(response.Errors); return new BadRequestObjectResult($"{e.Message}:{e.StackTrace}");
} }
return new OkObjectResult(fileConfiguration);
} }
} }
} }

View File

@ -99,7 +99,8 @@ namespace Ocelot.Raft
var response = _httpClient.PostAsync($"{_hostAndPort}/administration/raft/command", content).GetAwaiter().GetResult(); var response = _httpClient.PostAsync($"{_hostAndPort}/administration/raft/command", content).GetAwaiter().GetResult();
if(response.IsSuccessStatusCode) if(response.IsSuccessStatusCode)
{ {
return JsonConvert.DeserializeObject<OkResponse<T>>(response.Content.ReadAsStringAsync().GetAwaiter().GetResult(), _jsonSerializerSettings); var okResponse = JsonConvert.DeserializeObject<OkResponse<ICommand>>(response.Content.ReadAsStringAsync().GetAwaiter().GetResult(), _jsonSerializerSettings);
return new OkResponse<T>((T)okResponse.Command);
} }
else else
{ {

View File

@ -60,7 +60,7 @@ namespace Ocelot.IntegrationTests
} }
} }
[Fact(Skip = "This tests is flakey at the moment so ignoring will be fixed long term see https://github.com/TomPallister/Ocelot/issues/245")] [Fact]
public void should_persist_command_to_five_servers() public void should_persist_command_to_five_servers()
{ {
var configuration = new FileConfiguration var configuration = new FileConfiguration
@ -113,13 +113,12 @@ namespace Ocelot.IntegrationTests
var command = new UpdateFileConfiguration(updatedConfiguration); var command = new UpdateFileConfiguration(updatedConfiguration);
GivenThereIsAConfiguration(configuration); GivenThereIsAConfiguration(configuration);
GivenFiveServersAreRunning(); GivenFiveServersAreRunning();
GivenALeaderIsElected();
GivenIHaveAnOcelotToken("/administration"); GivenIHaveAnOcelotToken("/administration");
WhenISendACommandIntoTheCluster(command); WhenISendACommandIntoTheCluster(command);
ThenTheCommandIsReplicatedToAllStateMachines(command); ThenTheCommandIsReplicatedToAllStateMachines(command);
} }
[Fact(Skip = "This tests is flakey at the moment so ignoring will be fixed long term see https://github.com/TomPallister/Ocelot/issues/245")] [Fact]
public void should_persist_command_to_five_servers_when_using_administration_api() public void should_persist_command_to_five_servers_when_using_administration_api()
{ {
var configuration = new FileConfiguration var configuration = new FileConfiguration
@ -166,7 +165,6 @@ namespace Ocelot.IntegrationTests
var command = new UpdateFileConfiguration(updatedConfiguration); var command = new UpdateFileConfiguration(updatedConfiguration);
GivenThereIsAConfiguration(configuration); GivenThereIsAConfiguration(configuration);
GivenFiveServersAreRunning(); GivenFiveServersAreRunning();
GivenALeaderIsElected();
GivenIHaveAnOcelotToken("/administration"); GivenIHaveAnOcelotToken("/administration");
GivenIHaveAddedATokenToMyRequest(); GivenIHaveAddedATokenToMyRequest();
WhenIPostOnTheApiGateway("/administration/configuration", updatedConfiguration); WhenIPostOnTheApiGateway("/administration/configuration", updatedConfiguration);
@ -175,37 +173,45 @@ namespace Ocelot.IntegrationTests
private void WhenISendACommandIntoTheCluster(UpdateFileConfiguration command) private void WhenISendACommandIntoTheCluster(UpdateFileConfiguration command)
{ {
var p = _peers.Peers.First(); bool SendCommand()
var json = JsonConvert.SerializeObject(command,new JsonSerializerSettings() {
TypeNameHandling = TypeNameHandling.All
});
var httpContent = new StringContent(json);
httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
using(var httpClient = new HttpClient())
{ {
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _token.AccessToken); var p = _peers.Peers.First();
var response = httpClient.PostAsync($"{p.HostAndPort}/administration/raft/command", httpContent).GetAwaiter().GetResult(); var json = JsonConvert.SerializeObject(command,new JsonSerializerSettings() {
response.EnsureSuccessStatusCode(); TypeNameHandling = TypeNameHandling.All
var content = response.Content.ReadAsStringAsync().GetAwaiter().GetResult(); });
var result = JsonConvert.DeserializeObject<OkResponse<UpdateFileConfiguration>>(content); var httpContent = new StringContent(json);
result.Command.Configuration.ReRoutes.Count.ShouldBe(2); httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
using(var httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _token.AccessToken);
var response = httpClient.PostAsync($"{p.HostAndPort}/administration/raft/command", httpContent).GetAwaiter().GetResult();
response.EnsureSuccessStatusCode();
var content = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
var errorResult = JsonConvert.DeserializeObject<ErrorResponse<UpdateFileConfiguration>>(content);
if(!string.IsNullOrEmpty(errorResult.Error))
{
return false;
}
var okResult = JsonConvert.DeserializeObject<OkResponse<UpdateFileConfiguration>>(content);
if(okResult.Command.Configuration.ReRoutes.Count == 2)
{
return true;
}
}
return false;
} }
//dirty sleep to make sure command replicated... var commandSent = WaitFor(20000).Until(() => SendCommand());
var stopwatch = Stopwatch.StartNew(); commandSent.ShouldBeTrue();
while(stopwatch.ElapsedMilliseconds < 10000)
{
}
} }
private void ThenTheCommandIsReplicatedToAllStateMachines(UpdateFileConfiguration expecteds) private void ThenTheCommandIsReplicatedToAllStateMachines(UpdateFileConfiguration expecteds)
{ {
//dirty sleep to give a chance to replicate...
var stopwatch = Stopwatch.StartNew();
while(stopwatch.ElapsedMilliseconds < 2000)
{
}
bool CommandCalledOnAllStateMachines() bool CommandCalledOnAllStateMachines()
{ {
try try
@ -267,10 +273,34 @@ namespace Ocelot.IntegrationTests
private void WhenIPostOnTheApiGateway(string url, FileConfiguration updatedConfiguration) private void WhenIPostOnTheApiGateway(string url, FileConfiguration updatedConfiguration)
{ {
var json = JsonConvert.SerializeObject(updatedConfiguration); bool SendCommand()
var content = new StringContent(json); {
content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); var json = JsonConvert.SerializeObject(updatedConfiguration);
_response = _httpClient.PostAsync(url, content).Result; var content = new StringContent(json);
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
_response = _httpClient.PostAsync(url, content).Result;
var responseContent = _response.Content.ReadAsStringAsync().Result;
//Console.ForegroundColor = ConsoleColor.Green;
//Console.WriteLine(responseContent);
//Console.WriteLine(_response.StatusCode);
//Console.ForegroundColor = ConsoleColor.White;
if(responseContent == "There was a problem. This error message sucks raise an issue in GitHub.")
{
return false;
}
if(string.IsNullOrEmpty(responseContent))
{
return false;
}
return _response.IsSuccessStatusCode;
}
var commandSent = WaitFor(20000).Until(() => SendCommand());
commandSent.ShouldBeTrue();
} }
private void GivenIHaveAddedATokenToMyRequest() private void GivenIHaveAddedATokenToMyRequest()
@ -280,23 +310,40 @@ namespace Ocelot.IntegrationTests
private void GivenIHaveAnOcelotToken(string adminPath) private void GivenIHaveAnOcelotToken(string adminPath)
{ {
var tokenUrl = $"{adminPath}/connect/token"; bool AddToken()
var formData = new List<KeyValuePair<string, string>>
{ {
new KeyValuePair<string, string>("client_id", "admin"), try
new KeyValuePair<string, string>("client_secret", "secret"), {
new KeyValuePair<string, string>("scope", "admin"), var tokenUrl = $"{adminPath}/connect/token";
new KeyValuePair<string, string>("grant_type", "client_credentials") var formData = new List<KeyValuePair<string, string>>
}; {
var content = new FormUrlEncodedContent(formData); new KeyValuePair<string, string>("client_id", "admin"),
new KeyValuePair<string, string>("client_secret", "secret"),
new KeyValuePair<string, string>("scope", "admin"),
new KeyValuePair<string, string>("grant_type", "client_credentials")
};
var content = new FormUrlEncodedContent(formData);
var response = _httpClient.PostAsync(tokenUrl, content).Result;
var responseContent = response.Content.ReadAsStringAsync().Result;
if(!response.IsSuccessStatusCode)
{
return false;
}
_token = JsonConvert.DeserializeObject<BearerToken>(responseContent);
var configPath = $"{adminPath}/.well-known/openid-configuration";
response = _httpClient.GetAsync(configPath).Result;
return response.IsSuccessStatusCode;
}
catch(Exception e)
{
return false;
}
}
var addToken = WaitFor(20000).Until(() => AddToken());
addToken.ShouldBeTrue();
var response = _httpClient.PostAsync(tokenUrl, content).Result;
var responseContent = response.Content.ReadAsStringAsync().Result;
response.EnsureSuccessStatusCode();
_token = JsonConvert.DeserializeObject<BearerToken>(responseContent);
var configPath = $"{adminPath}/.well-known/openid-configuration";
response = _httpClient.GetAsync(configPath).Result;
response.EnsureSuccessStatusCode();
} }
private void GivenThereIsAConfiguration(FileConfiguration fileConfiguration) private void GivenThereIsAConfiguration(FileConfiguration fileConfiguration)
@ -380,14 +427,5 @@ namespace Ocelot.IntegrationTests
_threads.Add(thread); _threads.Add(thread);
} }
} }
private void GivenALeaderIsElected()
{
//dirty sleep to make sure we have a leader
var stopwatch = Stopwatch.StartNew();
while(stopwatch.ElapsedMilliseconds < 20000)
{
}
}
} }
} }