From a605ac4eeadf66115f82d0b0d4a9cfee727246f7 Mon Sep 17 00:00:00 2001 From: Paul Pacheco Date: Thu, 18 Nov 2010 22:38:45 -0600 Subject: [PATCH] Refatored createActor, separate unit tests cleanup according to viktor's suggestions --- .../src/main/scala/remote/RemoteServer.scala | 13 +- .../ServerInitiatedRemoteActorSpec.scala | 116 ------------- ...erverInitiatedRemoteSessionActorSpec.scala | 162 ++++++++++++++++++ ...InitiatedRemoteTypedSessionActorSpec.scala | 6 +- 4 files changed, 175 insertions(+), 122 deletions(-) create mode 100644 akka-remote/src/test/scala/remote/ServerInitiatedRemoteSessionActorSpec.scala diff --git a/akka-remote/src/main/scala/remote/RemoteServer.scala b/akka-remote/src/main/scala/remote/RemoteServer.scala index 57fb3d4078..7ec32c7c80 100644 --- a/akka-remote/src/main/scala/remote/RemoteServer.scala +++ b/akka-remote/src/main/scala/remote/RemoteServer.scala @@ -820,16 +820,23 @@ class RemoteServerHandler( val actorRefOrNull = findActorByIdOrUuid(id, uuidFrom(uuid.getHigh,uuid.getLow).toString) - if (actorRefOrNull ne null) + if (actorRefOrNull ne null) { + println("Giving actor by id or uuid ") actorRefOrNull + } else { // the actor has not been registered globally. See if we have it in the session val sessionActorRefOrNull = createSessionActor(actorInfo, channel); - if (sessionActorRefOrNull ne null) + if (sessionActorRefOrNull ne null) { + println("giving session actor") sessionActorRefOrNull + } else // maybe it is a client managed actor - createClientManagedActor(actorInfo) + { + println("Client managed actor") + createClientManagedActor(actorInfo) + } } } diff --git a/akka-remote/src/test/scala/remote/ServerInitiatedRemoteActorSpec.scala b/akka-remote/src/test/scala/remote/ServerInitiatedRemoteActorSpec.scala index 5ffbecb612..b0dee8563e 100644 --- a/akka-remote/src/test/scala/remote/ServerInitiatedRemoteActorSpec.scala +++ b/akka-remote/src/test/scala/remote/ServerInitiatedRemoteActorSpec.scala @@ -38,35 +38,6 @@ object ServerInitiatedRemoteActorSpec { } } - case class Login(user:String); - case class GetUser(); - case class DoSomethingFunny(); - - val instantiatedSessionActors= Set[ActorRef](); - - class RemoteStatefullSessionActorSpec extends Actor { - - var user : String= "anonymous"; - - override def preStart = { - instantiatedSessionActors += self; - } - - override def postStop = { - instantiatedSessionActors -= self; - } - - def receive = { - case Login(user) => - this.user = user; - case GetUser() => - self.reply(this.user) - case DoSomethingFunny() => - throw new Exception("Bad boy") - } - } - - object RemoteActorSpecActorAsyncSender { val latch = new CountDownLatch(1) } @@ -94,7 +65,6 @@ class ServerInitiatedRemoteActorSpec extends JUnitSuite { server.register(actorOf[RemoteActorSpecActorUnidirectional]) server.register(actorOf[RemoteActorSpecActorBidirectional]) server.register(actorOf[RemoteActorSpecActorAsyncSender]) - server.registerPerSession("untyped-session-actor-service", actorOf[RemoteStatefullSessionActorSpec]) Thread.sleep(1000) } @@ -135,85 +105,6 @@ class ServerInitiatedRemoteActorSpec extends JUnitSuite { actor.stop } - @Test - def shouldKeepSessionInformation { - - //RemoteClient.clientFor(HOSTNAME, PORT).connect - - val session1 = RemoteClient.actorFor( - "untyped-session-actor-service", - 5000L, - HOSTNAME, PORT) - - - val default1 = session1 !! GetUser(); - assert("anonymous" === default1.get.asInstanceOf[String]) - - session1 ! Login("session[1]"); - - val result1 = session1 !! GetUser(); - assert("session[1]" === result1.get.asInstanceOf[String]) - - session1.stop() - - RemoteClient.shutdownAll - - //RemoteClient.clientFor(HOSTNAME, PORT).connect - - val session2 = RemoteClient.actorFor( - "untyped-session-actor-service", - 5000L, - HOSTNAME, PORT) - - // since this is a new session, the server should reset the state - val default2 = session2 !! GetUser(); - assert("anonymous" === default2.get.asInstanceOf[String]) - - session2.stop() - - RemoteClient.shutdownAll - } - - @Test - def shouldStopActorOnDisconnect{ - - - val session1 = RemoteClient.actorFor( - "untyped-session-actor-service", - 5000L, - HOSTNAME, PORT) - - - val default1 = session1 !! GetUser(); - assert("anonymous" === default1.get.asInstanceOf[String]) - - assert(instantiatedSessionActors.size == 1); - - RemoteClient.shutdownAll - Thread.sleep(1000) - assert(instantiatedSessionActors.size == 0); - - } - - @Test - def shouldStopActorOnError{ - - - val session1 = RemoteClient.actorFor( - "untyped-session-actor-service", - 5000L, - HOSTNAME, PORT) - - - session1 ! DoSomethingFunny(); - session1.stop() - - RemoteClient.shutdownAll - Thread.sleep(1000) - - assert(instantiatedSessionActors.size == 0); - - } @Test def shouldSendWithBangAndGetReplyThroughSenderRef { @@ -317,13 +208,6 @@ class ServerInitiatedRemoteActorSpec extends JUnitSuite { server.unregister("my-service-1") assert(server.actors.get("my-service-1") eq null, "actor unregistered") } - @Test - def shouldRegisterAndUnregisterSession { - server.registerPerSession("my-service-1", actorOf[RemoteActorSpecActorUnidirectional]) - assert(server.actorsFactories.get("my-service-1") ne null, "actor registered") - server.unregisterPerSession("my-service-1") - assert(server.actorsFactories.get("my-service-1") eq null, "actor unregistered") - } @Test def shouldRegisterAndUnregisterByUuid { diff --git a/akka-remote/src/test/scala/remote/ServerInitiatedRemoteSessionActorSpec.scala b/akka-remote/src/test/scala/remote/ServerInitiatedRemoteSessionActorSpec.scala new file mode 100644 index 0000000000..e1f1db4c9e --- /dev/null +++ b/akka-remote/src/test/scala/remote/ServerInitiatedRemoteSessionActorSpec.scala @@ -0,0 +1,162 @@ +/** + * Copyright (C) 2009-2010 Scalable Solutions AB + */ + +package akka.actor.remote + +import org.scalatest._ +import org.scalatest.matchers.ShouldMatchers +import org.scalatest.BeforeAndAfterAll +import org.scalatest.junit.JUnitRunner +import org.junit.runner.RunWith + +import java.util.concurrent.TimeUnit + +import akka.remote.{RemoteServer, RemoteClient} +import akka.actor._ +import RemoteTypedActorLog._ + +object ServerInitiatedRemoteSessionActorSpec { + val HOSTNAME = "localhost" + val PORT = 9990 + var server: RemoteServer = null + + case class Login(user:String); + case class GetUser(); + case class DoSomethingFunny(); + + val instantiatedSessionActors= Set[ActorRef](); + + class RemoteStatefullSessionActorSpec extends Actor { + + var user : String= "anonymous"; + + override def preStart = { + instantiatedSessionActors += self; + } + + override def postStop = { + instantiatedSessionActors -= self; + } + + def receive = { + case Login(user) => + this.user = user; + case GetUser() => + self.reply(this.user) + case DoSomethingFunny() => + throw new Exception("Bad boy") + } + } + +} + +@RunWith(classOf[JUnitRunner]) +class ServerInitiatedRemoteSessionActorSpec extends + FlatSpec with + ShouldMatchers with + BeforeAndAfterEach { + import ServerInitiatedRemoteTypedActorSpec._ + + private val unit = TimeUnit.MILLISECONDS + + + override def beforeEach = { + server = new RemoteServer() + server.start(HOSTNAME, PORT) + + server.registerTypedPerSessionActor("untyped-session-actor-service", actorOf[RemoteStatefullSessionActorSpec]) + + Thread.sleep(1000) + } + + // make sure the servers shutdown cleanly after the test has finished + override def afterEach = { + try { + server.shutdown + RemoteClient.shutdownAll + Thread.sleep(1000) + } catch { + case e => () + } + } + + "A remote session Actor" should "create a new session actor per connection" in { + clearMessageLogs + + val session1 = RemoteClient.actorFor( + "untyped-session-actor-service", + 5000L, + HOSTNAME, PORT) + + val default1 = session1 !! GetUser() + default1.get.asInstanceOf[String] should equal ("anonymous") + session1 ! Login("session[1]") + val result1 = session1 !! GetUser() + result1.get.asInstanceOf[String] should equal ("session[1]") + + session1.stop() + + RemoteClient.shutdownAll + + //RemoteClient.clientFor(HOSTNAME, PORT).connect + + val session2 = RemoteClient.actorFor( + "untyped-session-actor-service", + 5000L, + HOSTNAME, PORT) + + // since this is a new session, the server should reset the state + val default2 = session2 !! GetUser(); + default2.get.asInstanceOf[String] should equal ("anonymous") + + session2.stop() + + } + + it should "stop the actor when the client disconnects" in { + + val session1 = RemoteClient.actorFor( + "untyped-session-actor-service", + 5000L, + HOSTNAME, PORT) + + + val default1 = session1 !! GetUser() + default1.get.asInstanceOf[String] should equal ("anonymous") + + instantiatedSessionActors.size should have size (1) + + RemoteClient.shutdownAll + Thread.sleep(1000) + instantiatedSessionActors.size should have size (0); + + } + + it should "stop the actor when there is an error" in { + + val session1 = RemoteClient.actorFor( + "untyped-session-actor-service", + 5000L, + HOSTNAME, PORT) + + + session1 ! DoSomethingFunny(); + session1.stop() + + RemoteClient.shutdownAll + Thread.sleep(1000) + + instantiatedSessionActors.size should have size (0); + } + + + it should "be able to unregister" in { + server.registerPerSession("my-service-1", actorOf[RemoteActorSpecActorUnidirectional]) + server.actorsFactories.get("my-service-1") should not be (null) + server.unregisterPerSession("my-service-1") + server.actorsFactories.get("my-service-1") should be (null) + } + +} + diff --git a/akka-remote/src/test/scala/remote/ServerInitiatedRemoteTypedSessionActorSpec.scala b/akka-remote/src/test/scala/remote/ServerInitiatedRemoteTypedSessionActorSpec.scala index e766c24d67..575d82ca96 100644 --- a/akka-remote/src/test/scala/remote/ServerInitiatedRemoteTypedSessionActorSpec.scala +++ b/akka-remote/src/test/scala/remote/ServerInitiatedRemoteTypedSessionActorSpec.scala @@ -26,13 +26,13 @@ object ServerInitiatedRemoteTypedSessionActorSpec { class ServerInitiatedRemoteTypedSessionActorSpec extends FlatSpec with ShouldMatchers with - BeforeAndAfterAll { + BeforeAndAfterEach { import ServerInitiatedRemoteTypedActorSpec._ private val unit = TimeUnit.MILLISECONDS - override def beforeAll = { + override def beforeEach = { server = new RemoteServer() server.start(HOSTNAME, PORT) @@ -43,7 +43,7 @@ class ServerInitiatedRemoteTypedSessionActorSpec extends } // make sure the servers shutdown cleanly after the test has finished - override def afterAll = { + override def afterEach = { try { server.shutdown RemoteClient.shutdownAll