Created
May 21, 2017 12:51
-
-
Save maurelio1234/724df2a39058b12730fc63d49b681a32 to your computer and use it in GitHub Desktop.
UnboundedPriorityMailbox doesn't keep message reception order
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Akka.Actor; | |
using Akka.Configuration; | |
using Akka.Dispatch; | |
using Akka.Util; | |
using NUnit.Framework; | |
using UntypedActor = Akka.Actor.UntypedActor; | |
namespace TestAkkaPriority | |
{ | |
/* | |
App.config: | |
<akka> | |
<hocon> | |
<![CDATA[ | |
my-unbounded-priority-mailbox { | |
mailbox-type : "TestAkkaPriority.PriorityMailbox, TestAkkaPriority" | |
} | |
]]> | |
</hocon> | |
</akka> | |
*/ | |
[TestFixture] | |
class PriorityUnboundedMailboxTest | |
{ | |
/* | |
Prints: | |
Received message: 0-m0 | |
Received message: 0-m3 | |
Received message: 0-m2 | |
Received message: 0-m1 | |
Received message: 1-m0 | |
Expected: | |
Received message: 0-m0 | |
Received message: 0-m1 | |
Received message: 0-m2 | |
Received message: 0-m3 | |
Received message: 1-m0 | |
*/ | |
[Test] | |
public void MailboxesDequeingOrder() | |
{ | |
using (var system = ActorSystem.Create("Mysystem")) | |
{ | |
var actor = system.ActorOf(Props.Create<SimpleActor>().WithMailbox("my-unbounded-priority-mailbox")); | |
actor.Tell("0-m0"); | |
actor.Tell("1-m0"); | |
actor.Tell("0-m1"); | |
actor.Tell("0-m2"); | |
actor.Tell("0-m3"); | |
} | |
} | |
/* | |
Prints: | |
Dequeued 0-m0 | |
Dequeued 0-m3 | |
Dequeued 0-m2 | |
Dequeued 0-m1 | |
Dequeued 1-m0 | |
*/ | |
[Test] | |
public void PriorityQueueOrdering() | |
{ | |
var queue = new ListPriorityQueue(10); | |
queue.SetPriorityCalculator(o => int.Parse((o as string).Split('-')[0])); | |
queue.Enqueue(GetEnvelope("0-m0")); | |
queue.Enqueue(GetEnvelope("1-m0")); | |
queue.Enqueue(GetEnvelope("0-m1")); | |
queue.Enqueue(GetEnvelope("0-m2")); | |
queue.Enqueue(GetEnvelope("0-m3")); | |
while (queue.Count() > 0) | |
{ | |
Console.WriteLine("Dequeued {0}", queue.Dequeue().Message); | |
} | |
} | |
private Envelope GetEnvelope(string message) | |
{ | |
return new Envelope(message, ActorRefs.NoSender); | |
} | |
} | |
class SimpleActor : UntypedActor | |
{ | |
protected override void OnReceive(object message) | |
{ | |
Console.WriteLine("Received message: {0}", message); | |
} | |
} | |
public class PriorityMailbox : UnboundedPriorityMailbox | |
{ | |
public PriorityMailbox(Settings settings, Config config) : base(settings, config) | |
{ | |
} | |
protected override int PriorityGenerator(object message) | |
{ | |
return int.Parse((message as string).Split('-')[0]); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment