pekko/akka-actor-tests/src/test/scala/akka/pattern/AskSpec.scala

83 lines
2.8 KiB
Scala
Raw Normal View History

/**
2013-01-09 01:47:48 +01:00
* Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
*/
package akka.pattern
import language.postfixOps
import akka.actor._
import akka.testkit.AkkaSpec
import akka.util.Timeout
import scala.concurrent.Await
import scala.concurrent.duration._
2012-08-20 15:21:44 +02:00
import scala.util.Failure
class AskSpec extends AkkaSpec {
"The “ask” pattern" must {
"return broken promises on DeadLetters" in {
implicit val timeout = Timeout(5 seconds)
val dead = system.actorFor("/system/deadLetters")
val f = dead.ask(42)(1 second)
f.isCompleted must be(true)
f.value.get match {
2012-08-20 15:21:44 +02:00
case Failure(_: AskTimeoutException)
case v fail(v + " was not Left(AskTimeoutException)")
}
}
"return broken promises on EmptyLocalActorRefs" in {
implicit val timeout = Timeout(5 seconds)
val empty = system.actorFor("unknown")
val f = empty ? 3.14
f.isCompleted must be(true)
f.value.get match {
2012-08-20 15:21:44 +02:00
case Failure(_: AskTimeoutException)
case v fail(v + " was not Left(AskTimeoutException)")
}
}
"return broken promises on unsupported ActorRefs" in {
implicit val timeout = Timeout(5 seconds)
val f = ask(null: ActorRef, 3.14)
f.isCompleted must be(true)
intercept[IllegalArgumentException] {
Await.result(f, remaining)
}.getMessage must be === "Unsupported recipient ActorRef type, question not sent to [null]"
}
"return broken promises on 0 timeout" in {
implicit val timeout = Timeout(0 seconds)
val echo = system.actorOf(Props(new Actor { def receive = { case x sender ! x } }))
val f = echo ? "foo"
val expectedMsg = "Timeout length must not be negative, question not sent to [%s]" format echo
intercept[IllegalArgumentException] {
Await.result(f, remaining)
}.getMessage must be === expectedMsg
}
"return broken promises on < 0 timeout" in {
implicit val timeout = Timeout(-1000 seconds)
val echo = system.actorOf(Props(new Actor { def receive = { case x sender ! x } }))
val f = echo ? "foo"
val expectedMsg = "Timeout length must not be negative, question not sent to [%s]" format echo
intercept[IllegalArgumentException] {
Await.result(f, remaining)
}.getMessage must be === expectedMsg
}
"work for ActorSelection" in {
implicit val timeout = Timeout(5 seconds)
import system.dispatcher
val echo = system.actorOf(Props(new Actor { def receive = { case x sender ! x } }), "select-echo")
val identityFuture = (system.actorSelection("/user/select-echo") ? Identify(None))
.mapTo[ActorIdentity].map(_.ref.get)
Await.result(identityFuture, 5 seconds) must be === echo
}
}
2013-01-09 01:47:48 +01:00
}