2010-10-26 12:49:25 +02:00
|
|
|
package akka.actor.dispatch
|
2010-08-24 23:21:28 +02:00
|
|
|
|
2011-05-18 17:25:30 +02:00
|
|
|
import java.util.concurrent.{ CountDownLatch, TimeUnit }
|
2010-08-24 23:21:28 +02:00
|
|
|
import org.scalatest.junit.JUnitSuite
|
2011-06-20 15:24:39 -06:00
|
|
|
import org.junit.{ Test, Before, After }
|
2010-08-24 23:21:28 +02:00
|
|
|
|
2011-08-26 17:25:18 +02:00
|
|
|
import akka.actor.Actor._
|
2010-08-24 23:21:28 +02:00
|
|
|
|
2011-06-20 15:24:39 -06:00
|
|
|
import akka.event.EventHandler
|
|
|
|
|
import akka.testkit.TestEvent._
|
|
|
|
|
import akka.testkit.EventFilter
|
2011-08-26 17:25:18 +02:00
|
|
|
import akka.dispatch.{ PinnedDispatcher, Dispatchers }
|
|
|
|
|
import akka.actor.{ Props, Actor }
|
2011-06-20 15:24:39 -06:00
|
|
|
|
2011-06-13 14:59:22 +02:00
|
|
|
object PinnedActorSpec {
|
2010-08-24 23:21:28 +02:00
|
|
|
class TestActor extends Actor {
|
|
|
|
|
def receive = {
|
2011-08-26 17:25:18 +02:00
|
|
|
case "Hello" ⇒ self.reply("World")
|
|
|
|
|
case "Failure" ⇒ throw new RuntimeException("Expected exception; to test fault-tolerance")
|
2010-08-24 23:21:28 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-13 14:59:22 +02:00
|
|
|
class PinnedActorSpec extends JUnitSuite {
|
|
|
|
|
import PinnedActorSpec._
|
2010-08-24 23:21:28 +02:00
|
|
|
|
|
|
|
|
private val unit = TimeUnit.MILLISECONDS
|
|
|
|
|
|
2011-06-20 15:24:39 -06:00
|
|
|
@Before
|
|
|
|
|
def beforeEach {
|
|
|
|
|
EventHandler.notify(Mute(EventFilter[RuntimeException]("Failure")))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@After
|
|
|
|
|
def afterEach {
|
|
|
|
|
EventHandler.notify(UnMuteAll)
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-18 17:25:30 +02:00
|
|
|
@Test
|
2011-08-01 17:07:12 +02:00
|
|
|
def shouldTell {
|
2010-08-24 23:21:28 +02:00
|
|
|
var oneWay = new CountDownLatch(1)
|
2011-08-26 17:25:18 +02:00
|
|
|
val actor = actorOf(Props(self ⇒ { case "OneWay" ⇒ oneWay.countDown() }).withDispatcher(new PinnedDispatcher()))
|
2010-08-24 23:21:28 +02:00
|
|
|
val result = actor ! "OneWay"
|
|
|
|
|
assert(oneWay.await(1, TimeUnit.SECONDS))
|
2011-04-12 10:53:56 +02:00
|
|
|
actor.stop()
|
2010-08-24 23:21:28 +02:00
|
|
|
}
|
|
|
|
|
|
2011-05-18 17:25:30 +02:00
|
|
|
@Test
|
|
|
|
|
def shouldSendReplySync = {
|
2011-08-26 17:25:18 +02:00
|
|
|
val actor = actorOf(Props[TestActor].withDispatcher(new PinnedDispatcher()))
|
2011-06-14 14:26:13 +02:00
|
|
|
val result = (actor.?("Hello", 10000)).as[String]
|
2010-08-24 23:21:28 +02:00
|
|
|
assert("World" === result.get)
|
2011-04-12 10:53:56 +02:00
|
|
|
actor.stop()
|
2010-08-24 23:21:28 +02:00
|
|
|
}
|
|
|
|
|
|
2011-05-18 17:25:30 +02:00
|
|
|
@Test
|
|
|
|
|
def shouldSendReplyAsync = {
|
2011-08-26 17:25:18 +02:00
|
|
|
val actor = actorOf(Props[TestActor].withDispatcher(new PinnedDispatcher()))
|
2011-06-13 14:59:22 +02:00
|
|
|
val result = (actor ? "Hello").as[String]
|
|
|
|
|
assert("World" === result.get)
|
2011-04-12 10:53:56 +02:00
|
|
|
actor.stop()
|
2010-08-24 23:21:28 +02:00
|
|
|
}
|
|
|
|
|
|
2011-05-18 17:25:30 +02:00
|
|
|
@Test
|
|
|
|
|
def shouldSendReceiveException = {
|
2011-08-26 17:25:18 +02:00
|
|
|
val actor = actorOf(Props[TestActor].withDispatcher(new PinnedDispatcher()))
|
2010-08-24 23:21:28 +02:00
|
|
|
try {
|
2011-06-13 15:29:35 +02:00
|
|
|
(actor ? "Failure").get
|
2010-08-24 23:21:28 +02:00
|
|
|
fail("Should have thrown an exception")
|
|
|
|
|
} catch {
|
2011-05-18 17:25:30 +02:00
|
|
|
case e ⇒
|
2010-08-24 23:21:28 +02:00
|
|
|
assert("Expected exception; to test fault-tolerance" === e.getMessage())
|
|
|
|
|
}
|
2011-04-12 10:53:56 +02:00
|
|
|
actor.stop()
|
2010-08-24 23:21:28 +02:00
|
|
|
}
|
|
|
|
|
}
|