pekko/akka-remote/src/test/scala/akka/serialization/ActorSerializeSpec.scala

154 lines
5.5 KiB
Scala
Raw Normal View History

package akka.serialization
import org.scalatest.WordSpec
import org.scalatest.matchers.ShouldMatchers
import org.scalatest.BeforeAndAfterAll
import org.scalatest.junit.JUnitRunner
import org.junit.runner.RunWith
import com.google.protobuf.Message
import akka.serialization.ActorSerialization._
import akka.actor._
import Actor._
import SerializeSpec._
case class MyMessage(id: Long, name: String, status: Boolean)
@RunWith(classOf[JUnitRunner])
class ActorSerializeSpec extends WordSpec with ShouldMatchers with BeforeAndAfterAll {
"Serializable actor" should {
"should be able to serialize and de-serialize a stateful actor with a given serializer" ignore {
2011-09-09 17:05:39 +02:00
val actor1 = new LocalActorRef(Props[MyJavaSerializableActor], newUuid.toString, systemService = true)
(actor1 ? "hello").get should equal("world 1")
(actor1 ? "hello").get should equal("world 2")
val bytes = toBinary(actor1)
2011-09-09 17:05:39 +02:00
val actor2 = fromBinary(bytes).asInstanceOf[LocalActorRef]
(actor2 ? "hello").get should equal("world 3")
actor2.underlying.receiveTimeout should equal(Some(1000))
actor1.stop()
actor2.stop()
}
"should be able to serialize and deserialize a MyStatelessActorWithMessagesInMailbox" ignore {
2011-09-09 17:05:39 +02:00
val actor1 = new LocalActorRef(Props[MyStatelessActorWithMessagesInMailbox], newUuid.toString, systemService = true)
for (i 1 to 10) actor1 ! "hello"
actor1.underlying.dispatcher.mailboxSize(actor1.underlying) should be > (0)
val actor2 = fromBinary(toBinary(actor1)).asInstanceOf[LocalActorRef]
Thread.sleep(1000)
actor2.underlying.dispatcher.mailboxSize(actor1.underlying) should be > (0)
(actor2 ? "hello-reply").get should equal("world")
val actor3 = fromBinary(toBinary(actor1, false)).asInstanceOf[LocalActorRef]
Thread.sleep(1000)
actor3.underlying.dispatcher.mailboxSize(actor1.underlying) should equal(0)
(actor3 ? "hello-reply").get should equal("world")
}
"should be able to serialize and deserialize a PersonActorWithMessagesInMailbox" ignore {
val p1 = Person("debasish ghosh", 25, SerializeSpec.Address("120", "Monroe Street", "Santa Clara", "95050"))
2011-09-09 17:05:39 +02:00
val actor1 = new LocalActorRef(Props[PersonActorWithMessagesInMailbox], newUuid.toString, systemService = true)
(actor1 ! p1)
(actor1 ! p1)
(actor1 ! p1)
(actor1 ! p1)
(actor1 ! p1)
(actor1 ! p1)
(actor1 ! p1)
(actor1 ! p1)
(actor1 ! p1)
(actor1 ! p1)
actor1.underlying.dispatcher.mailboxSize(actor1.underlying) should be > (0)
val actor2 = fromBinary(toBinary(actor1)).asInstanceOf[LocalActorRef]
Thread.sleep(1000)
actor2.underlying.dispatcher.mailboxSize(actor1.underlying) should be > (0)
(actor2 ? "hello-reply").get should equal("hello")
val actor3 = fromBinary(toBinary(actor1, false)).asInstanceOf[LocalActorRef]
Thread.sleep(1000)
actor3.underlying.dispatcher.mailboxSize(actor1.underlying) should equal(0)
(actor3 ? "hello-reply").get should equal("hello")
}
}
"serialize protobuf" should {
"should serialize" ignore {
val msg = MyMessage(123, "debasish ghosh", true)
import akka.serialization.Serialization._
val b = serialize(ProtobufProtocol.MyMessage.newBuilder.setId(msg.id).setName(msg.name).setStatus(msg.status).build) match {
case Left(exception) fail(exception)
case Right(bytes) bytes
}
val in = deserialize(b, classOf[ProtobufProtocol.MyMessage], None) match {
case Left(exception) fail(exception)
case Right(i) i
}
val m = in.asInstanceOf[ProtobufProtocol.MyMessage]
MyMessage(m.getId, m.getName, m.getStatus) should equal(msg)
}
}
"serialize actor that accepts protobuf message" ignore {
"should serialize" ignore {
2011-09-09 17:05:39 +02:00
val actor1 = new LocalActorRef(Props[MyActorWithProtobufMessagesInMailbox], newUuid.toString, systemService = true)
val msg = MyMessage(123, "debasish ghosh", true)
val b = ProtobufProtocol.MyMessage.newBuilder.setId(msg.id).setName(msg.name).setStatus(msg.status).build
for (i 1 to 10) actor1 ! b
actor1.underlying.dispatcher.mailboxSize(actor1.underlying) should be > (0)
val actor2 = fromBinary(toBinary(actor1)).asInstanceOf[LocalActorRef]
Thread.sleep(1000)
actor2.underlying.dispatcher.mailboxSize(actor1.underlying) should be > (0)
(actor2 ? "hello-reply").get should equal("world")
val actor3 = fromBinary(toBinary(actor1, false)).asInstanceOf[LocalActorRef]
Thread.sleep(1000)
actor3.underlying.dispatcher.mailboxSize(actor1.underlying) should equal(0)
(actor3 ? "hello-reply").get should equal("world")
}
}
}
class MyJavaSerializableActor extends Actor with scala.Serializable {
var count = 0
receiveTimeout = Some(1000)
def receive = {
case "hello"
count = count + 1
reply("world " + count)
}
}
class MyStatelessActorWithMessagesInMailbox extends Actor with scala.Serializable {
def receive = {
case "hello"
Thread.sleep(500)
case "hello-reply" reply("world")
}
}
class MyActorWithProtobufMessagesInMailbox extends Actor with scala.Serializable {
def receive = {
case m: Message
Thread.sleep(500)
case "hello-reply" reply("world")
}
}
class PersonActorWithMessagesInMailbox extends Actor with scala.Serializable {
def receive = {
case p: Person
Thread.sleep(500)
case "hello-reply" reply("hello")
}
}