From 4c4f455904d413437df79fb01c9cc6e5aa203287 Mon Sep 17 00:00:00 2001 From: Tom Gardham-Pallister Date: Thu, 21 Dec 2017 19:29:49 +0000 Subject: [PATCH 1/2] changed id to string not guid --- src/Rafty/Concensus/Messages/AppendEntries.cs | 4 +- .../Messages/AppendEntriesBuilder.cs | 4 +- src/Rafty/Concensus/Messages/RequestVote.cs | 4 +- .../Concensus/Messages/RequestVoteBuilder.cs | 4 +- src/Rafty/Concensus/Node/INode.cs | 2 +- src/Rafty/Concensus/Node/Node.cs | 4 +- src/Rafty/Concensus/Node/NodePeer.cs | 4 +- src/Rafty/Concensus/Peers/IPeer.cs | 2 +- src/Rafty/Concensus/Peers/Peer.cs | 2 +- src/Rafty/Concensus/States/CurrentState.cs | 8 ++-- src/Rafty/Concensus/States/Rules.cs | 2 +- src/Rafty/Infrastructure/NodeId.cs | 4 +- test/Rafty.AcceptanceTests/Tests.cs | 8 ++-- test/Rafty.IntegrationTests/FileFsm.cs | 4 +- .../FilePeersProvider.cs | 2 +- test/Rafty.IntegrationTests/HttpPeer.cs | 6 +-- test/Rafty.IntegrationTests/SqlLiteLog.cs | 2 +- .../Rafty.IntegrationTests/SqlLiteLogTests.cs | 4 +- test/Rafty.IntegrationTests/Startup.cs | 2 +- test/Rafty.IntegrationTests/Tests.cs | 2 +- .../AllServersApplyToStateMachineTests.cs | 6 +-- .../AllServersConvertToFollowerTests.cs | 12 +++--- test/Rafty.UnitTests/AppendEntriesTests.cs | 22 +++++----- test/Rafty.UnitTests/CandidateTests.cs | 12 +++--- test/Rafty.UnitTests/FakePeer.cs | 8 ++-- test/Rafty.UnitTests/FollowerTests.cs | 30 ++++++------- test/Rafty.UnitTests/LeaderTests.cs | 42 +++++++++---------- test/Rafty.UnitTests/NothingNode.cs | 2 +- test/Rafty.UnitTests/RemoteControledPeer.cs | 4 +- test/Rafty.UnitTests/RequestVoteTests.cs | 10 ++--- test/Rafty.UnitTests/TestingNode.cs | 2 +- 31 files changed, 112 insertions(+), 112 deletions(-) diff --git a/src/Rafty/Concensus/Messages/AppendEntries.cs b/src/Rafty/Concensus/Messages/AppendEntries.cs index c6a3f54..6eb6fda 100644 --- a/src/Rafty/Concensus/Messages/AppendEntries.cs +++ b/src/Rafty/Concensus/Messages/AppendEntries.cs @@ -6,7 +6,7 @@ namespace Rafty.Concensus public sealed class AppendEntries : Message { - public AppendEntries(long term, Guid leaderId, int previousLogIndex, long previousLogTerm, + public AppendEntries(long term, string leaderId, int previousLogIndex, long previousLogTerm, List entries, int leaderCommitIndex) : base(Guid.NewGuid()) { @@ -26,7 +26,7 @@ public AppendEntries(long term, Guid leaderId, int previousLogIndex, long previo /// // LeaderId so follower can redirect clients. /// - public Guid LeaderId { get; private set; } + public string LeaderId { get; private set; } /// // PrevLogIndex index of log entry immediately preceding new ones. diff --git a/src/Rafty/Concensus/Messages/AppendEntriesBuilder.cs b/src/Rafty/Concensus/Messages/AppendEntriesBuilder.cs index 020577b..63fa3f4 100644 --- a/src/Rafty/Concensus/Messages/AppendEntriesBuilder.cs +++ b/src/Rafty/Concensus/Messages/AppendEntriesBuilder.cs @@ -8,7 +8,7 @@ public class AppendEntriesBuilder { private List _entries; private int _leaderCommitIndex; - private Guid _leaderId; + private string _leaderId; private int _previousLogIndex; private long _previousLogTerm; private long _term; @@ -19,7 +19,7 @@ public AppendEntriesBuilder WithTerm(long term) return this; } - public AppendEntriesBuilder WithLeaderId(Guid leaderId) + public AppendEntriesBuilder WithLeaderId(string leaderId) { _leaderId = leaderId; return this; diff --git a/src/Rafty/Concensus/Messages/RequestVote.cs b/src/Rafty/Concensus/Messages/RequestVote.cs index 985c366..9fd4b0f 100644 --- a/src/Rafty/Concensus/Messages/RequestVote.cs +++ b/src/Rafty/Concensus/Messages/RequestVote.cs @@ -4,7 +4,7 @@ namespace Rafty.Concensus { public sealed class RequestVote { - public RequestVote(long term, Guid candidateId, long lastLogIndex, long lastLogTerm) + public RequestVote(long term, string candidateId, long lastLogIndex, long lastLogTerm) { Term = term; CandidateId = candidateId; @@ -20,7 +20,7 @@ public RequestVote(long term, Guid candidateId, long lastLogIndex, long lastLogT /// // CandidateId candidate requesting vote. /// - public Guid CandidateId {get;private set;} + public string CandidateId {get;private set;} /// // LastLogIndex index of candidate’s last log entry (§5.4). diff --git a/src/Rafty/Concensus/Messages/RequestVoteBuilder.cs b/src/Rafty/Concensus/Messages/RequestVoteBuilder.cs index 5d2e738..0afbac0 100644 --- a/src/Rafty/Concensus/Messages/RequestVoteBuilder.cs +++ b/src/Rafty/Concensus/Messages/RequestVoteBuilder.cs @@ -5,7 +5,7 @@ namespace Rafty.Concensus public class RequestVoteBuilder { private long _term; - private Guid _candidateId; + private string _candidateId; private long _lastLogIndex; private long _lastLogTerm; @@ -15,7 +15,7 @@ public RequestVoteBuilder WithTerm(long term) return this; } - public RequestVoteBuilder WithCandidateId(Guid candidateId) + public RequestVoteBuilder WithCandidateId(string candidateId) { _candidateId = candidateId; return this; diff --git a/src/Rafty/Concensus/Node/INode.cs b/src/Rafty/Concensus/Node/INode.cs index fd3c1e9..535c4fd 100644 --- a/src/Rafty/Concensus/Node/INode.cs +++ b/src/Rafty/Concensus/Node/INode.cs @@ -11,7 +11,7 @@ public interface INode void BecomeCandidate(CurrentState state); AppendEntriesResponse Handle(AppendEntries appendEntries); RequestVoteResponse Handle(RequestVote requestVote); - void Start(Guid id); + void Start(string id); void Stop(); Response Accept(T command) where T : ICommand; } diff --git a/src/Rafty/Concensus/Node/Node.cs b/src/Rafty/Concensus/Node/Node.cs index 721dca6..d56cc49 100644 --- a/src/Rafty/Concensus/Node/Node.cs +++ b/src/Rafty/Concensus/Node/Node.cs @@ -40,11 +40,11 @@ public Node( public IState State { get; private set; } - public void Start(Guid id) + public void Start(string id) { if(State?.CurrentState == null) { - BecomeFollower(new CurrentState(id, 0, default(Guid), 0, 0, default(Guid))); + BecomeFollower(new CurrentState(id, 0, default(string), 0, 0, default(string))); } else { diff --git a/src/Rafty/Concensus/Node/NodePeer.cs b/src/Rafty/Concensus/Node/NodePeer.cs index 4b28d85..98f40d4 100644 --- a/src/Rafty/Concensus/Node/NodePeer.cs +++ b/src/Rafty/Concensus/Node/NodePeer.cs @@ -9,7 +9,7 @@ public class NodePeer : IPeer { private Node _node; - public Guid Id + public string Id { get { @@ -18,7 +18,7 @@ public Guid Id return _node.State.CurrentState.Id; } - return default(Guid); + return default(string); } } diff --git a/src/Rafty/Concensus/Peers/IPeer.cs b/src/Rafty/Concensus/Peers/IPeer.cs index 2378c84..63e5518 100644 --- a/src/Rafty/Concensus/Peers/IPeer.cs +++ b/src/Rafty/Concensus/Peers/IPeer.cs @@ -8,7 +8,7 @@ public interface IPeer /// /// This will return the peers ID. /// - Guid Id {get;} + string Id {get;} /// /// This will make a requestvote request to the given peer. You must implement the transport. diff --git a/src/Rafty/Concensus/Peers/Peer.cs b/src/Rafty/Concensus/Peers/Peer.cs index a97f1e4..9cda50d 100644 --- a/src/Rafty/Concensus/Peers/Peer.cs +++ b/src/Rafty/Concensus/Peers/Peer.cs @@ -6,7 +6,7 @@ namespace Rafty.Concensus public class Peer : IPeer { - public Guid Id => throw new NotImplementedException(); + public string Id => throw new NotImplementedException(); public RequestVoteResponse Request(RequestVote requestVote) { diff --git a/src/Rafty/Concensus/States/CurrentState.cs b/src/Rafty/Concensus/States/CurrentState.cs index 08077ac..f74f3e2 100644 --- a/src/Rafty/Concensus/States/CurrentState.cs +++ b/src/Rafty/Concensus/States/CurrentState.cs @@ -6,7 +6,7 @@ namespace Rafty.Concensus public class CurrentState { - public CurrentState(Guid id, long currentTerm, Guid votedFor, int commitIndex, int lastApplied, Guid leaderId) + public CurrentState(string id, long currentTerm, string votedFor, int commitIndex, int lastApplied, string leaderId) { Id = id; CurrentTerm = currentTerm; @@ -17,11 +17,11 @@ public CurrentState(Guid id, long currentTerm, Guid votedFor, int commitIndex, i } public long CurrentTerm { get; private set; } - public Guid VotedFor { get; private set; } + public string VotedFor { get; private set; } public int CommitIndex { get; private set; } public int LastApplied { get; private set; } public Uri Address { get; private set; } - public Guid Id { get; private set; } - public Guid LeaderId { get; private set; } + public string Id { get; private set; } + public string LeaderId { get; private set; } } } \ No newline at end of file diff --git a/src/Rafty/Concensus/States/Rules.cs b/src/Rafty/Concensus/States/Rules.cs index 9ba1462..caae8ea 100644 --- a/src/Rafty/Concensus/States/Rules.cs +++ b/src/Rafty/Concensus/States/Rules.cs @@ -19,7 +19,7 @@ public class Rules : IRules // todo - consolidate with candidate and pass in as function public (RequestVoteResponse requestVoteResponse, bool shouldReturn) VotedForIsNotThisOrNobody(RequestVote requestVote, CurrentState currentState) { - if (currentState.VotedFor == currentState.Id || currentState.VotedFor != default(Guid)) + if (currentState.VotedFor == currentState.Id || currentState.VotedFor != default(string)) { return (new RequestVoteResponse(false, currentState.CurrentTerm), true); } diff --git a/src/Rafty/Infrastructure/NodeId.cs b/src/Rafty/Infrastructure/NodeId.cs index 2874ae4..f86e266 100644 --- a/src/Rafty/Infrastructure/NodeId.cs +++ b/src/Rafty/Infrastructure/NodeId.cs @@ -4,11 +4,11 @@ namespace Rafty.Infrastructure { public class NodeId { - public NodeId(Guid id) + public NodeId(string id) { Id = id; } - public Guid Id {get;private set;} + public string Id {get;private set;} } } diff --git a/test/Rafty.AcceptanceTests/Tests.cs b/test/Rafty.AcceptanceTests/Tests.cs index 26ca48e..a319669 100644 --- a/test/Rafty.AcceptanceTests/Tests.cs +++ b/test/Rafty.AcceptanceTests/Tests.cs @@ -188,7 +188,7 @@ private void AddNewServers(int count) peer.SetNode(server.Node); var nextIndex = _servers.Count; _servers.TryAdd(nextIndex, server); - node.Start(Guid.NewGuid()); + node.Start(Guid.NewGuid().ToString()); } } @@ -361,7 +361,7 @@ private void AssertLeaderElectedAndRemainsLeader() { var stopwatch = Stopwatch.StartNew(); var passed = false; - var leaderId = default(Guid); + var leaderId = default(string); while(stopwatch.Elapsed.TotalSeconds < 25) { Thread.Sleep(1000); @@ -373,7 +373,7 @@ private void AssertLeaderElectedAndRemainsLeader() if (leader.Count == 1 && followers.Count == 4) { //if the leader id hasnt been set set it... - if(leaderId == default(Guid)) + if(leaderId == default(string)) { leaderId = leader[0].State.CurrentState.Id; } @@ -429,7 +429,7 @@ private void StartNodes() _output.WriteLine("start the nodes"); foreach(var server in _servers) { - server.Value.Node.Start(Guid.NewGuid()); + server.Value.Node.Start(Guid.NewGuid().ToString()); } } } diff --git a/test/Rafty.IntegrationTests/FileFsm.cs b/test/Rafty.IntegrationTests/FileFsm.cs index 9688a1f..30fd412 100644 --- a/test/Rafty.IntegrationTests/FileFsm.cs +++ b/test/Rafty.IntegrationTests/FileFsm.cs @@ -9,7 +9,7 @@ namespace Rafty.IntegrationTests { public class FileFsm : IFiniteStateMachine { - private Guid _id; + private string _id; public FileFsm(NodeId nodeId) { _id = nodeId.Id; @@ -20,7 +20,7 @@ public void Handle(LogEntry log) try { var json = JsonConvert.SerializeObject(log.CommandData); - File.AppendAllText(_id.ToString(), json); + File.AppendAllText(_id.Replace("/","").Replace(":","").ToString(), json); } catch(Exception exception) { diff --git a/test/Rafty.IntegrationTests/FilePeersProvider.cs b/test/Rafty.IntegrationTests/FilePeersProvider.cs index 058bdad..30928ea 100644 --- a/test/Rafty.IntegrationTests/FilePeersProvider.cs +++ b/test/Rafty.IntegrationTests/FilePeersProvider.cs @@ -19,7 +19,7 @@ public FilePeersProvider(IOptions options) foreach (var item in _options.Value.Peers) { var httpClient = new HttpClient(); - var httpPeer = new HttpPeer(item.HostAndPort, Guid.Parse(item.Id), httpClient); + var httpPeer = new HttpPeer(item.HostAndPort, item.Id, httpClient); _peers.Add(httpPeer); } } diff --git a/test/Rafty.IntegrationTests/HttpPeer.cs b/test/Rafty.IntegrationTests/HttpPeer.cs index 6a5592c..6aba499 100644 --- a/test/Rafty.IntegrationTests/HttpPeer.cs +++ b/test/Rafty.IntegrationTests/HttpPeer.cs @@ -12,9 +12,9 @@ public class HttpPeer : IPeer private HttpClient _httpClient; private JsonSerializerSettings _jsonSerializerSettings; - public HttpPeer(string hostAndPort, Guid id, HttpClient httpClient) + public HttpPeer(string hostAndPort, string id, HttpClient httpClient) { - Id = id; + Id = hostAndPort; _hostAndPort = hostAndPort; _httpClient = httpClient; _jsonSerializerSettings = new JsonSerializerSettings() { @@ -22,7 +22,7 @@ public HttpPeer(string hostAndPort, Guid id, HttpClient httpClient) }; } - public Guid Id {get; private set;} + public string Id {get; private set;} public RequestVoteResponse Request(RequestVote requestVote) { diff --git a/test/Rafty.IntegrationTests/SqlLiteLog.cs b/test/Rafty.IntegrationTests/SqlLiteLog.cs index 72652b7..3d1d953 100644 --- a/test/Rafty.IntegrationTests/SqlLiteLog.cs +++ b/test/Rafty.IntegrationTests/SqlLiteLog.cs @@ -15,7 +15,7 @@ public class SqlLiteLog : ILog public SqlLiteLog(NodeId nodeId) { - _path = $"{nodeId.Id.ToString()}.db"; + _path = $"{nodeId.Id.Replace("/","").Replace(":","").ToString()}.db"; if(!File.Exists(_path)) { lock(_lock) diff --git a/test/Rafty.IntegrationTests/SqlLiteLogTests.cs b/test/Rafty.IntegrationTests/SqlLiteLogTests.cs index 27da424..f0e7e64 100644 --- a/test/Rafty.IntegrationTests/SqlLiteLogTests.cs +++ b/test/Rafty.IntegrationTests/SqlLiteLogTests.cs @@ -11,11 +11,11 @@ namespace Rafty.UnitTests public class SqlLiteLogTests : IDisposable { private SqlLiteLog _log; - private Guid _id; + private string _id; public SqlLiteLogTests() { - _id = Guid.NewGuid(); + _id = Guid.NewGuid().ToString(); _log = new SqlLiteLog(new NodeId(_id)); } diff --git a/test/Rafty.IntegrationTests/Startup.cs b/test/Rafty.IntegrationTests/Startup.cs index 6781758..e0bda94 100644 --- a/test/Rafty.IntegrationTests/Startup.cs +++ b/test/Rafty.IntegrationTests/Startup.cs @@ -53,7 +53,7 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF var node = (INode)app.ApplicationServices.GetService(typeof(INode)); var nodeId = (NodeId)app.ApplicationServices.GetService(typeof(NodeId)); var logger = loggerFactory.CreateLogger(); - node.Start(nodeId.Id); + node.Start(nodeId.Id.ToString()); var jsonSerializerSettings = new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.All diff --git a/test/Rafty.IntegrationTests/Tests.cs b/test/Rafty.IntegrationTests/Tests.cs index d32b868..919889d 100644 --- a/test/Rafty.IntegrationTests/Tests.cs +++ b/test/Rafty.IntegrationTests/Tests.cs @@ -67,7 +67,7 @@ private void GivenAServerIsRunning(string url, string id) .ConfigureServices(x => { x.AddSingleton(webHostBuilder); - x.AddSingleton(new NodeId(guid)); + x.AddSingleton(new NodeId(url)); }) .UseStartup(); diff --git a/test/Rafty.UnitTests/AllServersApplyToStateMachineTests.cs b/test/Rafty.UnitTests/AllServersApplyToStateMachineTests.cs index 014fb99..19bc4ac 100644 --- a/test/Rafty.UnitTests/AllServersApplyToStateMachineTests.cs +++ b/test/Rafty.UnitTests/AllServersApplyToStateMachineTests.cs @@ -37,7 +37,7 @@ public AllServersApplyToStateMachineTests() [Fact] public void FollowerShouldApplyLogsToFsm() { - var currentState = new CurrentState(Guid.NewGuid(), 0, default(Guid), 0, 0, default(Guid)); + var currentState = new CurrentState(Guid.NewGuid().ToString(), 0, default(string), 0, 0, default(string)); var fsm = new Rafty.FiniteStateMachine.InMemoryStateMachine(); var follower = new Follower(currentState, fsm, _log, _random, _node, _settings, _rules, _peers); var log = new LogEntry(new FakeCommand("test"), typeof(string), 1); @@ -59,7 +59,7 @@ public void FollowerShouldApplyLogsToFsm() [Fact] public void CandidateShouldApplyLogsToFsm() { - var currentState = new CurrentState(Guid.NewGuid(), 0, default(Guid), 0, 0, default(Guid)); + var currentState = new CurrentState(Guid.NewGuid().ToString(), 0, default(string), 0, 0, default(string)); var fsm = new Rafty.FiniteStateMachine.InMemoryStateMachine(); var candidate = new Candidate(currentState,fsm, _peers, _log, _random, _node, _settings, _rules); var log = new LogEntry(new FakeCommand("test"), typeof(string), 1); @@ -84,7 +84,7 @@ public void CandidateShouldApplyLogsToFsm() [Fact] public void LeaderShouldApplyLogsToFsm() { - var currentState = new CurrentState(Guid.NewGuid(), 0, default(Guid), 0, 0, default(Guid)); + var currentState = new CurrentState(Guid.NewGuid().ToString(), 0, default(string), 0, 0, default(string)); var fsm = new Rafty.FiniteStateMachine.InMemoryStateMachine(); var leader = new Leader(currentState, fsm, (s) => _peers, _log, _node, _settings, _rules); var log = new LogEntry(new FakeCommand("test"), typeof(string), 1); diff --git a/test/Rafty.UnitTests/AllServersConvertToFollowerTests.cs b/test/Rafty.UnitTests/AllServersConvertToFollowerTests.cs index 3e12f77..be9cccd 100644 --- a/test/Rafty.UnitTests/AllServersConvertToFollowerTests.cs +++ b/test/Rafty.UnitTests/AllServersConvertToFollowerTests.cs @@ -43,7 +43,7 @@ public AllServersConvertToFollowerTests() [InlineData(2, 1, 2)] public void FollowerShouldSetTermAsRpcTermAndStayFollowerWhenReceivesAppendEntries(int currentTerm, int rpcTerm, int expectedTerm) { - var currentState = new CurrentState(Guid.NewGuid(),currentTerm, default(Guid), 0, 0, default(Guid)); + var currentState = new CurrentState(Guid.NewGuid().ToString(),currentTerm, default(string), 0, 0, default(string)); var follower = new Follower(currentState, _fsm, _log, _random, _node, _settings, _rules, _peers); var appendEntriesResponse = follower.Handle(new AppendEntriesBuilder().WithTerm(rpcTerm).Build()); follower.CurrentState.CurrentTerm.ShouldBe(expectedTerm); @@ -54,7 +54,7 @@ public void FollowerShouldSetTermAsRpcTermAndStayFollowerWhenReceivesAppendEntri [InlineData(2, 1, 2)] public void FollowerShouldSetTermAsRpcTermAndStayFollowerWhenReceivesRequestVote(int currentTerm, int rpcTerm, int expectedTerm) { - var currentState = new CurrentState(Guid.NewGuid(), currentTerm, default(Guid), 0, 0, default(Guid)); + var currentState = new CurrentState(Guid.NewGuid().ToString(), currentTerm, default(string), 0, 0, default(string)); var follower = new Follower(currentState, _fsm, _log, _random, _node, _settings, _rules, _peers); var appendEntriesResponse = follower.Handle(new RequestVoteBuilder().WithTerm(rpcTerm).Build()); follower.CurrentState.CurrentTerm.ShouldBe(expectedTerm); @@ -66,7 +66,7 @@ public void FollowerShouldSetTermAsRpcTermAndStayFollowerWhenReceivesRequestVote [InlineData(2, 3, 3)] public void CandidateShouldSetTermAsRpcTermAndBecomeStateWhenReceivesAppendEntries(int currentTerm, int rpcTerm, int expectedTerm) { - var currentState = new CurrentState(Guid.NewGuid(), currentTerm, default(Guid), 0, 0, default(Guid)); + var currentState = new CurrentState(Guid.NewGuid().ToString(), currentTerm, default(string), 0, 0, default(string)); var candidate = new Candidate(currentState, _fsm, _peers, _log, _random, _node, _settings, _rules); var appendEntriesResponse = candidate.Handle(new AppendEntriesBuilder().WithTerm(rpcTerm).Build()); candidate.CurrentState.CurrentTerm.ShouldBe(expectedTerm); @@ -79,7 +79,7 @@ public void CandidateShouldSetTermAsRpcTermAndBecomeStateWhenReceivesAppendEntri [InlineData(2, 3, 3)] public void CandidateShouldSetTermAsRpcTermAndBecomeStateWhenReceivesRequestVote(int currentTerm, int rpcTerm, int expectedTerm) { - var currentState = new CurrentState(Guid.NewGuid(), currentTerm, default(Guid), 0, 0, default(Guid)); + var currentState = new CurrentState(Guid.NewGuid().ToString(), currentTerm, default(string), 0, 0, default(string)); var candidate = new Candidate(currentState, _fsm, _peers, _log, _random, _node, _settings, _rules); var requestVoteResponse = candidate.Handle(new RequestVoteBuilder().WithTerm(rpcTerm).WithLastLogIndex(1).Build()); candidate.CurrentState.CurrentTerm.ShouldBe(expectedTerm); @@ -93,7 +93,7 @@ public void CandidateShouldSetTermAsRpcTermAndBecomeStateWhenReceivesRequestVote [InlineData(2, 3, 3)] public void LeaderShouldSetTermAsRpcTermAndBecomeStateWhenReceivesAppendEntries(int currentTerm, int rpcTerm, int expectedTerm) { - var currentState = new CurrentState(Guid.NewGuid(), currentTerm, default(Guid), 0, 0, default(Guid)); + var currentState = new CurrentState(Guid.NewGuid().ToString(), currentTerm, default(string), 0, 0, default(string)); var leader = new Leader(currentState, _fsm, (s) => _peers, _log, _node, _settings, _rules); var appendEntriesResponse = leader.Handle(new AppendEntriesBuilder().WithTerm(rpcTerm).Build()); leader.CurrentState.CurrentTerm.ShouldBe(expectedTerm); @@ -104,7 +104,7 @@ public void LeaderShouldSetTermAsRpcTermAndBecomeStateWhenReceivesAppendEntries( [InlineData(2, 3, 3)] public void LeaderShouldSetTermAsRpcTermAndBecomeStateWhenReceivesRequestVote(int currentTerm, int rpcTerm, int expectedTerm) { - var currentState = new CurrentState(Guid.NewGuid(), currentTerm, default(Guid), 0, 0, default(Guid)); + var currentState = new CurrentState(Guid.NewGuid().ToString(), currentTerm, default(string), 0, 0, default(string)); var leader = new Leader(currentState, _fsm, (s) => _peers, _log, _node, _settings, _rules); var state = leader.Handle(new RequestVoteBuilder().WithTerm(rpcTerm).WithLastLogIndex(1).Build()); leader.CurrentState.CurrentTerm.ShouldBe(expectedTerm); diff --git a/test/Rafty.UnitTests/AppendEntriesTests.cs b/test/Rafty.UnitTests/AppendEntriesTests.cs index 26b87d4..4cb5962 100644 --- a/test/Rafty.UnitTests/AppendEntriesTests.cs +++ b/test/Rafty.UnitTests/AppendEntriesTests.cs @@ -47,7 +47,7 @@ public AppendEntriesTests() [Fact] public void ShouldReplyFalseIfRpcTermLessThanCurrentTerm() { - _currentState = new CurrentState(Guid.NewGuid(), 1, default(Guid), 0, 0, default(Guid)); + _currentState = new CurrentState(Guid.NewGuid().ToString(), 1, default(string), 0, 0, default(string)); var appendEntriesRpc = new AppendEntriesBuilder().WithTerm(0).Build(); var follower = new Follower(_currentState, _fsm, _log, _random, _node, _settings, _rules, _peers); var appendEntriesResponse = follower.Handle(appendEntriesRpc); @@ -58,7 +58,7 @@ public void ShouldReplyFalseIfRpcTermLessThanCurrentTerm() [Fact] public void ShouldReplyFalseIfLogDoesntContainEntryAtPreviousLogIndexWhoseTermMatchesRpcPrevLogTerm() { - _currentState = new CurrentState(Guid.NewGuid(), 2, default(Guid), 0, 0, default(Guid)); + _currentState = new CurrentState(Guid.NewGuid().ToString(), 2, default(string), 0, 0, default(string)); _log.Apply(new LogEntry(new FakeCommand(""), typeof(string), 2)); var appendEntriesRpc = new AppendEntriesBuilder().WithTerm(2).WithPreviousLogIndex(1).WithPreviousLogTerm(1).Build(); var follower = new Follower(_currentState, _fsm, _log, _random, _node, _settings, _rules, _peers); @@ -70,7 +70,7 @@ public void ShouldReplyFalseIfLogDoesntContainEntryAtPreviousLogIndexWhoseTermMa [Fact] public void ShouldDeleteExistingEntryIfItConflictsWithNewOne() { - _currentState = new CurrentState(Guid.NewGuid(), 1, default(Guid), 2, 0, default(Guid)); + _currentState = new CurrentState(Guid.NewGuid().ToString(), 1, default(string), 2, 0, default(string)); _log.Apply(new LogEntry(new FakeCommand("term 1 commit index 0"), typeof(string), 1)); _log.Apply(new LogEntry(new FakeCommand("term 1 commit index 1"), typeof(string), 1)); _log.Apply(new LogEntry(new FakeCommand("term 1 commit index 2"), typeof(string), 1)); @@ -89,7 +89,7 @@ public void ShouldDeleteExistingEntryIfItConflictsWithNewOne() [Fact] public void ShouldDeleteExistingEntryIfItConflictsWithNewOneAndAppendNewEntries() { - _currentState = new CurrentState(Guid.NewGuid(), 1, default(Guid), 0, 0, default(Guid)); + _currentState = new CurrentState(Guid.NewGuid().ToString(), 1, default(string), 0, 0, default(string)); _log.Apply(new LogEntry(new FakeCommand("term 1 commit index 0"), typeof(string), 1)); _log.Apply(new LogEntry(new FakeCommand("term 1 commit index 1"), typeof(string), 1)); _log.Apply(new LogEntry(new FakeCommand("term 1 commit index 2"), typeof(string), 1)); @@ -109,14 +109,14 @@ public void ShouldDeleteExistingEntryIfItConflictsWithNewOneAndAppendNewEntries( [Fact] public void ShouldAppendAnyEntriesNotInTheLog() { - _currentState = new CurrentState(Guid.NewGuid(), 1, default(Guid), 0, 0, default(Guid)); + _currentState = new CurrentState(Guid.NewGuid().ToString(), 1, default(string), 0, 0, default(string)); _log.Apply(new LogEntry(new FakeCommand("term 1 commit index 0"), typeof(string), 1)); var appendEntriesRpc = new AppendEntriesBuilder() .WithEntry(new LogEntry(new FakeCommand("term 1 commit index 1"), typeof(string), 1)) .WithTerm(1) .WithPreviousLogIndex(1) .WithPreviousLogTerm(1) - .WithLeaderId(Guid.NewGuid()) + .WithLeaderId(Guid.NewGuid().ToString()) .Build(); var follower = new Follower(_currentState, _fsm, _log, _random, _node, _settings, _rules, _peers); var appendEntriesResponse = follower.Handle(appendEntriesRpc); @@ -129,7 +129,7 @@ public void ShouldAppendAnyEntriesNotInTheLog() [Fact] public void FollowerShouldSetCommitIndexIfLeaderCommitGreaterThanCommitIndex() { - _currentState = new CurrentState(Guid.NewGuid(), 1, default(Guid), 0, 0, default(Guid)); + _currentState = new CurrentState(Guid.NewGuid().ToString(), 1, default(string), 0, 0, default(string)); var log = new LogEntry(new FakeCommand("term 1 commit index 0"), typeof(string), 1); _log.Apply(log); var appendEntriesRpc = new AppendEntriesBuilder() @@ -148,7 +148,7 @@ public void FollowerShouldSetCommitIndexIfLeaderCommitGreaterThanCommitIndex() [Fact] public void CandidateShouldSetCommitIndexIfLeaderCommitGreaterThanCommitIndex() { - _currentState = new CurrentState(Guid.NewGuid(), 0, default(Guid), 0, 0, default(Guid)); + _currentState = new CurrentState(Guid.NewGuid().ToString(), 0, default(string), 0, 0, default(string)); //assume log applied by node? var log = new LogEntry(new FakeCommand("term 1 commit index 0"), typeof(string), 1); _log.Apply(log); @@ -158,7 +158,7 @@ public void CandidateShouldSetCommitIndexIfLeaderCommitGreaterThanCommitIndex() .WithPreviousLogIndex(1) .WithPreviousLogTerm(1) .WithLeaderCommitIndex(1) - .WithLeaderId(Guid.NewGuid()) + .WithLeaderId(Guid.NewGuid().ToString()) .Build(); var candidate = new Candidate(_currentState, _fsm, _peers, _log, _random, _node, _settings, _rules); var appendEntriesResponse = candidate.Handle(appendEntriesRpc); @@ -169,7 +169,7 @@ public void CandidateShouldSetCommitIndexIfLeaderCommitGreaterThanCommitIndex() [Fact] public void LeaderShouldSetCommitIndexIfLeaderCommitGreaterThanCommitIndex() { - _currentState = new CurrentState(Guid.NewGuid(), 0, default(Guid), 0, 0, default(Guid)); + _currentState = new CurrentState(Guid.NewGuid().ToString(), 0, default(string), 0, 0, default(string)); //assume log applied by node? var log = new LogEntry(new FakeCommand("term 1 commit index 0"), typeof(string), 1); _log.Apply(log); @@ -179,7 +179,7 @@ public void LeaderShouldSetCommitIndexIfLeaderCommitGreaterThanCommitIndex() .WithPreviousLogIndex(1) .WithPreviousLogTerm(1) .WithLeaderCommitIndex(1) - .WithLeaderId(Guid.NewGuid()) + .WithLeaderId(Guid.NewGuid().ToString()) .Build(); var leader = new Leader(_currentState, _fsm, (s) => _peers, _log, _node, _settings, _rules); var state = leader.Handle(appendEntriesRpc); diff --git a/test/Rafty.UnitTests/CandidateTests.cs b/test/Rafty.UnitTests/CandidateTests.cs index 247853c..745571a 100644 --- a/test/Rafty.UnitTests/CandidateTests.cs +++ b/test/Rafty.UnitTests/CandidateTests.cs @@ -28,7 +28,7 @@ public class CandidateTests private List _peers; private IRandomDelay _random; private INode _node; - private readonly Guid _id; + private readonly string _id; private CurrentState _currentState; private InMemorySettings _settings; private IRules _rules; @@ -40,9 +40,9 @@ public CandidateTests() _log = new InMemoryLog(); _peers = new List(); _fsm = new InMemoryStateMachine(); - _id = Guid.NewGuid(); + _id = Guid.NewGuid().ToString(); _node = new NothingNode(); - _currentState = new CurrentState(_id, 0, default(Guid), 0, 0, default(Guid)); + _currentState = new CurrentState(_id, 0, default(string), 0, 0, default(string)); } [Fact] @@ -211,13 +211,13 @@ public void ShouldVoteForSelfWhenElectionStarts() public void ShouldVoteForNewCandidateInAnotherTermsElection() { _node = new NothingNode(); - _currentState = new CurrentState(Guid.NewGuid(), 0, default(Guid), 0, 0, default(Guid)); + _currentState = new CurrentState(Guid.NewGuid().ToString(), 0, default(string), 0, 0, default(string)); var candidate = new Candidate(_currentState, _fsm, _peers, _log, _random, _node, _settings, _rules); - var requestVote = new RequestVoteBuilder().WithTerm(0).WithCandidateId(Guid.NewGuid()).WithLastLogIndex(1).Build(); + var requestVote = new RequestVoteBuilder().WithTerm(0).WithCandidateId(Guid.NewGuid().ToString()).WithLastLogIndex(1).Build(); var requestVoteResponse = candidate.Handle(requestVote); candidate.CurrentState.VotedFor.ShouldBe(requestVote.CandidateId); requestVoteResponse.VoteGranted.ShouldBeTrue(); - requestVote = new RequestVoteBuilder().WithTerm(1).WithCandidateId(Guid.NewGuid()).WithLastLogIndex(1).Build(); + requestVote = new RequestVoteBuilder().WithTerm(1).WithCandidateId(Guid.NewGuid().ToString()).WithLastLogIndex(1).Build(); requestVoteResponse = candidate.Handle(requestVote); requestVoteResponse.VoteGranted.ShouldBeTrue(); candidate.CurrentState.VotedFor.ShouldBe(requestVote.CandidateId); diff --git a/test/Rafty.UnitTests/FakePeer.cs b/test/Rafty.UnitTests/FakePeer.cs index 3517fcf..296e35a 100644 --- a/test/Rafty.UnitTests/FakePeer.cs +++ b/test/Rafty.UnitTests/FakePeer.cs @@ -13,14 +13,14 @@ internal class FakePeer : IPeer private readonly bool _appendEntryTwo; private bool _appendEntryThree; private int _term = 1; - private Guid _id; + private string _id; public int ReceivedCommands; public FakePeer() { } - public FakePeer(Guid id) + public FakePeer(string id) { _id = id; } @@ -30,7 +30,7 @@ public FakePeer(int term) _term = term; } - public FakePeer(int term, Guid id) + public FakePeer(int term, string id) { _term = term; _id = id; @@ -66,7 +66,7 @@ public FakePeer(bool grantVote, bool appendEntry, bool appendEntryTwo, bool appe public List AppendEntriesResponses { get; } = new List(); - public Guid Id => _id; + public string Id => _id; public RequestVoteResponse Request(RequestVote requestVote) { diff --git a/test/Rafty.UnitTests/FollowerTests.cs b/test/Rafty.UnitTests/FollowerTests.cs index 8e5aa5f..1887dd7 100644 --- a/test/Rafty.UnitTests/FollowerTests.cs +++ b/test/Rafty.UnitTests/FollowerTests.cs @@ -36,14 +36,14 @@ public FollowerTests() _peers = new List(); _fsm = new InMemoryStateMachine(); _peersProvider = new InMemoryPeersProvider(_peers); - _currentState = new CurrentState(Guid.NewGuid(), 0, default(Guid), -1, -1, default(Guid)); + _currentState = new CurrentState(Guid.NewGuid().ToString(), 0, default(string), -1, -1, default(string)); } [Fact] public void CommitIndexShouldBeInitialisedToMinusOne() { _node = new Node(_fsm, _log, _settings, _peersProvider); - _node.Start(Guid.NewGuid()); + _node.Start(Guid.NewGuid().ToString()); _node.State.CurrentState.CommitIndex.ShouldBe(0); } @@ -51,7 +51,7 @@ public void CommitIndexShouldBeInitialisedToMinusOne() public void CurrentTermShouldBeInitialisedToZero() { _node = new Node(_fsm, _log, _settings, _peersProvider); - _node.Start(Guid.NewGuid()); + _node.Start(Guid.NewGuid().ToString()); _node.State.CurrentState.CurrentTerm.ShouldBe(0); } @@ -59,7 +59,7 @@ public void CurrentTermShouldBeInitialisedToZero() public void LastAppliedShouldBeInitialisedToZero() { _node = new Node(_fsm, _log, _settings, _peersProvider); - _node.Start(Guid.NewGuid()); + _node.Start(Guid.NewGuid().ToString()); _node.State.CurrentState.LastApplied.ShouldBe(0); } @@ -88,7 +88,7 @@ public void ShouldBecomeCandidateWhenFollowerReceivesTimeoutAndHasNotHeardFromLe public void ShouldNotBecomeCandidateWhenFollowerReceivesTimeoutAndHasHeardFromLeader() { _node = new Node(_fsm, _log, _settings, _peersProvider); - _node.Start(Guid.NewGuid()); + _node.Start(Guid.NewGuid().ToString()); _node.State.ShouldBeOfType(); _node.Handle(new AppendEntriesBuilder().WithTerm(1).WithLeaderCommitIndex(-1).Build()); _node.State.ShouldBeOfType(); @@ -98,7 +98,7 @@ public void ShouldNotBecomeCandidateWhenFollowerReceivesTimeoutAndHasHeardFromLe public void ShouldNotBecomeCandidateWhenFollowerReceivesTimeoutAndHasHeardFromLeaderSinceLastTimeout() { _node = new Node(_fsm, _log, _settings, _peersProvider); - _node.Start(Guid.NewGuid()); + _node.Start(Guid.NewGuid().ToString()); _node.State.ShouldBeOfType(); _node.Handle(new AppendEntriesBuilder().WithTerm(1).WithLeaderCommitIndex(-1).Build()); _node.State.ShouldBeOfType(); @@ -110,7 +110,7 @@ public void ShouldNotBecomeCandidateWhenFollowerReceivesTimeoutAndHasHeardFromLe public void ShouldStartAsFollower() { _node = new Node(_fsm, _log, _settings, _peersProvider); - _node.Start(Guid.NewGuid()); + _node.Start(Guid.NewGuid().ToString()); _node.State.ShouldBeOfType(); } @@ -118,17 +118,17 @@ public void ShouldStartAsFollower() public void VotedForShouldBeInitialisedToNone() { _node = new Node(_fsm, _log, _settings, _peersProvider); - _node.Start(Guid.NewGuid()); - _node.State.CurrentState.VotedFor.ShouldBe(default(Guid)); + _node.Start(Guid.NewGuid().ToString()); + _node.State.CurrentState.VotedFor.ShouldBe(default(string)); } [Fact] public void ShouldUpdateVotedFor() { _node = new NothingNode(); - _currentState = new CurrentState(Guid.NewGuid(), 0, default(Guid), 0, 0, default(Guid)); + _currentState = new CurrentState(Guid.NewGuid().ToString(), 0, default(string), 0, 0, default(string)); var follower = new Follower(_currentState, _fsm, _log, _random, _node, _settings, _rules, _peers); - var requestVote = new RequestVoteBuilder().WithCandidateId(Guid.NewGuid()).WithLastLogIndex(1).Build(); + var requestVote = new RequestVoteBuilder().WithCandidateId(Guid.NewGuid().ToString()).WithLastLogIndex(1).Build(); var requestVoteResponse = follower.Handle(requestVote); follower.CurrentState.VotedFor.ShouldBe(requestVote.CandidateId); } @@ -137,13 +137,13 @@ public void ShouldUpdateVotedFor() public void ShouldVoteForNewCandidateInAnotherTermsElection() { _node = new NothingNode(); - _currentState = new CurrentState(Guid.NewGuid(), 0, default(Guid), 0, 0, default(Guid)); + _currentState = new CurrentState(Guid.NewGuid().ToString(), 0, default(string), 0, 0, default(string)); var follower = new Follower(_currentState, _fsm, _log, _random, _node, _settings,_rules, _peers); - var requestVote = new RequestVoteBuilder().WithTerm(0).WithCandidateId(Guid.NewGuid()).WithLastLogIndex(1).Build(); + var requestVote = new RequestVoteBuilder().WithTerm(0).WithCandidateId(Guid.NewGuid().ToString()).WithLastLogIndex(1).Build(); var requestVoteResponse = follower.Handle(requestVote); follower.CurrentState.VotedFor.ShouldBe(requestVote.CandidateId); requestVoteResponse.VoteGranted.ShouldBeTrue(); - requestVote = new RequestVoteBuilder().WithTerm(1).WithCandidateId(Guid.NewGuid()).WithLastLogIndex(1).Build(); + requestVote = new RequestVoteBuilder().WithTerm(1).WithCandidateId(Guid.NewGuid().ToString()).WithLastLogIndex(1).Build(); requestVoteResponse = follower.Handle(requestVote); requestVoteResponse.VoteGranted.ShouldBeTrue(); follower.CurrentState.VotedFor.ShouldBe(requestVote.CandidateId); @@ -153,7 +153,7 @@ public void ShouldVoteForNewCandidateInAnotherTermsElection() public void FollowerShouldForwardCommandToLeader() { _node = new NothingNode(); - var leaderId = Guid.NewGuid(); + var leaderId = Guid.NewGuid().ToString(); var leader = new FakePeer(leaderId); _peers = new List { diff --git a/test/Rafty.UnitTests/LeaderTests.cs b/test/Rafty.UnitTests/LeaderTests.cs index 9a782c4..b71efdb 100644 --- a/test/Rafty.UnitTests/LeaderTests.cs +++ b/test/Rafty.UnitTests/LeaderTests.cs @@ -16,7 +16,7 @@ public class LeaderTests { private readonly IFiniteStateMachine _fsm; private INode _node; - private readonly Guid _id; + private readonly string _id; private CurrentState _currentState; private List _peers; private readonly ILog _log; @@ -32,8 +32,8 @@ public LeaderTests() _log = new InMemoryLog(); _peers = new List(); _fsm = new InMemoryStateMachine(); - _id = Guid.NewGuid(); - _currentState = new CurrentState(_id, 0, default(Guid), 0, 0, default(Guid)); + _id = Guid.NewGuid().ToString(); + _currentState = new CurrentState(_id, 0, default(string), 0, 0, default(string)); _node = new NothingNode(); } @@ -45,7 +45,7 @@ public void ShouldSendEmptyAppendEntriesRpcOnElection() { _peers.Add(new FakePeer(true)); } - _currentState = new CurrentState(_id, 0, default(Guid), 0, 0, default(Guid)); + _currentState = new CurrentState(_id, 0, default(string), 0, 0, default(string)); var leader = new Leader(_currentState, _fsm, (s) => _peers, _log, _node, _settings, _rules); bool TestPeers(List peers) { @@ -77,7 +77,7 @@ public void ShouldAppendCommandToLocalLog() _peers.Add(peer); } var log = new InMemoryLog(); - _currentState = new CurrentState(_id, 0, default(Guid), 0, 0, default(Guid)); + _currentState = new CurrentState(_id, 0, default(string), 0, 0, default(string)); var leader = new Leader(_currentState, _fsm, (s) => _peers, log, _node, _settings, _rules); leader.Accept(new FakeCommand()); log.ExposedForTesting.Count.ShouldBe(1); @@ -94,7 +94,7 @@ public void ShouldApplyCommandToStateMachine() _peers.Add(peer); } var log = new InMemoryLog(); - _currentState = new CurrentState(_id, 0, default(Guid), 0, 0, default(Guid)); + _currentState = new CurrentState(_id, 0, default(string), 0, 0, default(string)); var leader = new Leader(_currentState, _fsm, (s) => _peers, log, _node, _settings, _rules); var response = leader.Accept(new FakeCommand()); log.ExposedForTesting.Count.ShouldBe(1); @@ -109,7 +109,7 @@ public void ShouldHandleCommandIfNoPeers() { _peers = new List(); var log = new InMemoryLog(); - _currentState = new CurrentState(_id, 0, default(Guid), 0, 0, default(Guid)); + _currentState = new CurrentState(_id, 0, default(string), 0, 0, default(string)); var leader = new Leader(_currentState, _fsm, (s) => _peers, log, _node, _settings, _rules); var response = leader.Accept(new FakeCommand()); log.ExposedForTesting.Count.ShouldBe(1); @@ -126,7 +126,7 @@ public void ShouldInitialiseNextIndex() { _peers.Add(new FakePeer(true)); } - _currentState = new CurrentState(_id, 0, default(Guid), 0, 0, default(Guid)); + _currentState = new CurrentState(_id, 0, default(string), 0, 0, default(string)); var leader = new Leader(_currentState, _fsm, (s) => _peers, _log, _node, _settings, _rules); leader.PeerStates.ForEach(pS => @@ -143,7 +143,7 @@ public void ShouldInitialiseMatchIndex() { _peers.Add(new FakePeer(true)); } - _currentState = new CurrentState(_id, 0, default(Guid), 0, 0, default(Guid)); + _currentState = new CurrentState(_id, 0, default(string), 0, 0, default(string)); var leader = new Leader(_currentState,_fsm, (s) => _peers, _log, _node, _settings, _rules); leader.PeerStates.ForEach(pS => { @@ -157,9 +157,9 @@ public void ShouldInitialiseNextAndMatchIndexWhenNewPeerJoins() _peers = new List(); for (var i = 0; i < 1; i++) { - _peers.Add(new FakePeer(Guid.NewGuid())); + _peers.Add(new FakePeer(Guid.NewGuid().ToString())); } - _currentState = new CurrentState(_id, 0, default(Guid), 0, 0, default(Guid)); + _currentState = new CurrentState(_id, 0, default(string), 0, 0, default(string)); var leader = new Leader(_currentState,_fsm, (s) => _peers, _log, _node, _settings, _rules); leader.PeerStates.Count.ShouldBe(1); leader.PeerStates.ForEach(pS => @@ -170,7 +170,7 @@ public void ShouldInitialiseNextAndMatchIndexWhenNewPeerJoins() for (var i = 0; i < 3; i++) { - _peers.Add(new FakePeer(Guid.NewGuid())); + _peers.Add(new FakePeer(Guid.NewGuid().ToString())); } bool TestPeerStates() @@ -208,7 +208,7 @@ public void ShouldSendAppendEntriesStartingAtNextIndex() _log.Apply(logTwo); var logThree = new LogEntry(new FakeCommand("3"), typeof(string), 1); _log.Apply(logThree); - _currentState = new CurrentState(_id, 1, default(Guid), 2, 2, default(Guid)); + _currentState = new CurrentState(_id, 1, default(string), 2, 2, default(string)); var leader = new Leader(_currentState, _fsm, (s) => _peers, _log, _node, _settings, _rules); var logs = _log.GetFrom(1); logs.Count.ShouldBe(3); @@ -223,7 +223,7 @@ public void ShouldUpdateMatchIndexAndNextIndexIfSuccessful() _peers.Add(new FakePeer(true, true, true)); } //add 3 logs - _currentState = new CurrentState(_id, 1, default(Guid), 2, 2, default(Guid)); + _currentState = new CurrentState(_id, 1, default(string), 2, 2, default(string)); var logOne = new LogEntry(new FakeCommand("1"), typeof(string), 1); _log.Apply(logOne); var logTwo = new LogEntry(new FakeCommand("2"), typeof(string), 1); @@ -267,7 +267,7 @@ public void ShouldDecrementNextIndexAndRetry() _peers.Add(peer); } - _currentState = new CurrentState(_id, 1, default(Guid), 1, 1, default(Guid)); + _currentState = new CurrentState(_id, 1, default(string), 1, 1, default(string)); var leader = new Leader(_currentState, _fsm, (s) => _peers, _log, _node, _settings, _rules); //send first command, this wont get commited because the guys are replying false @@ -421,7 +421,7 @@ public void ShouldSetCommitIndex() _peers.Add(peer); } //add 3 logs - _currentState = new CurrentState(_id, 1, default(Guid), 0, 0, default(Guid)); + _currentState = new CurrentState(_id, 1, default(string), 0, 0, default(string)); var leader = new Leader(_currentState, _fsm, (s) => _peers, _log, _node, _settings, _rules); leader.Accept(new FakeCommand()); leader.Accept(new FakeCommand()); @@ -459,7 +459,7 @@ public void ShouldBeAbleToHandleWhenLeaderHasNoLogsAndCandidatesReturnSuccess() { _peers.Add(new FakePeer(true, true, true)); } - _currentState = new CurrentState(_id, 1, default(Guid), 0, 0, default(Guid)); + _currentState = new CurrentState(_id, 1, default(string), 0, 0, default(string)); var leader = new Leader(_currentState,_fsm, (s) => _peers, _log, _node, _settings, _rules); bool TestPeerStates(List peerState) { @@ -494,7 +494,7 @@ public void ShouldReplicateCommand() { _peers.Add(new FakePeer(true, true, true)); } - _currentState = new CurrentState(_id, 1, default(Guid), 0, 0, default(Guid)); + _currentState = new CurrentState(_id, 1, default(string), 0, 0, default(string)); var leader = new Leader(_currentState,_fsm, (s) => _peers, _log, _node, _settings, _rules); var command = new FakeCommand(); var response = leader.Accept(command); @@ -530,7 +530,7 @@ public void ShouldBeAbleToHandleWhenLeaderHasNoLogsAndCandidatesReturnFail() { _peers.Add(new FakePeer(true, true, true)); } - _currentState = new CurrentState(_id, 1, default(Guid), 0, 0, default(Guid)); + _currentState = new CurrentState(_id, 1, default(string), 0, 0, default(string)); var leader = new Leader(_currentState, _fsm, (s) => _peers, _log, _node, _settings, _rules); bool TestPeerStates(List peerState) { @@ -563,7 +563,7 @@ public void ShouldTimeoutAfterXSecondsIfCannotReplicateCommand() { _peers.Add(new FakePeer(false, false, false)); } - _currentState = new CurrentState(_id, 1, default(Guid), 0, 0, default(Guid)); + _currentState = new CurrentState(_id, 1, default(string), 0, 0, default(string)); _settings = new InMemorySettingsBuilder().WithCommandTimeout(1).Build(); var leader = new Leader(_currentState,_fsm, (s) => _peers, _log, _node, _settings, _rules); var command = new FakeCommand(); @@ -605,7 +605,7 @@ public void ShouldTimeoutAfterXSecondsIfCannotReplicateCommandAndRollbackIndexes _peers.Add(new FakePeer(true, true, true)); - _currentState = new CurrentState(_id, 1, default(Guid), 0, 0, default(Guid)); + _currentState = new CurrentState(_id, 1, default(string), 0, 0, default(string)); _settings = new InMemorySettingsBuilder().WithCommandTimeout(1).Build(); var leader = new Leader(_currentState,_fsm, (s) => _peers, _log, _node, _settings, _rules); var command = new FakeCommand(); diff --git a/test/Rafty.UnitTests/NothingNode.cs b/test/Rafty.UnitTests/NothingNode.cs index cfbb722..e7c73c9 100644 --- a/test/Rafty.UnitTests/NothingNode.cs +++ b/test/Rafty.UnitTests/NothingNode.cs @@ -37,7 +37,7 @@ public RequestVoteResponse Handle(RequestVote requestVote) return new RequestVoteResponseBuilder().Build(); } - public void Start(Guid id) + public void Start(string id) { throw new System.NotImplementedException(); } diff --git a/test/Rafty.UnitTests/RemoteControledPeer.cs b/test/Rafty.UnitTests/RemoteControledPeer.cs index beac030..07cfa7d 100644 --- a/test/Rafty.UnitTests/RemoteControledPeer.cs +++ b/test/Rafty.UnitTests/RemoteControledPeer.cs @@ -13,10 +13,10 @@ public class RemoteControledPeer : IPeer public RemoteControledPeer() { - Id = Guid.NewGuid(); + Id = Guid.NewGuid().ToString(); } - public Guid Id { get; } + public string Id { get; } public void SetRequestVoteResponse(RequestVoteResponse requestVoteResponse) { diff --git a/test/Rafty.UnitTests/RequestVoteTests.cs b/test/Rafty.UnitTests/RequestVoteTests.cs index eb975ac..b03d5f8 100644 --- a/test/Rafty.UnitTests/RequestVoteTests.cs +++ b/test/Rafty.UnitTests/RequestVoteTests.cs @@ -44,7 +44,7 @@ public void Dispose() [Fact] public void FollowerShouldReplyFalseIfTermIsLessThanCurrentTerm() { - _currentState = new CurrentState(Guid.NewGuid(), 1, default(Guid), 1, 0, default(Guid)); + _currentState = new CurrentState(Guid.NewGuid().ToString(), 1, default(string), 1, 0, default(string)); var requestVoteRpc = new RequestVoteBuilder().WithTerm(0).Build(); var follower = new Follower(_currentState, _fsm, _log, _random, _node, _settings,_rules, _peers); var requestVoteResponse = follower.Handle(requestVoteRpc); @@ -55,7 +55,7 @@ public void FollowerShouldReplyFalseIfTermIsLessThanCurrentTerm() [Fact] public void FollowerShouldReplyFalseIfVotedForIsNotDefault() { - _currentState = new CurrentState(Guid.NewGuid(), 1, Guid.NewGuid(), 1, 0, default(Guid)); + _currentState = new CurrentState(Guid.NewGuid().ToString(), 1, Guid.NewGuid().ToString(), 1, 0, default(string)); var requestVoteRpc = new RequestVoteBuilder().WithTerm(0).Build(); var follower = new Follower(_currentState, _fsm, _log, _random, _node, _settings,_rules, _peers); var requestVoteResponse = follower.Handle(requestVoteRpc); @@ -66,8 +66,8 @@ public void FollowerShouldReplyFalseIfVotedForIsNotDefault() [Fact] public void FollowerShouldReplyFalseIfVotedForIsNotCandidateId() { - _currentState = new CurrentState(Guid.NewGuid(), 1, Guid.NewGuid(), 1, 0, default(Guid)); - var requestVoteRpc = new RequestVoteBuilder().WithCandidateId(Guid.NewGuid()).WithTerm(0).Build(); + _currentState = new CurrentState(Guid.NewGuid().ToString(), 1, Guid.NewGuid().ToString(), 1, 0, default(string)); + var requestVoteRpc = new RequestVoteBuilder().WithCandidateId(Guid.NewGuid().ToString()).WithTerm(0).Build(); var follower = new Follower(_currentState, _fsm, _log, _random, _node, _settings,_rules, _peers); var requestVoteResponse = follower.Handle(requestVoteRpc); requestVoteResponse.VoteGranted.ShouldBe(false); @@ -77,7 +77,7 @@ public void FollowerShouldReplyFalseIfVotedForIsNotCandidateId() [Fact] public void FollowerShouldGrantVote() { - _currentState = new CurrentState(Guid.NewGuid(), 1, default(Guid), 1, 0, default(Guid)); + _currentState = new CurrentState(Guid.NewGuid().ToString(), 1, default(string), 1, 0, default(string)); var requestVoteRpc = new RequestVoteBuilder().WithLastLogIndex(1).WithLastLogTerm(0).WithTerm(1).Build(); var follower = new Follower(_currentState, _fsm, _log, _random, _node, _settings,_rules, _peers); var requestVoteResponse = follower.Handle(requestVoteRpc); diff --git a/test/Rafty.UnitTests/TestingNode.cs b/test/Rafty.UnitTests/TestingNode.cs index ee4663d..2882199 100644 --- a/test/Rafty.UnitTests/TestingNode.cs +++ b/test/Rafty.UnitTests/TestingNode.cs @@ -48,7 +48,7 @@ public RequestVoteResponse Handle(RequestVote requestVote) return State.Handle(requestVote); } - public void Start(Guid id) + public void Start(string id) { throw new NotImplementedException(); } From f2e0be3ab7f4cdbb266fd8417374191b63ce0dd4 Mon Sep 17 00:00:00 2001 From: Tom Gardham-Pallister Date: Thu, 21 Dec 2017 19:50:57 +0000 Subject: [PATCH 2/2] tests passing with string as id --- test/Rafty.IntegrationTests/FilePeer.cs | 1 - test/Rafty.IntegrationTests/FilePeersProvider.cs | 2 +- test/Rafty.IntegrationTests/HttpPeer.cs | 2 +- test/Rafty.IntegrationTests/Tests.cs | 12 +++++------- test/Rafty.IntegrationTests/peers.json | 5 ----- 5 files changed, 7 insertions(+), 15 deletions(-) diff --git a/test/Rafty.IntegrationTests/FilePeer.cs b/test/Rafty.IntegrationTests/FilePeer.cs index f2c3a3c..46e42bc 100644 --- a/test/Rafty.IntegrationTests/FilePeer.cs +++ b/test/Rafty.IntegrationTests/FilePeer.cs @@ -2,7 +2,6 @@ namespace Rafty.IntegrationTests { public class FilePeer { - public string Id { get; set; } public string HostAndPort { get; set; } } } diff --git a/test/Rafty.IntegrationTests/FilePeersProvider.cs b/test/Rafty.IntegrationTests/FilePeersProvider.cs index 30928ea..0d7c25a 100644 --- a/test/Rafty.IntegrationTests/FilePeersProvider.cs +++ b/test/Rafty.IntegrationTests/FilePeersProvider.cs @@ -19,7 +19,7 @@ public FilePeersProvider(IOptions options) foreach (var item in _options.Value.Peers) { var httpClient = new HttpClient(); - var httpPeer = new HttpPeer(item.HostAndPort, item.Id, httpClient); + var httpPeer = new HttpPeer(item.HostAndPort, httpClient); _peers.Add(httpPeer); } } diff --git a/test/Rafty.IntegrationTests/HttpPeer.cs b/test/Rafty.IntegrationTests/HttpPeer.cs index 6aba499..3f25082 100644 --- a/test/Rafty.IntegrationTests/HttpPeer.cs +++ b/test/Rafty.IntegrationTests/HttpPeer.cs @@ -12,7 +12,7 @@ public class HttpPeer : IPeer private HttpClient _httpClient; private JsonSerializerSettings _jsonSerializerSettings; - public HttpPeer(string hostAndPort, string id, HttpClient httpClient) + public HttpPeer(string hostAndPort, HttpClient httpClient) { Id = hostAndPort; _hostAndPort = hostAndPort; diff --git a/test/Rafty.IntegrationTests/Tests.cs b/test/Rafty.IntegrationTests/Tests.cs index 919889d..2a737b1 100644 --- a/test/Rafty.IntegrationTests/Tests.cs +++ b/test/Rafty.IntegrationTests/Tests.cs @@ -41,8 +41,8 @@ public void Dispose() foreach (var peer in _peers.Peers) { - File.Delete(peer.Id); - File.Delete($"{peer.Id.ToString()}.db"); + File.Delete(peer.HostAndPort.Replace("/", "").Replace(":", "")); + File.Delete($"{peer.HostAndPort.Replace("/", "").Replace(":", "")}.db"); } } @@ -56,10 +56,8 @@ public void ShouldPersistCommandToFiveServers() ThenTheCommandIsReplicatedToAllStateMachines(command); } - private void GivenAServerIsRunning(string url, string id) + private void GivenAServerIsRunning(string url) { - var guid = Guid.Parse(id); - IWebHostBuilder webHostBuilder = new WebHostBuilder(); webHostBuilder.UseUrls(url) .UseKestrel() @@ -85,7 +83,7 @@ private void GivenFiveServersAreRunning() foreach (var peer in _peers.Peers) { - var thread = new Thread(() => GivenAServerIsRunning(peer.HostAndPort, peer.Id)); + var thread = new Thread(() => GivenAServerIsRunning(peer.HostAndPort)); thread.Start(); _threads.Add(thread); } @@ -140,7 +138,7 @@ bool CommandCalledOnAllStateMachines() foreach (var peer in _peers.Peers) { string fsmData; - fsmData = File.ReadAllText(peer.Id); + fsmData = File.ReadAllText(peer.HostAndPort.Replace("/", "").Replace(":", "")); fsmData.ShouldNotBeNullOrEmpty(); var fakeCommand = JsonConvert.DeserializeObject(fsmData); fakeCommand.Value.ShouldBe(command.Value); diff --git a/test/Rafty.IntegrationTests/peers.json b/test/Rafty.IntegrationTests/peers.json index f9ad909..d81d183 100644 --- a/test/Rafty.IntegrationTests/peers.json +++ b/test/Rafty.IntegrationTests/peers.json @@ -1,22 +1,17 @@ { "Peers": [{ - "Id": "2f40ace0-5023-4c4e-853d-50db97ba4e25", "HostAndPort": "http://localhost:5000" }, { - "Id": "51e4ef33-52ad-4cb8-b571-5aa791adeda8", "HostAndPort": "http://localhost:5002" }, { - "Id": "ce79e5fe-9c44-4c4b-9b64-4ee4bb124177", "HostAndPort": "http://localhost:5003" }, { - "Id": "9a589455-24b4-46fe-a243-49e7fbf1c028", "HostAndPort": "http://localhost:5004" }, { - "Id": "41f54603-9898-4850-ae40-29b40630110e", "HostAndPort": "http://localhost:5001" } ]