Skip to content

Commit

Permalink
updated to use the type information form json.net...this isnt great f…
Browse files Browse the repository at this point in the history
…or cross compat but gets Ocelot going and now can handle multiple commands on state machine
  • Loading branch information
Tom Gardham-Pallister committed Apr 3, 2017
1 parent 59379aa commit 6120fb8
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 62 deletions.
38 changes: 0 additions & 38 deletions src/Rafty/Infrastructure/JsonCreationConverter.cs

This file was deleted.

19 changes: 13 additions & 6 deletions src/Rafty/Infrastructure/RaftyConfigurationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@ public static IApplicationBuilder UseRafty(this IApplicationBuilder builder,
IServiceRegistry serviceRegistry,
ILogger logger,
IServersInCluster serversInCluster,
JsonConverter[] jsonConverters,
string raftyBasePath = null)
{
builder.UseRaftyForTesting(baseUri, messageSender, messageBus, stateMachine, serviceRegistry,
logger, serversInCluster, jsonConverters, raftyBasePath);
logger, serversInCluster, raftyBasePath);

return builder;
}
Expand All @@ -41,7 +40,6 @@ public static (IApplicationBuilder builder, Server server, ServerInCluster serve
IServiceRegistry serviceRegistry,
ILogger logger,
IServersInCluster serversInCluster,
JsonConverter[] jsonConverters,
string raftyBasePath = null)
{
var urlConfig = RaftyUrlConfig.Get(raftyBasePath);
Expand All @@ -64,7 +62,10 @@ public static (IApplicationBuilder builder, Server server, ServerInCluster serve
{
var reader = new StreamReader(context.Request.Body);
var content = reader.ReadToEnd();
var appendEntries = JsonConvert.DeserializeObject<AppendEntries>(content, jsonConverters);
var appendEntries = JsonConvert.DeserializeObject<AppendEntries>(content, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.All
});
var appendEntriesResponse = server.Receive(appendEntries);
await context.Response.WriteAsync(JsonConvert.SerializeObject(appendEntriesResponse));
}
Expand All @@ -83,7 +84,10 @@ public static (IApplicationBuilder builder, Server server, ServerInCluster serve
{
var reader = new StreamReader(context.Request.Body);
var content = reader.ReadToEnd();
var requestVote = JsonConvert.DeserializeObject<RequestVote>(content, jsonConverters);
var requestVote = JsonConvert.DeserializeObject<RequestVote>(content, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.All
});
var requestVoteResponse = server.Receive(requestVote);
await context.Response.WriteAsync(JsonConvert.SerializeObject(requestVoteResponse));
}
Expand All @@ -102,7 +106,10 @@ public static (IApplicationBuilder builder, Server server, ServerInCluster serve
{
var reader = new StreamReader(context.Request.Body);
var content = reader.ReadToEnd();
var command = JsonConvert.DeserializeObject<Command>(content, jsonConverters);
var command = JsonConvert.DeserializeObject<Command>(content, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.All
});
var sendCommandToLeaderResponse = server.Receive(command);
await context.Response.WriteAsync(JsonConvert.SerializeObject(sendCommandToLeaderResponse));
}
Expand Down
1 change: 1 addition & 0 deletions test/Rafty.AcceptanceTests/AcceptanceTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Threading;
using TestStack.BDDfy;
using Xunit;

Expand Down
54 changes: 50 additions & 4 deletions test/Rafty.AcceptanceTests/AcceptanceTestsSteps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class AcceptanceTestsSteps : IDisposable
private ServiceRegistry _serviceRegistry;
private List<ServerContainer> _servers;
private FakeCommand _fakeCommand;
private FooCommand _fooCommand;

public AcceptanceTestsSteps()
{
Expand Down Expand Up @@ -163,14 +164,59 @@ public void ThenTheOtherNodesAreFollowers(int expected)
fourFollowers.ShouldBeTrue();
}

internal void ThenTheFakeCommandTwoIsPersistedToAllStateMachines(int v1, int v2)
internal void ThenTheFakeCommandTwoIsPersistedToAllStateMachines(int index, int serversToCheck)
{
throw new NotImplementedException();
var stopWatch = Stopwatch.StartNew();
var updated = new List<Guid>();

while (stopWatch.ElapsedMilliseconds < 90000)
{
foreach (var server in _servers)
{
var fakeStateMachine = (FakeStateMachine)server.StateMachine;

if (fakeStateMachine.Commands.Count > 0)
{
var command = (FooCommand)fakeStateMachine.Commands[index];
command.Description.ShouldBe(_fooCommand.Description);
if (!updated.Contains(server.Server.Id))
{
updated.Add(server.Server.Id);
}
}
}

if (updated.Count == serversToCheck)
{
break;
}
}

updated.Count.ShouldBe(serversToCheck);
}

internal void AFakeCommandTwoIsSentToTheLeader()
{
throw new NotImplementedException();
var leader = _servers.SingleOrDefault(x => x.Server.State is Leader);
while(leader == null)
{
ThenANewLeaderIsElected();
leader = _servers.SingleOrDefault(x => x.Server.State is Leader);
}
_fooCommand = new FooCommand("some description.....");
var urlOfLeader = leader.ServerUrl;
var json = JsonConvert.SerializeObject(_fooCommand, Formatting.None, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.All
});
var httpContent = new StringContent(json);

using (var httpClient = new HttpClient())
{
httpClient.BaseAddress = new Uri(urlOfLeader);
var response = httpClient.PostAsync("/command", httpContent).Result;
response.EnsureSuccessStatusCode();
}
}

public void ThenANewLeaderIsElected()
Expand Down Expand Up @@ -258,7 +304,7 @@ private async Task GivenAServerIsRunning(string baseUrl)
stateMachine = new FakeStateMachine();

var result = app.UseRaftyForTesting(new Uri(baseUrl), messageSender, messageBus, stateMachine,
_serviceRegistry, logger, _serversInCluster, new JsonConverter[]{ new FakeCommandConverter()});
_serviceRegistry, logger, _serversInCluster);

server = result.server;
serverInCluster = result.serverInCluster;
Expand Down
14 changes: 14 additions & 0 deletions test/Rafty.AcceptanceTests/FakeCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,18 @@ public FakeCommand(Guid id)
}
public Guid Id { get; set; }
}

public class FooCommand : Command
{
public FooCommand()
{
}

public FooCommand(string description)
{
Description = description;
}

public string Description {get;set;}
}
}
14 changes: 0 additions & 14 deletions test/Rafty.AcceptanceTests/FakeCommandConverter.cs

This file was deleted.

0 comments on commit 6120fb8

Please sign in to comment.