2009-06-25 23:47:30 +02:00
|
|
|
package se.scalablesolutions.akka.kernel.actor
|
|
|
|
|
|
|
|
|
|
import java.util.concurrent.TimeUnit
|
2009-06-30 16:01:50 +02:00
|
|
|
import junit.framework.TestCase
|
2009-07-01 15:29:06 +02:00
|
|
|
import kernel.nio.{RemoteServer, RemoteClient}
|
2009-06-25 23:47:30 +02:00
|
|
|
import org.junit.{Test, Before}
|
|
|
|
|
import org.junit.Assert._
|
|
|
|
|
|
|
|
|
|
object Global {
|
|
|
|
|
var oneWay = "nada"
|
|
|
|
|
}
|
|
|
|
|
class RemoteActorSpecActorUnidirectional extends Actor {
|
|
|
|
|
def receive: PartialFunction[Any, Unit] = {
|
|
|
|
|
case "OneWay" =>
|
|
|
|
|
Global.oneWay = "received"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class RemoteActorSpecActorBidirectional extends Actor {
|
|
|
|
|
def receive: PartialFunction[Any, Unit] = {
|
|
|
|
|
case "Hello" =>
|
|
|
|
|
reply("World")
|
|
|
|
|
case "Failure" =>
|
|
|
|
|
throw new RuntimeException("expected")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-06-30 16:01:50 +02:00
|
|
|
class RemoteActorSpec extends TestCase {
|
2009-06-25 23:47:30 +02:00
|
|
|
|
2009-07-03 17:15:36 +02:00
|
|
|
kernel.Kernel.config
|
2009-06-25 23:47:30 +02:00
|
|
|
new Thread(new Runnable() {
|
|
|
|
|
def run = {
|
2009-07-01 15:29:06 +02:00
|
|
|
val server = new RemoteServer
|
2009-07-02 13:23:03 +02:00
|
|
|
server.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-06-30 16:01:50 +02:00
|
|
|
def testSendOneWay = {
|
2009-06-25 23:47:30 +02:00
|
|
|
implicit val timeout = 5000L
|
|
|
|
|
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)
|
|
|
|
|
assertEquals("received", Global.oneWay)
|
|
|
|
|
actor.stop
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
2009-06-30 16:01:50 +02:00
|
|
|
def testSendReplySync = {
|
2009-06-25 23:47:30 +02:00
|
|
|
implicit val timeout = 5000L
|
|
|
|
|
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: String = actor !? "Hello"
|
|
|
|
|
assertEquals("World", result)
|
|
|
|
|
actor.stop
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
2009-06-30 16:01:50 +02:00
|
|
|
def testSendReplyAsync = {
|
2009-06-25 23:47:30 +02:00
|
|
|
implicit val timeout = 5000L
|
|
|
|
|
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"
|
|
|
|
|
assertEquals("World", result.get.asInstanceOf[String])
|
|
|
|
|
actor.stop
|
|
|
|
|
}
|
|
|
|
|
|
2009-06-30 16:01:50 +02:00
|
|
|
@Test
|
|
|
|
|
def testSendReceiveException = {
|
2009-06-25 23:47:30 +02:00
|
|
|
implicit val timeout = 5000L
|
|
|
|
|
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 =>
|
|
|
|
|
assertEquals("expected", e.getMessage())
|
|
|
|
|
}
|
|
|
|
|
actor.stop
|
|
|
|
|
}
|
|
|
|
|
}
|