2016-04-28 23:33:59 +10:00
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2009-2016 Lightbend Inc. <http://www.lightbend.com>
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package akka.remote.serialization
|
|
|
|
|
|
|
|
|
|
import akka.actor._
|
|
|
|
|
import akka.remote.MessageSerializer
|
|
|
|
|
import akka.serialization.SerializationExtension
|
|
|
|
|
import akka.testkit.AkkaSpec
|
|
|
|
|
import com.typesafe.config.ConfigFactory
|
2016-09-28 11:14:33 +02:00
|
|
|
import scala.util.control.NoStackTrace
|
2016-04-28 23:33:59 +10:00
|
|
|
|
|
|
|
|
object MiscMessageSerializerSpec {
|
|
|
|
|
val serializationTestOverrides =
|
2016-06-19 17:55:10 +02:00
|
|
|
"""
|
2016-09-09 09:01:15 +02:00
|
|
|
akka.actor.enable-additional-serialization-bindings=on
|
|
|
|
|
# or they can be enabled with
|
|
|
|
|
# akka.remote.artery.enabled=on
|
2016-09-28 11:14:33 +02:00
|
|
|
|
|
|
|
|
akka.actor.serialization-bindings {
|
|
|
|
|
"akka.remote.serialization.MiscMessageSerializerSpec$TestException" = akka-misc
|
|
|
|
|
}
|
2016-09-09 09:01:15 +02:00
|
|
|
"""
|
2016-04-28 23:33:59 +10:00
|
|
|
|
|
|
|
|
val testConfig = ConfigFactory.parseString(serializationTestOverrides).withFallback(AkkaSpec.testConf)
|
2016-09-28 11:14:33 +02:00
|
|
|
|
|
|
|
|
class TestException(msg: String, cause: Throwable) extends RuntimeException(msg, cause) {
|
|
|
|
|
def this(msg: String) = this(msg, null)
|
|
|
|
|
|
|
|
|
|
override def equals(other: Any): Boolean = other match {
|
|
|
|
|
case e: TestException ⇒
|
|
|
|
|
e.getMessage == getMessage && e.stackTrace == stackTrace && e.getCause == getCause
|
|
|
|
|
case _ ⇒ false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def stackTrace: List[StackTraceElement] =
|
|
|
|
|
if (getStackTrace == null) Nil
|
|
|
|
|
else getStackTrace.toList
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class TestExceptionNoStack(msg: String) extends TestException(msg) with NoStackTrace {
|
|
|
|
|
override def equals(other: Any): Boolean = other match {
|
|
|
|
|
case e: TestExceptionNoStack ⇒
|
|
|
|
|
e.getMessage == getMessage && e.stackTrace == stackTrace
|
|
|
|
|
case _ ⇒ false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class OtherException(msg: String) extends IllegalArgumentException(msg) {
|
|
|
|
|
override def equals(other: Any): Boolean = other match {
|
|
|
|
|
case e: OtherException ⇒ e.getMessage == getMessage
|
|
|
|
|
case _ ⇒ false
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-04-28 23:33:59 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class MiscMessageSerializerSpec extends AkkaSpec(MiscMessageSerializerSpec.testConfig) {
|
2016-09-28 11:14:33 +02:00
|
|
|
import MiscMessageSerializerSpec._
|
2016-04-28 23:33:59 +10:00
|
|
|
|
|
|
|
|
"MiscMessageSerializer" must {
|
|
|
|
|
Seq(
|
2016-06-02 14:06:57 +02:00
|
|
|
"Identify" → Identify("some-message"),
|
2016-06-19 17:55:10 +02:00
|
|
|
"Identify with None" → Identify(None),
|
|
|
|
|
"Identify with Some" → Identify(Some("value")),
|
|
|
|
|
"ActorIdentity without actor ref" → ActorIdentity("some-message", ref = None),
|
|
|
|
|
"ActorIdentity with actor ref" → ActorIdentity("some-message", ref = Some(testActor)),
|
2016-09-28 11:14:33 +02:00
|
|
|
"TestException" → new TestException("err"),
|
|
|
|
|
"TestExceptionNoStack" → new TestExceptionNoStack("err2"),
|
|
|
|
|
"TestException with cause" → new TestException("err3", new TestException("cause")),
|
|
|
|
|
"Status.Success" → Status.Success("value"),
|
|
|
|
|
"Status.Failure" → Status.Failure(new TestException("err")),
|
|
|
|
|
"Status.Failure JavaSer" → Status.Failure(new OtherException("exc")), // exc with JavaSerializer
|
2016-06-19 17:55:10 +02:00
|
|
|
"Some" → Some("value"),
|
|
|
|
|
"None" → None).foreach {
|
2016-04-28 23:33:59 +10:00
|
|
|
case (scenario, item) ⇒
|
|
|
|
|
s"resolve serializer for $scenario" in {
|
|
|
|
|
val serializer = SerializationExtension(system)
|
|
|
|
|
serializer.serializerFor(item.getClass).getClass should ===(classOf[MiscMessageSerializer])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
s"serialize and de-serialize $scenario" in {
|
|
|
|
|
verifySerialization(item)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"reject invalid manifest" in {
|
|
|
|
|
intercept[IllegalArgumentException] {
|
|
|
|
|
val serializer = new MiscMessageSerializer(system.asInstanceOf[ExtendedActorSystem])
|
|
|
|
|
serializer.manifest("INVALID")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"reject deserialization with invalid manifest" in {
|
|
|
|
|
intercept[IllegalArgumentException] {
|
|
|
|
|
val serializer = new MiscMessageSerializer(system.asInstanceOf[ExtendedActorSystem])
|
|
|
|
|
serializer.fromBinary(Array.empty[Byte], "INVALID")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def verifySerialization(msg: AnyRef): Unit = {
|
|
|
|
|
val serializer = new MiscMessageSerializer(system.asInstanceOf[ExtendedActorSystem])
|
|
|
|
|
serializer.fromBinary(serializer.toBinary(msg), serializer.manifest(msg)) should ===(msg)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|