pekko/akka-actor-tests/src/test/scala/akka/dispatch/PinnedActorSpec.scala

77 lines
2 KiB
Scala
Raw Normal View History

package akka.actor.dispatch
import java.util.concurrent.{ CountDownLatch, TimeUnit }
import org.scalatest.junit.JUnitSuite
2011-06-20 15:24:39 -06:00
import org.junit.{ Test, Before, After }
2011-08-26 17:25:18 +02:00
import akka.actor.Actor._
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
object PinnedActorSpec {
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")
}
}
}
class PinnedActorSpec extends JUnitSuite {
import PinnedActorSpec._
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)
}
@Test
def shouldTell {
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()))
val result = actor ! "OneWay"
assert(oneWay.await(1, TimeUnit.SECONDS))
2011-04-12 10:53:56 +02:00
actor.stop()
}
@Test
def shouldSendReplySync = {
2011-08-26 17:25:18 +02:00
val actor = actorOf(Props[TestActor].withDispatcher(new PinnedDispatcher()))
val result = (actor.?("Hello", 10000)).as[String]
assert("World" === result.get)
2011-04-12 10:53:56 +02:00
actor.stop()
}
@Test
def shouldSendReplyAsync = {
2011-08-26 17:25:18 +02:00
val actor = actorOf(Props[TestActor].withDispatcher(new PinnedDispatcher()))
val result = (actor ? "Hello").as[String]
assert("World" === result.get)
2011-04-12 10:53:56 +02:00
actor.stop()
}
@Test
def shouldSendReceiveException = {
2011-08-26 17:25:18 +02:00
val actor = actorOf(Props[TestActor].withDispatcher(new PinnedDispatcher()))
try {
(actor ? "Failure").get
fail("Should have thrown an exception")
} catch {
case e
assert("Expected exception; to test fault-tolerance" === e.getMessage())
}
2011-04-12 10:53:56 +02:00
actor.stop()
}
}