Optimize remote serialization for the create from class case. See #1755

* Added FromClassCreator as special Function0 to be able to serialize
  class name as protobuf string
This commit is contained in:
Patrik Nordwall 2012-05-15 18:22:40 +02:00
parent 793af8b4ec
commit 31ace9e83f
5 changed files with 278 additions and 160 deletions

View file

@ -17,6 +17,7 @@ import akka.remote.RemoteScope
import akka.routing.RoundRobinRouter
import akka.routing.FromConfig
import akka.util.duration._
import akka.actor.FromClassCreator
object DaemonMsgCreateSerializerSpec {
class MyActor extends Actor {
@ -39,45 +40,51 @@ class DaemonMsgCreateSerializerSpec extends AkkaSpec {
ser.serializerFor(classOf[DaemonMsgCreate]).getClass must be(classOf[DaemonMsgCreateSerializer])
}
"serialize and de-serialize simple DaemonMsgCreate" in {
val msg = DaemonMsgCreate(
props = Props[MyActor],
deploy = Deploy(),
path = "foo",
supervisor = supervisor)
val bytes = ser.serialize(msg) match {
case Left(exception) fail(exception)
case Right(bytes) bytes
"serialize and de-serialize DaemonMsgCreate with FromClassCreator" in {
verifySerialization {
DaemonMsgCreate(
props = Props[MyActor],
deploy = Deploy(),
path = "foo",
supervisor = supervisor)
}
ser.deserialize(bytes.asInstanceOf[Array[Byte]], classOf[DaemonMsgCreate]) match {
case Left(exception) fail(exception)
case Right(m: DaemonMsgCreate) assertDaemonMsgCreate(msg, m)
}
"serialize and de-serialize DaemonMsgCreate with function creator" in {
verifySerialization {
DaemonMsgCreate(
props = Props().withCreator(new MyActor),
deploy = Deploy(),
path = "foo",
supervisor = supervisor)
}
}
"serialize and de-serialize DaemonMsgCreate with Deploy and RouterConfig" in {
// Duration.Inf doesn't equal Duration.Inf, so we use another for test
val supervisorStrategy = OneForOneStrategy(3, 10 seconds) {
case _ SupervisorStrategy.Escalate
verifySerialization {
// Duration.Inf doesn't equal Duration.Inf, so we use another for test
val supervisorStrategy = OneForOneStrategy(3, 10 seconds) {
case _ SupervisorStrategy.Escalate
}
val deploy1 = Deploy(
path = "path1",
config = ConfigFactory.parseString("a=1"),
routerConfig = RoundRobinRouter(nrOfInstances = 5, supervisorStrategy = supervisorStrategy),
scope = RemoteScope(Address("akka", "Test", "host1", 1921)))
val deploy2 = Deploy(
path = "path2",
config = ConfigFactory.parseString("a=2"),
routerConfig = FromConfig,
scope = RemoteScope(Address("akka", "Test", "host2", 1922)))
DaemonMsgCreate(
props = Props[MyActor].withDispatcher("my-disp").withDeploy(deploy1),
deploy = deploy2,
path = "foo",
supervisor = supervisor)
}
val deploy1 = Deploy(
path = "path1",
config = ConfigFactory.parseString("a=1"),
routerConfig = RoundRobinRouter(nrOfInstances = 5, supervisorStrategy = supervisorStrategy),
scope = RemoteScope(Address("akka", "Test", "host1", 1921)))
val deploy2 = Deploy(
path = "path2",
config = ConfigFactory.parseString("a=2"),
routerConfig = FromConfig,
scope = RemoteScope(Address("akka", "Test", "host2", 1922)))
val msg = DaemonMsgCreate(
props = Props[MyActor].withDispatcher("my-disp").withDeploy(deploy1),
deploy = deploy2,
path = "foo",
supervisor = supervisor)
}
def verifySerialization(msg: DaemonMsgCreate): Unit = {
val bytes = ser.serialize(msg) match {
case Left(exception) fail(exception)
case Right(bytes) bytes
@ -89,7 +96,9 @@ class DaemonMsgCreateSerializerSpec extends AkkaSpec {
}
def assertDaemonMsgCreate(expected: DaemonMsgCreate, got: DaemonMsgCreate): Unit = {
// can't compare props.creator
// can't compare props.creator when function
if (expected.props.creator.isInstanceOf[FromClassCreator])
assert(got.props.creator === expected.props.creator)
assert(got.props.dispatcher === expected.props.dispatcher)
assert(got.props.dispatcher === expected.props.dispatcher)
assert(got.props.routerConfig === expected.props.routerConfig)