2009-09-02 09:10:21 +02:00
|
|
|
package se.scalablesolutions.akka.actor
|
2009-06-25 23:47:30 +02:00
|
|
|
|
|
|
|
|
import java.util.concurrent.TimeUnit
|
2009-06-30 16:01:50 +02:00
|
|
|
import junit.framework.TestCase
|
2009-09-03 11:02:21 +02:00
|
|
|
|
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}
|
2009-06-25 23:47:30 +02:00
|
|
|
|
|
|
|
|
object Global {
|
|
|
|
|
var oneWay = "nada"
|
|
|
|
|
}
|
|
|
|
|
class RemoteActorSpecActorUnidirectional extends Actor {
|
2009-11-20 08:29:31 +01:00
|
|
|
def receive = {
|
2009-06-25 23:47:30 +02:00
|
|
|
case "OneWay" =>
|
|
|
|
|
Global.oneWay = "received"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class RemoteActorSpecActorBidirectional extends Actor {
|
2009-11-20 08:29:31 +01:00
|
|
|
def receive = {
|
2009-06-25 23:47:30 +02:00
|
|
|
case "Hello" =>
|
|
|
|
|
reply("World")
|
|
|
|
|
case "Failure" =>
|
|
|
|
|
throw new RuntimeException("expected")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-10-26 11:30:03 +01:00
|
|
|
class RemoteActorTest extends JUnitSuite {
|
2009-11-23 15:19:53 +01:00
|
|
|
import Actor.Sender.Self
|
|
|
|
|
|
2009-09-03 11:02:21 +02:00
|
|
|
akka.Config.config
|
2009-06-25 23:47:30 +02:00
|
|
|
new Thread(new Runnable() {
|
|
|
|
|
def run = {
|
2009-11-25 12:42:50 +01:00
|
|
|
RemoteNode.start
|
2009-06-25 23:47:30 +02:00
|
|
|
}
|
|
|
|
|
}).start
|
2009-06-29 15:01:20 +02:00
|
|
|
Thread.sleep(1000)
|
2009-06-25 23:47:30 +02:00
|
|
|
|
|
|
|
|
private val unit = TimeUnit.MILLISECONDS
|
|
|
|
|
|
|
|
|
|
@Test
|
2009-10-26 11:30:03 +01:00
|
|
|
def shouldSendOneWay = {
|
2009-07-18 00:16:32 +02:00
|
|
|
implicit val timeout = 500000000L
|
2009-06-25 23:47:30 +02:00
|
|
|
val actor = new RemoteActorSpecActorUnidirectional
|
2009-07-02 13:23:03 +02:00
|
|
|
actor.makeRemote(RemoteServer.HOSTNAME, RemoteServer.PORT)
|
2009-06-25 23:47:30 +02:00
|
|
|
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
|
2009-06-25 23:47:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
2009-10-26 11:30:03 +01:00
|
|
|
def shouldSendReplyAsync = {
|
2009-07-18 00:16:32 +02:00
|
|
|
implicit val timeout = 500000000L
|
2009-06-25 23:47:30 +02:00
|
|
|
val actor = new RemoteActorSpecActorBidirectional
|
2009-07-02 13:23:03 +02:00
|
|
|
actor.makeRemote(RemoteServer.HOSTNAME, RemoteServer.PORT)
|
2009-06-25 23:47:30 +02:00
|
|
|
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
|
2009-06-25 23:47:30 +02:00
|
|
|
}
|
|
|
|
|
|
2009-06-30 16:01:50 +02:00
|
|
|
@Test
|
2009-10-26 11:30:03 +01:00
|
|
|
def shouldSendReceiveException = {
|
2009-07-18 00:16:32 +02:00
|
|
|
implicit val timeout = 500000000L
|
2009-06-25 23:47:30 +02:00
|
|
|
val actor = new RemoteActorSpecActorBidirectional
|
2009-07-02 13:23:03 +02:00
|
|
|
actor.makeRemote(RemoteServer.HOSTNAME, RemoteServer.PORT)
|
2009-06-25 23:47:30 +02:00
|
|
|
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-06-25 23:47:30 +02:00
|
|
|
}
|
2009-11-20 11:12:03 +01:00
|
|
|
actor.stop
|
2009-06-25 23:47:30 +02:00
|
|
|
}
|
|
|
|
|
}
|