From 673bb2b8bbb404aee46c5e9f580460b685754775 Mon Sep 17 00:00:00 2001 From: Jan Van Besien Date: Fri, 19 Mar 2010 13:49:49 +0100 Subject: [PATCH] Don't allow two different actors (different types) to share the same work stealing dispatcher. Added unit test. --- ...sedEventDrivenWorkStealingDispatcher.scala | 7 +++- ...ventDrivenWorkStealingDispatcherTest.scala | 38 +++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/akka-core/src/main/scala/dispatch/ExecutorBasedEventDrivenWorkStealingDispatcher.scala b/akka-core/src/main/scala/dispatch/ExecutorBasedEventDrivenWorkStealingDispatcher.scala index 24dc3acaed..0f12d16394 100644 --- a/akka-core/src/main/scala/dispatch/ExecutorBasedEventDrivenWorkStealingDispatcher.scala +++ b/akka-core/src/main/scala/dispatch/ExecutorBasedEventDrivenWorkStealingDispatcher.scala @@ -161,6 +161,7 @@ class ExecutorBasedEventDrivenWorkStealingDispatcher(_name: String) extends Mess private[akka] def init = withNewThreadPoolWithLinkedBlockingQueueWithUnboundedCapacity.buildThreadPool override def register(actor: Actor) = { + verifyActorsAreOfSameType(actor) super.register(actor) } @@ -170,8 +171,10 @@ class ExecutorBasedEventDrivenWorkStealingDispatcher(_name: String) extends Mess actorType = Some(newActor.getClass) } case Some(aType) => { - if (aType != newActor.getClass) // TODO: is assignable from ?! - throw new IllegalStateException(String.format("Can't register actor %s in a work stealing dispatcher which already knows actors of type %s", newActor, aType)) + if (aType != newActor.getClass) + throw new IllegalStateException( + String.format("Can't register actor %s in a work stealing dispatcher which already knows actors of type %s", + newActor, aType)) } } diff --git a/akka-core/src/test/scala/ExecutorBasedEventDrivenWorkStealingDispatcherTest.scala b/akka-core/src/test/scala/ExecutorBasedEventDrivenWorkStealingDispatcherTest.scala index a7299dcc1a..ecc911734a 100644 --- a/akka-core/src/test/scala/ExecutorBasedEventDrivenWorkStealingDispatcherTest.scala +++ b/akka-core/src/test/scala/ExecutorBasedEventDrivenWorkStealingDispatcherTest.scala @@ -58,4 +58,42 @@ class ExecutorBasedEventDrivenWorkStealingDispatcherTest extends JUnitSuite with } }) + @Test def canNotUseActorsOfDifferentTypesInSameDispatcher:Unit = { + val first = new FirstActor + val second = new SecondActor + + first.start + intercept[IllegalStateException] { + second.start + } + } + + class FirstActor extends Actor { + messageDispatcher = poolDispatcher + def receive = {case _ => {}} + } + + class SecondActor extends Actor { + messageDispatcher = poolDispatcher + def receive = {case _ => {}} + } + + @Test def canNotUseActorsOfDifferentSubTypesInSameDispatcher:Unit = { + val parent = new ParentActor + val child = new ChildActor + + parent.start + intercept[IllegalStateException] { + child.start + } + } + + class ParentActor extends Actor { + messageDispatcher = poolDispatcher + def receive = {case _ => {}} + } + + class ChildActor extends ParentActor { + } + }