public void TestParsePulse() { var pulse1 = OperationalMessage.MessageFrom(OperationalMessage.PULSE + "\n" + "id=1"); Assert.True(pulse1.IsPulse); Assert.Equal(Id.Of(1), pulse1.Id); var expectedPulse1 = new Pulse(Id.Of(1)); Assert.Equal(expectedPulse1, pulse1); var pulse100 = OperationalMessage.MessageFrom(OperationalMessage.PULSE + "\n" + "id=100"); Assert.True(pulse100.IsPulse); Assert.Equal(Id.Of(100), pulse100.Id); var expectedPulse100 = new Pulse(Id.Of(100)); Assert.Equal(expectedPulse100, pulse100); }
public void TestParseLeave() { var leave1 = OperationalMessage.MessageFrom(MessageFixtures.LeaveAsText()); Assert.True(leave1.IsLeave); Assert.Equal(Id.Of(1), leave1.Id); var expectedLeave1 = new Leave(Id.Of(1)); Assert.Equal(expectedLeave1, leave1); var leave100 = OperationalMessage.MessageFrom(OperationalMessage.LEAVE + "\n" + "id=100"); Assert.True(leave100.IsLeave); Assert.Equal(Id.Of(100), leave100.Id); var expectedLeave100 = new Leave(Id.Of(100)); Assert.Equal(expectedLeave100, leave100); }
public void TestParsePing() { var ping1 = OperationalMessage.MessageFrom(OperationalMessage.PING + "\n" + "id=1"); Assert.True(ping1.IsPing); Assert.Equal(Id.Of(1), ping1.Id); var expectedPing1 = new Ping(Id.Of(1)); Assert.Equal(expectedPing1, ping1); var ping100 = OperationalMessage.MessageFrom(OperationalMessage.PING + "\n" + "id=100"); Assert.True(ping100.IsPing); Assert.Equal(Id.Of(100), ping100.Id); var expectedPing100 = new Ping(Id.Of(100)); Assert.Equal(expectedPing100, ping100); }
public void TestKnownSizeWithAppend() { var buffer = new MemoryStream(1000); var node1 = NextNodeWith(1); var join = new Join(node1); MessageConverters.MessageToBytes(join, buffer); buffer.Flip(); var messageSize = buffer.Length; var message = new RawMessage(messageSize); // known size message.Header(RawMessageHeader.From(node1.Id.Value, 0, messageSize)); message.Append(buffer.ToArray(), 0, messageSize); Assert.Equal(node1.Id.Value, message.Header().NodeId); Assert.Equal(messageSize, message.Header().Length); Assert.Equal(join, OperationalMessage.MessageFrom(message.AsTextMessage())); }
public void TestParseElect() { var elect1 = OperationalMessage.MessageFrom(OperationalMessage.ELECT + "\n" + "id=1"); Assert.True(elect1.IsElect); Assert.Equal(Id.Of(1), elect1.Id); var expectedElec1 = new Elect(Id.Of(1)); Assert.Equal(expectedElec1, elect1); var elect100 = OperationalMessage.MessageFrom(OperationalMessage.ELECT + "\n" + "id=100"); Assert.True(elect100.IsElect); Assert.Equal(Id.Of(100), elect100.Id); var expectedElec100 = new Elect(Id.Of(100)); Assert.Equal(expectedElec100, elect100); }
public void TestParseVote() { var vote1 = OperationalMessage.MessageFrom(OperationalMessage.VOTE + "\n" + "id=1"); Assert.True(vote1.IsVote); Assert.Equal(Id.Of(1), vote1.Id); var expectedVote1 = new Vote(Id.Of(1)); Assert.Equal(expectedVote1, vote1); var vote100 = OperationalMessage.MessageFrom(OperationalMessage.VOTE + "\n" + "id=100"); Assert.True(vote100.IsVote); Assert.Equal(Id.Of(100), vote100.Id); var expectedVote100 = new Vote(Id.Of(100)); Assert.Equal(expectedVote100, vote100); }
//=================================== // LocalLiveNode //=================================== public void Handle(OperationalMessage message) { if (message.IsDirectory) { _state?.Handle((Directory)message); } else if (message.IsElect) { _state?.Handle((Elect)message); } else if (message.IsJoin) { _state?.Handle((Join)message); } else if (message.IsLeader) { _state?.Handle((Leader)message); } else if (message.IsLeave) { _state?.Handle((Leave)message); } else if (message.IsPing) { _state?.Handle((Ping)message); } else if (message.IsPulse) { _state?.Handle((Pulse)message); } else if (message.IsSplit) { _state?.Handle((Split)message); } else if (message.IsVote) { _state?.Handle((Vote)message); } else if (message.IsCheckHealth) { CheckHealth(); InformHealth(); } }
public void TestParseDirectory() { var dir = OperationalMessage.MessageFrom(MessageFixtures.DirectoryAsText(1, 2, 3)); Assert.True(dir.IsDirectory); Assert.Equal(Id.Of(MessageFixtures.DefaultNodeId), dir.Id); Assert.Equal(new Name(MessageFixtures.DefaultNodeName), ((Directory)dir).Name); int index = 1; foreach (var node in ((Directory)dir).Nodes) { Assert.Equal(Id.Of(index), node.Id); Assert.Equal(new Name("node" + index), node.Name); Assert.Equal(Address.From(MessageFixtures.OpAddresses[index], AddressType.Op), node.OperationalAddress); Assert.Equal(Address.From(MessageFixtures.AppAddresses[index], AddressType.App), node.ApplicationAddress); ++index; } var nodeEntries = new HashSet <Node>(); nodeEntries.Add(new Node( Id.Of(1), new Name("node1"), Address.From(MessageFixtures.OpAddresses[1], AddressType.Op), Address.From(MessageFixtures.AppAddresses[1], AddressType.App))); nodeEntries.Add(new Node( Id.Of(2), new Name("node2"), Address.From(MessageFixtures.OpAddresses[2], AddressType.Op), Address.From(MessageFixtures.AppAddresses[2], AddressType.App))); nodeEntries.Add(new Node( Id.Of(3), new Name("node3"), Address.From(MessageFixtures.OpAddresses[3], AddressType.Op), Address.From(MessageFixtures.AppAddresses[3], AddressType.App))); var expectedDir = new Directory(Id.Of(MessageFixtures.DefaultNodeId), new Name(MessageFixtures.DefaultNodeName), nodeEntries); Assert.Equal(expectedDir, dir); }
public void TestFromBytesWithLengthAndRequiredMessageLength() { var buffer = new MemoryStream(1000); var node1 = NextNodeWith(1); var join = new Join(node1); MessageConverters.MessageToBytes(join, buffer); buffer.Flip(); var messageSize = buffer.Length; var messageBytes = new byte[messageSize]; Array.Copy(buffer.ToArray(), 0, messageBytes, 0, messageSize); var message = new RawMessage(messageBytes); message.Header(RawMessageHeader.From(node1.Id.Value, 0, message.Length)); Assert.Equal(node1.Id.Value, message.Header().NodeId); Assert.Equal(message.Length, message.Header().Length); Assert.Equal(message.Length, message.RequiredMessageLength); Assert.Equal(join, OperationalMessage.MessageFrom(message.AsTextMessage())); }
public void TestCopyBytesTo() { var buffer = new MemoryStream(1000); var node1 = NextNodeWith(1); var join = new Join(node1); MessageConverters.MessageToBytes(join, buffer); buffer.Flip(); var messageSize = buffer.Length; var messageBytes = new byte[messageSize]; Array.Copy(buffer.ToArray(), 0, messageBytes, 0, messageSize); var message = new RawMessage(messageBytes); message.Header(RawMessageHeader.From(node1.Id.Value, 0, message.Length)); buffer.Clear(); message.CopyBytesTo(buffer); // copyBytesTo var text = buffer.ToArray().BytesToText(RawMessageHeader.Bytes, (int)message.Length); Assert.True(OperationalMessage.MessageFrom(text).IsJoin); }
public void TestTwoInboundMessages() { Join(); Leader(); _builder.PrepareContent().Sync(); Assert.True(_builder.IsCurrentMessageComplete()); Assert.True(_builder.HasContent); var inboundJoin = _builder.CurrentRawMessage(); var joinText = inboundJoin.AsTextMessage(); var joinFromText = OperationalMessage.MessageFrom(joinText); Assert.Equal(_join, joinFromText); _builder.PrepareForNextMessage(); Assert.True(_builder.HasContent); _builder.Sync(); var inboundLeader = _builder.CurrentRawMessage(); var leaderText = inboundLeader.AsTextMessage(); var leaderFromText = OperationalMessage.MessageFrom(leaderText); Assert.Equal(_leader, leaderFromText); }
//========================================= // InboundStreamInterest (operations and application) //========================================= public void HandleInboundStreamMessage(AddressType addressType, RawMessage message) { if (IsStopped) { return; } if (addressType.IsOperational) { var textMessage = message.AsTextMessage(); var typedMessage = OperationalMessage.MessageFrom(textMessage); if (typedMessage != null) { if (typedMessage.IsApp) { _attributesAgent.HandleInboundStreamMessage(addressType, message); } else { _localLiveNode.Handle(typedMessage); } } else { Logger.Warn($"ClusterSnapshot received invalid raw message '{textMessage}'"); } } else if (addressType.IsApplication) { _clusterApplication.HandleApplicationMessage(message); // TODO } else { Logger.Warn($"ClusterSnapshot couldn't dispatch incoming message; unknown address type: {addressType} for message: {message.AsTextMessage()}"); } }