From 5573b59bc53cb0010be9f2f3baf80e278a305c56 Mon Sep 17 00:00:00 2001 From: Debasish Ghosh Date: Sun, 20 Mar 2011 22:16:26 +0530 Subject: [PATCH] added test cases for Java serialization of actors in course of documenting the stuff in the wiki --- .../akka/serialization/SerializationTest.java | 124 ++++++++++++++++++ .../serialization/SerializationTestActor.java | 9 ++ .../serialization/JavaSerializationTest.scala | 5 + 3 files changed, 138 insertions(+) create mode 100644 akka-remote/src/test/java/akka/serialization/SerializationTest.java create mode 100644 akka-remote/src/test/java/akka/serialization/SerializationTestActor.java create mode 100644 akka-remote/src/test/scala/serialization/JavaSerializationTest.scala diff --git a/akka-remote/src/test/java/akka/serialization/SerializationTest.java b/akka-remote/src/test/java/akka/serialization/SerializationTest.java new file mode 100644 index 0000000000..837b2790b4 --- /dev/null +++ b/akka-remote/src/test/java/akka/serialization/SerializationTest.java @@ -0,0 +1,124 @@ +package akka.serialization; + +import org.junit.Test; +import akka.actor.*; +import akka.actor.serialization.*; +import static org.junit.Assert.*; +import static akka.serialization.ActorSerialization.*; + +class SerializationTestActorFormat implements StatelessActorFormat { + @Override + public SerializationTestActor fromBinary(byte[] bytes, SerializationTestActor act) { + return (SerializationTestActor) StatelessActorFormat$class.fromBinary(this, bytes, act); + } + + @Override + public byte[] toBinary(SerializationTestActor ac) { + return StatelessActorFormat$class.toBinary(this, ac); + } +} + +class MyUntypedActorFormat implements Format { + @Override + public MyUntypedActor fromBinary(byte[] bytes, MyUntypedActor act) { + ProtobufProtocol.Counter p = + (ProtobufProtocol.Counter) new SerializerFactory().getProtobuf().fromBinary(bytes, ProtobufProtocol.Counter.class); + act.count_$eq(p.getCount()); + return act; + } + + @Override + public byte[] toBinary(MyUntypedActor ac) { + return ProtobufProtocol.Counter.newBuilder().setCount(ac.count()).build().toByteArray(); + } + } + + +public class SerializationTest { + + @Test public void mustBeAbleToSerializeAfterCreateActorRefFromClass() { + ActorRef ref = Actors.actorOf(SerializationTestActor.class); + assertNotNull(ref); + ref.start(); + try { + Object result = ref.sendRequestReply("Hello"); + assertEquals("got it!", result); + } catch (ActorTimeoutException ex) { + fail("actor should not time out"); + } + + Format f = new SerializationTestActorFormat(); + byte[] bytes = toBinaryJ(ref, f, false); + ActorRef r = fromBinaryJ(bytes, f); + assertNotNull(r); + r.start(); + try { + Object result = r.sendRequestReply("Hello"); + assertEquals("got it!", result); + } catch (ActorTimeoutException ex) { + fail("actor should not time out"); + } + ref.stop(); + r.stop(); + } + + @Test public void mustBeAbleToSerializeAfterCreateActorRefFromFactory() { + ActorRef ref = Actors.actorOf(new UntypedActorFactory() { + public Actor create() { + return new SerializationTestActor(); + } + }); + assertNotNull(ref); + ref.start(); + try { + Object result = ref.sendRequestReply("Hello"); + assertEquals("got it!", result); + } catch (ActorTimeoutException ex) { + fail("actor should not time out"); + } + + Format f = new SerializationTestActorFormat(); + byte[] bytes = toBinaryJ(ref, f, false); + ActorRef r = fromBinaryJ(bytes, f); + assertNotNull(r); + r.start(); + try { + Object result = r.sendRequestReply("Hello"); + assertEquals("got it!", result); + } catch (ActorTimeoutException ex) { + fail("actor should not time out"); + } + ref.stop(); + r.stop(); + } + + @Test public void mustBeAbleToSerializeAStatefulActor() { + ActorRef ref = Actors.actorOf(MyUntypedActor.class); + assertNotNull(ref); + ref.start(); + try { + Object result = ref.sendRequestReply("hello"); + assertEquals("world 1", result); + result = ref.sendRequestReply("hello"); + assertEquals("world 2", result); + } catch (ActorTimeoutException ex) { + fail("actor should not time out"); + } + + Format f = new MyUntypedActorFormat(); + byte[] bytes = toBinaryJ(ref, f, false); + ActorRef r = fromBinaryJ(bytes, f); + assertNotNull(r); + r.start(); + try { + Object result = r.sendRequestReply("hello"); + assertEquals("world 3", result); + result = r.sendRequestReply("hello"); + assertEquals("world 4", result); + } catch (ActorTimeoutException ex) { + fail("actor should not time out"); + } + ref.stop(); + r.stop(); + } +} diff --git a/akka-remote/src/test/java/akka/serialization/SerializationTestActor.java b/akka-remote/src/test/java/akka/serialization/SerializationTestActor.java new file mode 100644 index 0000000000..a1077e01af --- /dev/null +++ b/akka-remote/src/test/java/akka/serialization/SerializationTestActor.java @@ -0,0 +1,9 @@ +package akka.serialization; + +import akka.actor.UntypedActor; + +public class SerializationTestActor extends UntypedActor { + public void onReceive(Object msg) { + getContext().replySafe("got it!"); + } +} diff --git a/akka-remote/src/test/scala/serialization/JavaSerializationTest.scala b/akka-remote/src/test/scala/serialization/JavaSerializationTest.scala new file mode 100644 index 0000000000..9705a30cca --- /dev/null +++ b/akka-remote/src/test/scala/serialization/JavaSerializationTest.scala @@ -0,0 +1,5 @@ +package akka.serialization + +import org.scalatest.junit.JUnitSuite + +class JavaSerializationTest extends SerializationTest with JUnitSuite