pekko/akka-core/src/test/scala/ClientInitiatedRemoteActorTest.scala

135 lines
2.9 KiB
Scala
Raw Normal View History

2009-09-02 09:10:21 +02:00
package se.scalablesolutions.akka.actor
import java.util.concurrent.TimeUnit
import junit.framework.TestCase
2009-10-26 11:30:03 +01:00
import org.scalatest.junit.JUnitSuite
import org.junit.{Test, Before, After}
2009-11-25 12:42:50 +01:00
2009-12-16 23:20:15 +01:00
import se.scalablesolutions.akka.remote.{RemoteServer, RemoteClient}
import se.scalablesolutions.akka.dispatch.Dispatchers
2010-02-16 18:36:16 +01:00
object Global {
var oneWay = "nada"
var remoteReply = "nada"
}
class RemoteActorSpecActorUnidirectional extends Actor {
dispatcher = Dispatchers.newThreadBasedDispatcher(this)
2010-02-16 18:36:16 +01:00
def receive = {
case "OneWay" =>
Global.oneWay = "received"
}
2010-02-16 18:36:16 +01:00
}
2010-02-16 18:36:16 +01:00
class RemoteActorSpecActorBidirectional extends Actor {
def receive = {
case "Hello" =>
reply("World")
case "Failure" =>
throw new RuntimeException("expected")
}
2010-02-16 18:36:16 +01:00
}
2010-02-16 18:36:16 +01:00
case class Send(actor: Actor)
2010-02-16 18:36:16 +01:00
class RemoteActorSpecActorAsyncSender extends Actor {
def receive = {
case Send(actor: Actor) =>
actor ! "Hello"
case "World" =>
Global.remoteReply = "replied"
}
def send(actor: Actor) {
this ! Send(actor)
}
}
class ClientInitiatedRemoteActorTest extends JUnitSuite {
2010-02-16 18:36:16 +01:00
import Actor.Sender.Self
akka.config.Config.config
val HOSTNAME = "localhost"
val PORT1 = 9990
val PORT2 = 9991
var s1: RemoteServer = null
var s2: RemoteServer = null
@Before
def init() {
s1 = new RemoteServer()
s2 = new RemoteServer()
s1.start(HOSTNAME, PORT1)
s2.start(HOSTNAME, PORT2)
Thread.sleep(1000)
}
private val unit = TimeUnit.MILLISECONDS
// make sure the servers shutdown cleanly after the test has
// finished
@After
def finished() {
s1.shutdown
s2.shutdown
RemoteClient.shutdownAll
Thread.sleep(1000)
}
@Test
2009-10-26 11:30:03 +01:00
def shouldSendOneWay = {
val actor = new RemoteActorSpecActorUnidirectional
actor.makeRemote(HOSTNAME, PORT1)
actor.start
val result = actor ! "OneWay"
Thread.sleep(1000)
2009-10-26 11:30:03 +01:00
assert("received" === Global.oneWay)
2009-11-20 11:12:03 +01:00
actor.stop
}
@Test
2009-10-26 11:30:03 +01:00
def shouldSendReplyAsync = {
val actor = new RemoteActorSpecActorBidirectional
actor.makeRemote(HOSTNAME, PORT1)
actor.start
val result = actor !! "Hello"
2009-10-26 11:30:03 +01:00
assert("World" === result.get.asInstanceOf[String])
2009-11-20 11:12:03 +01:00
actor.stop
}
@Test
def shouldSendRemoteReply = {
implicit val timeout = 500000000L
val actor = new RemoteActorSpecActorBidirectional
actor.makeRemote(HOSTNAME, PORT2)
actor.start
val sender = new RemoteActorSpecActorAsyncSender
2010-01-18 22:33:29 +01:00
sender.setReplyToAddress(HOSTNAME, PORT1)
sender.start
sender.send(actor)
Thread.sleep(1000)
assert("replied" === Global.remoteReply)
actor.stop
}
@Test
2009-10-26 11:30:03 +01:00
def shouldSendReceiveException = {
implicit val timeout = 500000000L
val actor = new RemoteActorSpecActorBidirectional
actor.makeRemote(HOSTNAME, PORT1)
actor.start
try {
actor !! "Failure"
fail("Should have thrown an exception")
} catch {
case e =>
2009-10-26 11:30:03 +01:00
assert("expected" === e.getMessage())
}
2009-11-20 11:12:03 +01:00
actor.stop
}
}