Adding support for verifying serializability of Props.creator as well

This commit is contained in:
Viktor Klang 2011-12-27 17:30:05 +01:00
parent f6f56889f8
commit 9701685897
5 changed files with 71 additions and 3 deletions

View file

@ -10,6 +10,9 @@ import akka.testkit.AkkaSpec
import com.typesafe.config.ConfigFactory
import akka.actor._
import java.io._
import akka.dispatch.Await
import akka.util.Timeout
import akka.util.duration._
object SerializeSpec {
@ -129,3 +132,59 @@ class SerializeSpec extends AkkaSpec(SerializeSpec.serializationConf) {
}
}
}
object VerifySerializabilitySpec {
val conf = ConfigFactory.parseString("""
akka {
actor {
serialize-messages = on
serialize-creators = on
serializers {
java = "akka.serialization.JavaSerializer"
proto = "akka.testing.ProtobufSerializer"
sjson = "akka.testing.SJSONSerializer"
default = "akka.serialization.JavaSerializer"
}
serialization-bindings {
java = ["akka.serialization.SerializeSpec$Address", "akka.serialization.MyJavaSerializableActor", "akka.serialization.MyStatelessActorWithMessagesInMailbox", "akka.serialization.MyActorWithProtobufMessagesInMailbox"]
sjson = ["akka.serialization.SerializeSpec$Person"]
proto = ["com.google.protobuf.Message", "akka.actor.ProtobufProtocol$MyMessage"]
}
}
}
""")
class FooActor extends Actor {
def receive = {
case s: String sender ! s
}
}
class NonSerializableActor(system: ActorSystem) extends Actor {
def receive = {
case s: String sender ! s
}
}
}
class VerifySerializabilitySpec extends AkkaSpec(VerifySerializabilitySpec.conf) {
import VerifySerializabilitySpec._
"verify creators and messages" in {
implicit val timeout = Timeout(5 seconds)
system.settings.SerializeAllCreators must be(true)
system.settings.SerializeAllMessages must be(true)
val a = system.actorOf(Props[FooActor])
Await.result(a ? "pigdog", timeout.duration) must be("pigdog")
intercept[NotSerializableException] {
Await.result(a ? new AnyRef, timeout.duration)
}
intercept[java.io.NotSerializableException] {
val b = system.actorOf(Props(new NonSerializableActor(system)))
}
}
}

View file

@ -25,8 +25,17 @@ import java.util.regex.Pattern
*/
trait AutoReceivedMessage extends Serializable
/**
* Marker trait to indicate that a message might be potentially harmful,
* this is used to block messages coming in over remoting.
*/
trait PossiblyHarmful
/**
* Marker trait to signal that this class should not be verified for serializability.
*/
trait NoSerializationVerificationNeeded
case class Failed(cause: Throwable) extends AutoReceivedMessage with PossiblyHarmful
case object PoisonPill extends AutoReceivedMessage with PossiblyHarmful

View file

@ -215,7 +215,7 @@ private[akka] class ActorCell(
final var childrenRefs: TreeMap[String, ChildRestartStats] = emptyChildrenRefs
private def _actorOf(props: Props, name: String): ActorRef = {
if (system.settings.SerializeAllCreators) {
if (system.settings.SerializeAllCreators && !props.creator.isInstanceOf[NoSerializationVerificationNeeded]) {
val ser = SerializationExtension(system)
ser.serialize(props.creator) match {
case Left(t) throw t

View file

@ -19,7 +19,7 @@ final case class Envelope(val message: Any, val sender: ActorRef)(system: ActorS
if (message.isInstanceOf[AnyRef]) {
val msg = message.asInstanceOf[AnyRef]
if (msg eq null) throw new InvalidMessageException("Message is null")
if (system.settings.SerializeAllMessages) {
if (system.settings.SerializeAllMessages && !msg.isInstanceOf[NoSerializationVerificationNeeded]) {
val ser = SerializationExtension(system)
ser.serialize(msg) match { //Verify serializability
case Left(t) throw t

View file

@ -363,7 +363,7 @@ object Logging {
* message. This is necessary to ensure that additional subscriptions are in
* effect when the logging system finished starting.
*/
case class InitializeLogger(bus: LoggingBus)
case class InitializeLogger(bus: LoggingBus) extends NoSerializationVerificationNeeded
/**
* Response message each logger must send within 1 second after receiving the