Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests for ProtocolHandler Mailboxes (Neo 3.x) #809

Merged
merged 15 commits into from
Jun 12, 2019
Merged
37 changes: 37 additions & 0 deletions neo.UnitTests/UT_ConsensusServiceMailbox.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Akka.TestKit;
using Akka.TestKit.Xunit2;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using Moq;
using Neo.Ledger;
using Neo.Network.P2P.Payloads;
using Neo.Network.P2P;
using Akka.Configuration;
using Neo.Consensus;

namespace Neo.UnitTests
{
[TestClass]
public class UT_ConsensusServiceMailbox : TestKit
{
private static readonly Random TestRandom = new Random(1337); // use fixed seed for guaranteed determinism

ConsensusServiceMailbox uut;

[TestCleanup]
public void Cleanup()
{
Shutdown();
}

[TestInitialize]
public void TestSetup()
{
Akka.Actor.ActorSystem system = Sys;
var config = TestKit.DefaultConfig;
var akkaSettings = new Akka.Actor.Settings(system, config);
uut = new ConsensusServiceMailbox(akkaSettings, config);
}
}
}
194 changes: 194 additions & 0 deletions neo.UnitTests/UT_ProtocolHandlerMailbox.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
using Akka.TestKit;
using Akka.TestKit.Xunit2;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using Moq;
using Neo.Ledger;
using Neo.Network.P2P.Payloads;
using Neo.Network.P2P;
using Akka.Configuration;
using Neo.IO;
using System.Linq;
using System.Collections.Generic;

namespace Neo.UnitTests
{
[TestClass]
public class UT_ProtocolHandlerMailbox : TestKit
{
private static readonly Random TestRandom = new Random(1337); // use fixed seed for guaranteed determinism

ProtocolHandlerMailbox uut;

[TestCleanup]
public void Cleanup()
{
Shutdown();
}

[TestInitialize]
public void TestSetup()
{
Akka.Actor.ActorSystem system = Sys;
var config = TestKit.DefaultConfig;
var akkaSettings = new Akka.Actor.Settings(system, config);
uut = new ProtocolHandlerMailbox(akkaSettings, config);
}

[TestMethod]
public void ProtocolHandlerMailbox_Test_IsHighPriority()
{
ISerializable s = null;

//handshaking
uut.IsHighPriority(Message.Create(MessageCommand.Version, s)).Should().Be(true);
uut.IsHighPriority(Message.Create(MessageCommand.Verack, s)).Should().Be(true);

//connectivity
uut.IsHighPriority(Message.Create(MessageCommand.GetAddr, s)).Should().Be(false);
uut.IsHighPriority(Message.Create(MessageCommand.Addr, s)).Should().Be(false);
uut.IsHighPriority(Message.Create(MessageCommand.Ping, s)).Should().Be(false);
uut.IsHighPriority(Message.Create(MessageCommand.Pong, s)).Should().Be(false);

//synchronization
uut.IsHighPriority(Message.Create(MessageCommand.GetHeaders, s)).Should().Be(false);
uut.IsHighPriority(Message.Create(MessageCommand.Headers, s)).Should().Be(false);
uut.IsHighPriority(Message.Create(MessageCommand.GetBlocks, s)).Should().Be(false);
uut.IsHighPriority(Message.Create(MessageCommand.Mempool, s)).Should().Be(false);
uut.IsHighPriority(Message.Create(MessageCommand.Inv, s)).Should().Be(false);
uut.IsHighPriority(Message.Create(MessageCommand.GetData, s)).Should().Be(false);
uut.IsHighPriority(Message.Create(MessageCommand.NotFound, s)).Should().Be(false);
uut.IsHighPriority(Message.Create(MessageCommand.Transaction, s)).Should().Be(false);
uut.IsHighPriority(Message.Create(MessageCommand.Block, s)).Should().Be(false);
uut.IsHighPriority(Message.Create(MessageCommand.Consensus, s)).Should().Be(true);
uut.IsHighPriority(Message.Create(MessageCommand.Reject, s)).Should().Be(false);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here the tests @shargon! hahaha

Focused on ProtocolHandler this time... Coverage increased a lot already, and on future we cover others ;)


//SPV protocol
uut.IsHighPriority(Message.Create(MessageCommand.FilterLoad, s)).Should().Be(true);
uut.IsHighPriority(Message.Create(MessageCommand.FilterAdd, s)).Should().Be(true);
uut.IsHighPriority(Message.Create(MessageCommand.FilterClear, s)).Should().Be(true);
uut.IsHighPriority(Message.Create(MessageCommand.MerkleBlock, s)).Should().Be(false);

//others
uut.IsHighPriority(Message.Create(MessageCommand.Alert, s)).Should().Be(true);

// any random object (non Message) should not have priority
object obj = null;
uut.IsHighPriority(obj).Should().Be(false);
}


[TestMethod]
public void ProtocolHandlerMailbox_Test_ShallDrop()
{
// using this for messages
ISerializable s = null;
Message msg = null; // multiple uses
// empty queue
IEnumerable<object> emptyQueue = Enumerable.Empty<object>();

// any random object (non Message) should be dropped
object obj = null;
uut.ShallDrop(obj, emptyQueue).Should().Be(true);

//handshaking
// Version (no drop)
msg = Message.Create(MessageCommand.Version, s);
uut.ShallDrop(msg, emptyQueue).Should().Be(false);
uut.ShallDrop(msg, new object[]{ msg }).Should().Be(false);
// Verack (no drop)
msg = Message.Create(MessageCommand.Verack, s);
uut.ShallDrop(msg, emptyQueue).Should().Be(false);
uut.ShallDrop(msg, new object[]{ msg }).Should().Be(false);

//connectivity
// GetAddr (drop)
msg = Message.Create(MessageCommand.GetAddr, s);
uut.ShallDrop(msg, emptyQueue).Should().Be(false);
uut.ShallDrop(msg, new object[]{ msg }).Should().Be(true);
// Addr (no drop)
msg = Message.Create(MessageCommand.Addr, s);
uut.ShallDrop(msg, emptyQueue).Should().Be(false);
uut.ShallDrop(msg, new object[]{ msg }).Should().Be(false);
// Ping (no drop)
msg = Message.Create(MessageCommand.Ping, s);
uut.ShallDrop(msg, emptyQueue).Should().Be(false);
uut.ShallDrop(msg, new object[]{ msg }).Should().Be(false);
// Pong (no drop)
msg = Message.Create(MessageCommand.Pong, s);
uut.ShallDrop(msg, emptyQueue).Should().Be(false);
uut.ShallDrop(msg, new object[]{ msg }).Should().Be(false);

//synchronization
// GetHeaders (drop)
msg = Message.Create(MessageCommand.GetHeaders, s);
uut.ShallDrop(msg, emptyQueue).Should().Be(false);
uut.ShallDrop(msg, new object[]{ msg }).Should().Be(true);
// Headers (no drop)
msg = Message.Create(MessageCommand.Headers, s);
uut.ShallDrop(msg, emptyQueue).Should().Be(false);
uut.ShallDrop(msg, new object[]{ msg }).Should().Be(false);
// GetBlocks (drop)
msg = Message.Create(MessageCommand.GetBlocks, s);
uut.ShallDrop(msg, emptyQueue).Should().Be(false);
uut.ShallDrop(msg, new object[]{ msg }).Should().Be(true);
// Mempool (drop)
msg = Message.Create(MessageCommand.Mempool, s);
uut.ShallDrop(msg, emptyQueue).Should().Be(false);
uut.ShallDrop(msg, new object[]{ msg }).Should().Be(true);
// Inv (no drop)
msg = Message.Create(MessageCommand.Inv, s);
uut.ShallDrop(msg, emptyQueue).Should().Be(false);
uut.ShallDrop(msg, new object[]{ msg }).Should().Be(false);
// GetData (drop)
msg = Message.Create(MessageCommand.GetData, s);
uut.ShallDrop(msg, emptyQueue).Should().Be(false);
uut.ShallDrop(msg, new object[]{ msg }).Should().Be(true);
// NotFound (no drop)
msg = Message.Create(MessageCommand.NotFound, s);
uut.ShallDrop(msg, emptyQueue).Should().Be(false);
uut.ShallDrop(msg, new object[]{ msg }).Should().Be(false);
// Transaction (no drop)
msg = Message.Create(MessageCommand.Transaction, s);
uut.ShallDrop(msg, emptyQueue).Should().Be(false);
uut.ShallDrop(msg, new object[]{ msg }).Should().Be(false);
// Block (no drop)
msg = Message.Create(MessageCommand.Block, s);
uut.ShallDrop(msg, emptyQueue).Should().Be(false);
uut.ShallDrop(msg, new object[]{ msg }).Should().Be(false);
// Consensus (no drop)
msg = Message.Create(MessageCommand.Consensus, s);
uut.ShallDrop(msg, emptyQueue).Should().Be(false);
uut.ShallDrop(msg, new object[]{ msg }).Should().Be(false);
// Reject (no drop)
msg = Message.Create(MessageCommand.Reject, s);
uut.ShallDrop(msg, emptyQueue).Should().Be(false);
uut.ShallDrop(msg, new object[]{ msg }).Should().Be(false);

//SPV protocol
// FilterLoad (no drop)
msg = Message.Create(MessageCommand.FilterLoad, s);
uut.ShallDrop(msg, emptyQueue).Should().Be(false);
uut.ShallDrop(msg, new object[]{ msg }).Should().Be(false);
// FilterAdd (no drop)
msg = Message.Create(MessageCommand.FilterAdd, s);
uut.ShallDrop(msg, emptyQueue).Should().Be(false);
uut.ShallDrop(msg, new object[]{ msg }).Should().Be(false);
// FilterClear (no drop)
msg = Message.Create(MessageCommand.FilterClear, s);
uut.ShallDrop(msg, emptyQueue).Should().Be(false);
uut.ShallDrop(msg, new object[]{ msg }).Should().Be(false);
// MerkleBlock (no drop)
msg = Message.Create(MessageCommand.MerkleBlock, s);
uut.ShallDrop(msg, emptyQueue).Should().Be(false);
uut.ShallDrop(msg, new object[]{ msg }).Should().Be(false);

//others
// Alert (no drop)
msg = Message.Create(MessageCommand.Alert, s);
uut.ShallDrop(msg, emptyQueue).Should().Be(false);
uut.ShallDrop(msg, new object[]{ msg }).Should().Be(false);
}
}
}
36 changes: 36 additions & 0 deletions neo.UnitTests/UT_RemoteNodeMailbox.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Akka.TestKit;
using Akka.TestKit.Xunit2;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using Moq;
using Neo.Ledger;
using Neo.Network.P2P.Payloads;
using Neo.Network.P2P;
using Akka.Configuration;

namespace Neo.UnitTests
{
[TestClass]
public class UT_RemoteNodeMailbox : TestKit
{
private static readonly Random TestRandom = new Random(1337); // use fixed seed for guaranteed determinism

RemoteNodeMailbox uut;

[TestCleanup]
public void Cleanup()
{
Shutdown();
}

[TestInitialize]
public void TestSetup()
{
Akka.Actor.ActorSystem system = Sys;
var config = TestKit.DefaultConfig;
var akkaSettings = new Akka.Actor.Settings(system, config);
uut = new RemoteNodeMailbox(akkaSettings, config);
}
}
}
36 changes: 36 additions & 0 deletions neo.UnitTests/UT_TaskManagerMailbox.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Akka.TestKit;
using Akka.TestKit.Xunit2;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using Moq;
using Neo.Ledger;
using Neo.Network.P2P.Payloads;
using Neo.Network.P2P;
using Akka.Configuration;

namespace Neo.UnitTests
{
[TestClass]
public class UT_TaskManagerMailbox : TestKit
{
private static readonly Random TestRandom = new Random(1337); // use fixed seed for guaranteed determinism

TaskManagerMailbox uut;

[TestCleanup]
public void Cleanup()
{
Shutdown();
}

[TestInitialize]
public void TestSetup()
{
Akka.Actor.ActorSystem system = Sys;
var config = TestKit.DefaultConfig;
var akkaSettings = new Akka.Actor.Settings(system, config);
uut = new TaskManagerMailbox(akkaSettings, config);
}
}
}
2 changes: 1 addition & 1 deletion neo/Consensus/ConsensusService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ public ConsensusServiceMailbox(Akka.Actor.Settings settings, Config config)
{
}

protected override bool IsHighPriority(object message)
internal protected override bool IsHighPriority(object message)
{
switch (message)
{
Expand Down
4 changes: 2 additions & 2 deletions neo/IO/Actors/PriorityMailbox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public override IMessageQueue Create(IActorRef owner, ActorSystem system)
return new PriorityMessageQueue(ShallDrop, IsHighPriority);
}

protected virtual bool IsHighPriority(object message) => false;
protected virtual bool ShallDrop(object message, IEnumerable queue) => false;
internal protected virtual bool IsHighPriority(object message) => false;
internal protected virtual bool ShallDrop(object message, IEnumerable queue) => false;
}
}
2 changes: 1 addition & 1 deletion neo/Ledger/Blockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ public BlockchainMailbox(Akka.Actor.Settings settings, Config config)
{
}

protected override bool IsHighPriority(object message)
internal protected override bool IsHighPriority(object message)
{
switch (message)
{
Expand Down
4 changes: 2 additions & 2 deletions neo/Network/P2P/ProtocolHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ public ProtocolHandlerMailbox(Settings settings, Config config)
{
}

protected override bool IsHighPriority(object message)
internal protected override bool IsHighPriority(object message)
{
if (!(message is Message msg)) return false;
switch (msg.Command)
Expand All @@ -325,7 +325,7 @@ protected override bool IsHighPriority(object message)
}
}

protected override bool ShallDrop(object message, IEnumerable queue)
internal protected override bool ShallDrop(object message, IEnumerable queue)
{
if (!(message is Message msg)) return true;
switch (msg.Command)
Expand Down
2 changes: 1 addition & 1 deletion neo/Network/P2P/RemoteNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ internal class RemoteNodeMailbox : PriorityMailbox
{
public RemoteNodeMailbox(Settings settings, Config config) : base(settings, config) { }

protected override bool IsHighPriority(object message)
internal protected override bool IsHighPriority(object message)
{
switch (message)
{
Expand Down
2 changes: 1 addition & 1 deletion neo/Network/P2P/TaskManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ public TaskManagerMailbox(Akka.Actor.Settings settings, Config config)
{
}

protected override bool IsHighPriority(object message)
internal protected override bool IsHighPriority(object message)
{
switch (message)
{
Expand Down