pekko/akka-actors/src/test/scala/RemoteActorTest.scala

82 lines
1.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
2009-11-25 12:42:50 +01:00
import se.scalablesolutions.akka.nio.{RemoteNode, RemoteServer, RemoteClient}
object Global {
var oneWay = "nada"
}
class RemoteActorSpecActorUnidirectional extends Actor {
def receive = {
case "OneWay" =>
Global.oneWay = "received"
}
}
class RemoteActorSpecActorBidirectional extends Actor {
def receive = {
case "Hello" =>
reply("World")
case "Failure" =>
throw new RuntimeException("expected")
}
}
2009-10-26 11:30:03 +01:00
class RemoteActorTest extends JUnitSuite {
import Actor.Sender.Self
akka.Config.config
new Thread(new Runnable() {
def run = {
2009-11-25 12:42:50 +01:00
RemoteNode.start
}
}).start
Thread.sleep(1000)
private val unit = TimeUnit.MILLISECONDS
@Test
2009-10-26 11:30:03 +01:00
def shouldSendOneWay = {
implicit val timeout = 500000000L
val actor = new RemoteActorSpecActorUnidirectional
actor.makeRemote(RemoteServer.HOSTNAME, RemoteServer.PORT)
actor.start
val result = actor ! "OneWay"
Thread.sleep(100)
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 = {
implicit val timeout = 500000000L
val actor = new RemoteActorSpecActorBidirectional
actor.makeRemote(RemoteServer.HOSTNAME, RemoteServer.PORT)
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
2009-10-26 11:30:03 +01:00
def shouldSendReceiveException = {
implicit val timeout = 500000000L
val actor = new RemoteActorSpecActorBidirectional
actor.makeRemote(RemoteServer.HOSTNAME, RemoteServer.PORT)
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
}
}