2011-09-09 13:46:36 +02:00
|
|
|
package akka.serialization
|
|
|
|
|
|
2011-09-16 15:28:43 +02:00
|
|
|
import org.scalatest.WordSpec
|
2011-09-09 13:46:36 +02:00
|
|
|
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])
|
2011-09-16 15:28:43 +02:00
|
|
|
class ActorSerializeSpec extends WordSpec with ShouldMatchers with BeforeAndAfterAll {
|
2011-09-09 13:46:36 +02:00
|
|
|
|
2011-09-16 15:28:43 +02:00
|
|
|
"Serializable actor" should {
|
|
|
|
|
"should be able to serialize and de-serialize a stateful actor with a given serializer" ignore {
|
2011-09-09 13:46:36 +02:00
|
|
|
|
2011-09-09 17:05:39 +02:00
|
|
|
val actor1 = new LocalActorRef(Props[MyJavaSerializableActor], newUuid.toString, systemService = true)
|
2011-09-16 15:28:43 +02:00
|
|
|
|
2011-09-09 13:46:36 +02:00
|
|
|
(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]
|
2011-09-09 13:46:36 +02:00
|
|
|
(actor2 ? "hello").get should equal("world 3")
|
|
|
|
|
|
|
|
|
|
actor2.receiveTimeout should equal(Some(1000))
|
|
|
|
|
actor1.stop()
|
|
|
|
|
actor2.stop()
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-16 15:28:43 +02:00
|
|
|
"should be able to serialize and deserialize a MyStatelessActorWithMessagesInMailbox" ignore {
|
2011-09-09 13:46:36 +02:00
|
|
|
|
2011-09-09 17:05:39 +02:00
|
|
|
val actor1 = new LocalActorRef(Props[MyStatelessActorWithMessagesInMailbox], newUuid.toString, systemService = true)
|
2011-09-09 13:46:36 +02:00
|
|
|
for (i ← 1 to 10) actor1 ! "hello"
|
|
|
|
|
|
2011-09-15 08:12:07 +02:00
|
|
|
actor1.getDispatcher.mailboxSize(actor1.underlying) should be > (0)
|
2011-09-09 13:46:36 +02:00
|
|
|
val actor2 = fromBinary(toBinary(actor1)).asInstanceOf[LocalActorRef]
|
|
|
|
|
Thread.sleep(1000)
|
2011-09-15 08:12:07 +02:00
|
|
|
actor2.getDispatcher.mailboxSize(actor1.underlying) should be > (0)
|
2011-09-09 13:46:36 +02:00
|
|
|
(actor2 ? "hello-reply").get should equal("world")
|
|
|
|
|
|
|
|
|
|
val actor3 = fromBinary(toBinary(actor1, false)).asInstanceOf[LocalActorRef]
|
|
|
|
|
Thread.sleep(1000)
|
2011-09-15 08:12:07 +02:00
|
|
|
actor3.getDispatcher.mailboxSize(actor1.underlying) should equal(0)
|
2011-09-09 13:46:36 +02:00
|
|
|
(actor3 ? "hello-reply").get should equal("world")
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-16 15:28:43 +02:00
|
|
|
"should be able to serialize and deserialize a PersonActorWithMessagesInMailbox" ignore {
|
2011-09-09 13:46:36 +02:00
|
|
|
|
|
|
|
|
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)
|
2011-09-09 13:46:36 +02:00
|
|
|
(actor1 ! p1)
|
|
|
|
|
(actor1 ! p1)
|
|
|
|
|
(actor1 ! p1)
|
|
|
|
|
(actor1 ! p1)
|
|
|
|
|
(actor1 ! p1)
|
|
|
|
|
(actor1 ! p1)
|
|
|
|
|
(actor1 ! p1)
|
|
|
|
|
(actor1 ! p1)
|
|
|
|
|
(actor1 ! p1)
|
|
|
|
|
(actor1 ! p1)
|
2011-09-15 08:12:07 +02:00
|
|
|
actor1.getDispatcher.mailboxSize(actor1.underlying) should be > (0)
|
2011-09-09 13:46:36 +02:00
|
|
|
val actor2 = fromBinary(toBinary(actor1)).asInstanceOf[LocalActorRef]
|
|
|
|
|
Thread.sleep(1000)
|
2011-09-15 08:12:07 +02:00
|
|
|
actor2.getDispatcher.mailboxSize(actor1.underlying) should be > (0)
|
2011-09-09 13:46:36 +02:00
|
|
|
(actor2 ? "hello-reply").get should equal("hello")
|
|
|
|
|
|
|
|
|
|
val actor3 = fromBinary(toBinary(actor1, false)).asInstanceOf[LocalActorRef]
|
|
|
|
|
Thread.sleep(1000)
|
2011-09-15 08:12:07 +02:00
|
|
|
actor3.getDispatcher.mailboxSize(actor1.underlying) should equal(0)
|
2011-09-09 13:46:36 +02:00
|
|
|
(actor3 ? "hello-reply").get should equal("hello")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-16 15:28:43 +02:00
|
|
|
"serialize protobuf" should {
|
|
|
|
|
"should serialize" ignore {
|
2011-09-09 13:46:36 +02:00
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-16 15:28:43 +02:00
|
|
|
"serialize actor that accepts protobuf message" ignore {
|
|
|
|
|
"should serialize" ignore {
|
2011-09-09 13:46:36 +02:00
|
|
|
|
2011-09-09 17:05:39 +02:00
|
|
|
val actor1 = new LocalActorRef(Props[MyActorWithProtobufMessagesInMailbox], newUuid.toString, systemService = true)
|
2011-09-09 13:46:36 +02:00
|
|
|
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
|
2011-09-15 08:12:07 +02:00
|
|
|
actor1.getDispatcher.mailboxSize(actor1.underlying) should be > (0)
|
2011-09-09 13:46:36 +02:00
|
|
|
val actor2 = fromBinary(toBinary(actor1)).asInstanceOf[LocalActorRef]
|
|
|
|
|
Thread.sleep(1000)
|
2011-09-15 08:12:07 +02:00
|
|
|
actor2.getDispatcher.mailboxSize(actor1.underlying) should be > (0)
|
2011-09-09 13:46:36 +02:00
|
|
|
(actor2 ? "hello-reply").get should equal("world")
|
|
|
|
|
|
|
|
|
|
val actor3 = fromBinary(toBinary(actor1, false)).asInstanceOf[LocalActorRef]
|
|
|
|
|
Thread.sleep(1000)
|
2011-09-15 08:12:07 +02:00
|
|
|
actor3.getDispatcher.mailboxSize(actor1.underlying) should equal(0)
|
2011-09-09 13:46:36 +02:00
|
|
|
(actor3 ? "hello-reply").get should equal("world")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class MyJavaSerializableActor extends Actor with scala.Serializable {
|
|
|
|
|
var count = 0
|
|
|
|
|
self.receiveTimeout = Some(1000)
|
|
|
|
|
|
|
|
|
|
def receive = {
|
|
|
|
|
case "hello" ⇒
|
|
|
|
|
count = count + 1
|
|
|
|
|
self.reply("world " + count)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class MyStatelessActorWithMessagesInMailbox extends Actor with scala.Serializable {
|
|
|
|
|
def receive = {
|
|
|
|
|
case "hello" ⇒
|
|
|
|
|
Thread.sleep(500)
|
|
|
|
|
case "hello-reply" ⇒ self.reply("world")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class MyActorWithProtobufMessagesInMailbox extends Actor with scala.Serializable {
|
|
|
|
|
def receive = {
|
|
|
|
|
case m: Message ⇒
|
|
|
|
|
Thread.sleep(500)
|
|
|
|
|
case "hello-reply" ⇒ self.reply("world")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class PersonActorWithMessagesInMailbox extends Actor with scala.Serializable {
|
|
|
|
|
def receive = {
|
|
|
|
|
case p: Person ⇒
|
|
|
|
|
Thread.sleep(500)
|
|
|
|
|
case "hello-reply" ⇒ self.reply("hello")
|
|
|
|
|
}
|
|
|
|
|
}
|