2010-06-10 16:00:42 +02:00
|
|
|
package se.scalablesolutions.akka.actor
|
|
|
|
|
|
|
|
|
|
import Actor._
|
|
|
|
|
|
|
|
|
|
import org.scalatest.Spec
|
|
|
|
|
import org.scalatest.Assertions
|
|
|
|
|
import org.scalatest.matchers.ShouldMatchers
|
|
|
|
|
import org.scalatest.BeforeAndAfterAll
|
|
|
|
|
import org.scalatest.junit.JUnitRunner
|
|
|
|
|
import org.junit.runner.RunWith
|
|
|
|
|
|
2010-06-18 09:30:31 +02:00
|
|
|
import com.google.protobuf.Message
|
|
|
|
|
|
2010-06-10 16:00:42 +02:00
|
|
|
@RunWith(classOf[JUnitRunner])
|
|
|
|
|
class SerializableActorSpec extends
|
|
|
|
|
Spec with
|
|
|
|
|
ShouldMatchers with
|
|
|
|
|
BeforeAndAfterAll {
|
|
|
|
|
|
|
|
|
|
describe("SerializableActor") {
|
|
|
|
|
it("should be able to serialize and deserialize a JavaSerializableActor") {
|
|
|
|
|
val actor1 = actorOf[JavaSerializableTestActor].start
|
2010-06-18 09:30:31 +02:00
|
|
|
(actor1 !! "hello").getOrElse("_") should equal("world 1")
|
|
|
|
|
|
|
|
|
|
val bytes = actor1.toBinary
|
|
|
|
|
val actor2 = ActorRef.fromBinaryToLocalActorRef(bytes)
|
|
|
|
|
|
|
|
|
|
actor2.start
|
|
|
|
|
(actor2 !! "hello").getOrElse("_") should equal("world 2")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
it("should be able to serialize and deserialize a ProtobufSerializableActor") {
|
|
|
|
|
val actor1 = actorOf[ProtobufSerializableTestActor].start
|
|
|
|
|
(actor1 !! "hello").getOrElse("_") should equal("world 1")
|
|
|
|
|
(actor1 !! "hello").getOrElse("_") should equal("world 2")
|
|
|
|
|
|
|
|
|
|
val bytes = actor1.toBinary
|
|
|
|
|
val actor2 = ActorRef.fromBinaryToLocalActorRef(bytes)
|
|
|
|
|
|
|
|
|
|
actor2.start
|
|
|
|
|
(actor2 !! "hello").getOrElse("_") should equal("world 3")
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-22 10:02:19 +02:00
|
|
|
it("should be able to serialize and deserialize a StatelessSerializableActor") {
|
|
|
|
|
val actor1 = actorOf[StatelessSerializableTestActor].start
|
|
|
|
|
(actor1 !! "hello").getOrElse("_") should equal("world")
|
2010-06-10 16:00:42 +02:00
|
|
|
|
|
|
|
|
val bytes = actor1.toBinary
|
2010-06-16 16:15:34 +02:00
|
|
|
val actor2 = ActorRef.fromBinaryToLocalActorRef(bytes)
|
|
|
|
|
|
|
|
|
|
actor2.start
|
2010-06-22 10:02:19 +02:00
|
|
|
(actor2 !! "hello").getOrElse("_") should equal("world")
|
2010-06-10 16:00:42 +02:00
|
|
|
}
|
2010-06-24 08:48:48 +02:00
|
|
|
|
|
|
|
|
it("should be able to serialize and deserialize a StatelessSerializableTestActorWithMessagesInMailbox") {
|
|
|
|
|
val actor1 = actorOf[StatelessSerializableTestActorWithMessagesInMailbox].start
|
|
|
|
|
(actor1 ! "hello")
|
|
|
|
|
(actor1 ! "hello")
|
|
|
|
|
(actor1 ! "hello")
|
|
|
|
|
(actor1 ! "hello")
|
|
|
|
|
(actor1 ! "hello")
|
|
|
|
|
(actor1 ! "hello")
|
|
|
|
|
(actor1 ! "hello")
|
|
|
|
|
(actor1 ! "hello")
|
|
|
|
|
(actor1 ! "hello")
|
|
|
|
|
(actor1 ! "hello")
|
|
|
|
|
val actor2 = ActorRef.fromBinaryToLocalActorRef(actor1.toBinary)
|
|
|
|
|
Thread.sleep(1000)
|
|
|
|
|
(actor2 !! "hello-reply").getOrElse("_") should equal("world")
|
|
|
|
|
}
|
2010-06-18 09:30:31 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@serializable class JavaSerializableTestActor extends JavaSerializableActor {
|
|
|
|
|
private var count = 0
|
|
|
|
|
def receive = {
|
|
|
|
|
case "hello" =>
|
|
|
|
|
count = count + 1
|
|
|
|
|
self.reply("world " + count)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-22 10:02:19 +02:00
|
|
|
class StatelessSerializableTestActor extends StatelessSerializableActor {
|
|
|
|
|
def receive = {
|
|
|
|
|
case "hello" =>
|
|
|
|
|
self.reply("world")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-18 09:30:31 +02:00
|
|
|
class ProtobufSerializableTestActor extends ProtobufSerializableActor[ProtobufProtocol.Counter] {
|
|
|
|
|
val clazz = classOf[ProtobufProtocol.Counter]
|
|
|
|
|
private var count = 0
|
|
|
|
|
|
|
|
|
|
def toProtobuf = ProtobufProtocol.Counter.newBuilder.setCount(count).build
|
|
|
|
|
def fromProtobuf(message: ProtobufProtocol.Counter) = count = message.getCount
|
|
|
|
|
|
|
|
|
|
def receive = {
|
|
|
|
|
case "hello" =>
|
|
|
|
|
count = count + 1
|
|
|
|
|
self.reply("world " + count)
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-06-24 08:48:48 +02:00
|
|
|
|
|
|
|
|
class StatelessSerializableTestActorWithMessagesInMailbox extends StatelessSerializableActor {
|
|
|
|
|
def receive = {
|
|
|
|
|
case "hello" =>
|
|
|
|
|
println("# messages in mailbox " + self.mailbox.size)
|
|
|
|
|
Thread.sleep(500)
|
|
|
|
|
case "hello-reply" => self.reply("world")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|