diff --git a/src/Rafty/Infrastructure/JsonCreationConverter.cs b/src/Rafty/Infrastructure/JsonCreationConverter.cs deleted file mode 100644 index d75ddd3..0000000 --- a/src/Rafty/Infrastructure/JsonCreationConverter.cs +++ /dev/null @@ -1,38 +0,0 @@ -using System; -using System.Reflection; -using Newtonsoft.Json; -using Newtonsoft.Json.Linq; - -namespace Rafty.Infrastructure -{ - public abstract class JsonCreationConverter : JsonConverter - { - protected abstract T Create(Type objectType, JObject jObject); - - public override bool CanConvert(Type objectType) - { - return objectType.IsAssignableFrom(typeof(T)); - } - - public override bool CanWrite => false; - - public override object ReadJson(JsonReader reader, - Type objectType, - object existingValue, - JsonSerializer serializer) - { - var jObject = JObject.Load(reader); - - var target = Create(objectType, jObject); - - serializer.Populate(jObject.CreateReader(), target); - - return target; - } - - public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) - { - throw new NotImplementedException(); - } - } -} \ No newline at end of file diff --git a/src/Rafty/Infrastructure/RaftyConfigurationExtensions.cs b/src/Rafty/Infrastructure/RaftyConfigurationExtensions.cs index 9826b19..e105277 100644 --- a/src/Rafty/Infrastructure/RaftyConfigurationExtensions.cs +++ b/src/Rafty/Infrastructure/RaftyConfigurationExtensions.cs @@ -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; } @@ -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); @@ -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(content, jsonConverters); + var appendEntries = JsonConvert.DeserializeObject(content, new JsonSerializerSettings + { + TypeNameHandling = TypeNameHandling.All + }); var appendEntriesResponse = server.Receive(appendEntries); await context.Response.WriteAsync(JsonConvert.SerializeObject(appendEntriesResponse)); } @@ -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(content, jsonConverters); + var requestVote = JsonConvert.DeserializeObject(content, new JsonSerializerSettings + { + TypeNameHandling = TypeNameHandling.All + }); var requestVoteResponse = server.Receive(requestVote); await context.Response.WriteAsync(JsonConvert.SerializeObject(requestVoteResponse)); } @@ -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(content, jsonConverters); + var command = JsonConvert.DeserializeObject(content, new JsonSerializerSettings + { + TypeNameHandling = TypeNameHandling.All + }); var sendCommandToLeaderResponse = server.Receive(command); await context.Response.WriteAsync(JsonConvert.SerializeObject(sendCommandToLeaderResponse)); } diff --git a/test/Rafty.AcceptanceTests/AcceptanceTests.cs b/test/Rafty.AcceptanceTests/AcceptanceTests.cs index 80766bd..06bb15d 100644 --- a/test/Rafty.AcceptanceTests/AcceptanceTests.cs +++ b/test/Rafty.AcceptanceTests/AcceptanceTests.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Threading; using TestStack.BDDfy; using Xunit; diff --git a/test/Rafty.AcceptanceTests/AcceptanceTestsSteps.cs b/test/Rafty.AcceptanceTests/AcceptanceTestsSteps.cs index c98bda7..74ce9ad 100644 --- a/test/Rafty.AcceptanceTests/AcceptanceTestsSteps.cs +++ b/test/Rafty.AcceptanceTests/AcceptanceTestsSteps.cs @@ -25,6 +25,7 @@ public class AcceptanceTestsSteps : IDisposable private ServiceRegistry _serviceRegistry; private List _servers; private FakeCommand _fakeCommand; + private FooCommand _fooCommand; public AcceptanceTestsSteps() { @@ -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(); + + 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() @@ -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; diff --git a/test/Rafty.AcceptanceTests/FakeCommand.cs b/test/Rafty.AcceptanceTests/FakeCommand.cs index 0723e60..340aacf 100644 --- a/test/Rafty.AcceptanceTests/FakeCommand.cs +++ b/test/Rafty.AcceptanceTests/FakeCommand.cs @@ -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;} + } } \ No newline at end of file diff --git a/test/Rafty.AcceptanceTests/FakeCommandConverter.cs b/test/Rafty.AcceptanceTests/FakeCommandConverter.cs deleted file mode 100644 index e1fd4d5..0000000 --- a/test/Rafty.AcceptanceTests/FakeCommandConverter.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System; -using Newtonsoft.Json.Linq; -using Rafty.Infrastructure; - -namespace Rafty.AcceptanceTests -{ - public class FakeCommandConverter : JsonCreationConverter - { - protected override FakeCommand Create(Type objectType, JObject jObject) - { - return new FakeCommand(); - } - } -} \ No newline at end of file