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

Test for Akka Mailboxes #806

Merged
merged 4 commits into from Jun 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 42 additions & 0 deletions neo.UnitTests/UT_ConsensusServiceMailbox.cs
@@ -0,0 +1,42 @@
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);
}

[TestMethod]
public void ConsensusServiceMailbox_Test_IsHighPriority()
{
}
}
}
81 changes: 81 additions & 0 deletions neo.UnitTests/UT_ProtocolHandlerMailbox.cs
@@ -0,0 +1,81 @@
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;
// test priority commands
uut.IsHighPriority(Message.Create("consensus", s)).Should().Be(true);
uut.IsHighPriority(Message.Create("filteradd", s)).Should().Be(true);
uut.IsHighPriority(Message.Create("filterclear", s)).Should().Be(true);
uut.IsHighPriority(Message.Create("verack", s)).Should().Be(true);
uut.IsHighPriority(Message.Create("version", s)).Should().Be(true);
uut.IsHighPriority(Message.Create("alert", s)).Should().Be(true);
// any random command should not have priority
uut.IsHighPriority(Message.Create("_", s)).Should().Be(false);
// 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;
// empty queue
IEnumerable<object> queue = Enumerable.Empty<object>();

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

// test drop for specific commands (empty queue)
uut.ShallDrop(Message.Create("getaddr", s), queue).Should().Be(false);
uut.ShallDrop(Message.Create("getblocks", s), queue).Should().Be(false);
uut.ShallDrop(Message.Create("getdata", s), queue).Should().Be(false);
uut.ShallDrop(Message.Create("getheaders", s), queue).Should().Be(false);
uut.ShallDrop(Message.Create("version", s), queue).Should().Be(false);
uut.ShallDrop(Message.Create("mempool", s), queue).Should().Be(false);
// any random command should not be dropped
uut.ShallDrop(Message.Create("_", s), queue).Should().Be(false);
}
}
}
41 changes: 41 additions & 0 deletions neo.UnitTests/UT_RemoteNodeMailbox.cs
@@ -0,0 +1,41 @@
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);
}

[TestMethod]
public void Test_IsHighPriority()
{
}
}
}
41 changes: 41 additions & 0 deletions neo.UnitTests/UT_TaskManagerMailbox.cs
@@ -0,0 +1,41 @@
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);
}

[TestMethod]
public void Test_IsHighPriority()
{
}
}
}
2 changes: 1 addition & 1 deletion neo/Consensus/ConsensusService.cs
Expand Up @@ -647,7 +647,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
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
Expand Up @@ -737,7 +737,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
Expand Up @@ -312,7 +312,7 @@ public ProtocolHandlerMailbox(Akka.Actor.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 @@ -330,7 +330,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
Expand Up @@ -240,7 +240,7 @@ public RemoteNodeMailbox(Akka.Actor.Settings settings, Config 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
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