Removing UnhandledMessageException and fixing tests

This commit is contained in:
Viktor Klang 2011-12-20 11:19:06 +01:00
parent 9a9e800b9f
commit 49b3bac69c
8 changed files with 12 additions and 55 deletions

View file

@ -11,15 +11,7 @@ import akka.util.Duration
import com.typesafe.config.ConfigFactory import com.typesafe.config.ConfigFactory
import scala.collection.JavaConverters._ import scala.collection.JavaConverters._
import java.util.Properties import java.util.Properties
import akka.actor.Actor import akka.actor._
import akka.actor.ActorSystem
import akka.actor.UnhandledMessageException
import akka.actor.PoisonPill
import akka.actor.ActorSystemImpl
import akka.actor.Props
import akka.actor.OneForOneStrategy
import akka.actor.ActorKilledException
import akka.actor.Kill
object LoggingReceiveSpec { object LoggingReceiveSpec {
class TestLogActor extends Actor { class TestLogActor extends Actor {
@ -75,7 +67,7 @@ class LoggingReceiveSpec extends WordSpec with BeforeAndAfterEach with BeforeAnd
new TestKit(appLogging) with ImplicitSender { new TestKit(appLogging) with ImplicitSender {
ignoreMute(this) ignoreMute(this)
system.eventStream.subscribe(testActor, classOf[Logging.Debug]) system.eventStream.subscribe(testActor, classOf[Logging.Debug])
system.eventStream.subscribe(testActor, classOf[Logging.Error]) system.eventStream.subscribe(testActor, classOf[UnhandledMessage])
val r: Actor.Receive = { val r: Actor.Receive = {
case null case null
@ -97,12 +89,11 @@ class LoggingReceiveSpec extends WordSpec with BeforeAndAfterEach with BeforeAnd
actor ! "becomenull" actor ! "becomenull"
EventFilter[UnhandledMessageException](pattern = "does not handle", occurrences = 1) intercept { within(500 millis) {
within(500 millis) { actor ! "bah"
actor ! "bah" val deadletters = system.deadLetters
expectMsgPF() { expectMsgPF() {
case Logging.Error(_: UnhandledMessageException, `name`, _) true case UnhandledMessage("bah", testActor, `actor`) true
}
} }
} }
} }

View file

@ -87,21 +87,6 @@ case class ActorInterruptedException private[akka] (cause: Throwable)
extends AkkaException(cause.getMessage, cause) extends AkkaException(cause.getMessage, cause)
with NoStackTrace with NoStackTrace
/**
* This message is thrown by default when an Actors behavior doesn't match a message
*/
case class UnhandledMessageException(msg: Any, ref: ActorRef = null) extends RuntimeException {
def this(msg: String) = this(msg, null)
// constructor with 'null' ActorRef needed to work with client instantiation of remote exception
override def getMessage =
if (ref ne null) "Actor [%s] does not handle [%s]".format(ref, msg)
else "Actor does not handle [%s]".format(msg)
override def fillInStackTrace() = this //Don't waste cycles generating stack trace
}
/** /**
* This message is published to the EventStream whenever an Actor receives a message it doesn't understand * This message is published to the EventStream whenever an Actor receives a message it doesn't understand
*/ */

View file

@ -124,16 +124,6 @@ abstract class UntypedActor extends Actor {
*/ */
override def postRestart(reason: Throwable): Unit = super.postRestart(reason) override def postRestart(reason: Throwable): Unit = super.postRestart(reason)
/**
* User overridable callback.
* <p/>
* Is called when a message isn't handled by the current behavior of the actor
* by default it throws an UnhandledMessageException
*/
override def unhandled(msg: Any) {
throw new UnhandledMessageException(msg, self)
}
final protected def receive = { final protected def receive = {
case msg onReceive(msg) case msg onReceive(msg)
} }

View file

@ -6,7 +6,6 @@ package akka.docs.actor;
//#receive-timeout //#receive-timeout
import akka.actor.Actors; import akka.actor.Actors;
import akka.actor.ReceiveTimeout; import akka.actor.ReceiveTimeout;
import akka.actor.UnhandledMessageException;
import akka.actor.UntypedActor; import akka.actor.UntypedActor;
import akka.util.Duration; import akka.util.Duration;

View file

@ -5,7 +5,6 @@ package akka.docs.actor;
//#my-untyped-actor //#my-untyped-actor
import akka.actor.UntypedActor; import akka.actor.UntypedActor;
import akka.actor.UnhandledMessageException;
import akka.event.Logging; import akka.event.Logging;
import akka.event.LoggingAdapter; import akka.event.LoggingAdapter;

View file

@ -7,7 +7,6 @@ import static akka.docs.actor.UntypedActorSwapper.Swap.SWAP;
import akka.actor.ActorRef; import akka.actor.ActorRef;
import akka.actor.Props; import akka.actor.Props;
import akka.actor.ActorSystem; import akka.actor.ActorSystem;
import akka.actor.UnhandledMessageException;
import akka.actor.UntypedActor; import akka.actor.UntypedActor;
import akka.event.Logging; import akka.event.Logging;
import akka.event.LoggingAdapter; import akka.event.LoggingAdapter;

View file

@ -47,7 +47,7 @@ Please note that the Akka Actor ``receive`` message loop is exhaustive, which is
different compared to Erlang and Scala Actors. This means that you need to different compared to Erlang and Scala Actors. This means that you need to
provide a pattern match for all messages that it can accept and if you want to provide a pattern match for all messages that it can accept and if you want to
be able to handle unknown messages then you need to have a default case as in be able to handle unknown messages then you need to have a default case as in
the example above. Otherwise an ``akka.actor.UnhandledMessage(message, actor)`` will be the example above. Otherwise an ``akka.actor.UnhandledMessage(message, sender, recipient)`` will be
published to the ``ActorSystem``'s ``EventStream``. published to the ``ActorSystem``'s ``EventStream``.
Creating Actors with default constructor Creating Actors with default constructor

View file

@ -5,20 +5,14 @@ package akka.docs.testkit
//#imports-test-probe //#imports-test-probe
import akka.testkit.TestProbe import akka.testkit.TestProbe
import akka.actor.Actor
import akka.actor.ActorRef
import akka.actor.Props
import akka.util.duration._ import akka.util.duration._
import akka.actor._
//#imports-test-probe //#imports-test-probe
import akka.testkit.AkkaSpec import akka.testkit.AkkaSpec
import akka.actor.Actor
import akka.testkit.DefaultTimeout import akka.testkit.DefaultTimeout
import akka.testkit.ImplicitSender import akka.testkit.ImplicitSender
import akka.actor.ActorRef
import akka.actor.Props
object TestkitDocSpec { object TestkitDocSpec {
case object Say42 case object Say42
case object Unknown case object Unknown
@ -136,10 +130,10 @@ class TestkitDocSpec extends AkkaSpec with DefaultTimeout with ImplicitSender {
"demonstrate unhandled message" in { "demonstrate unhandled message" in {
//#test-unhandled //#test-unhandled
import akka.testkit.TestActorRef import akka.testkit.TestActorRef
import akka.actor.UnhandledMessageException system.eventStream.subscribe(testActor, classOf[UnhandledMessage])
val ref = TestActorRef[MyActor] val ref = TestActorRef[MyActor]
intercept[UnhandledMessageException] { ref(Unknown) } ref(Unknown)
expectMsg(1 second, UnhandledMessage(Unknown, system.deadLetters, ref))
//#test-unhandled //#test-unhandled
} }