2012-01-19 15:13:10 +01:00
|
|
|
/**
|
2013-01-09 01:47:48 +01:00
|
|
|
* Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
|
2012-01-19 15:13:10 +01:00
|
|
|
*/
|
|
|
|
|
package akka.pattern
|
|
|
|
|
|
2012-06-21 16:09:14 +02:00
|
|
|
import language.postfixOps
|
|
|
|
|
|
2013-07-15 15:43:22 +02:00
|
|
|
import akka.actor._
|
2012-01-19 15:13:10 +01:00
|
|
|
import akka.testkit.AkkaSpec
|
2012-07-23 13:52:48 +02:00
|
|
|
import akka.util.Timeout
|
2013-07-15 15:43:22 +02:00
|
|
|
import scala.concurrent.Await
|
|
|
|
|
import scala.concurrent.duration._
|
2012-08-20 15:21:44 +02:00
|
|
|
import scala.util.Failure
|
2012-01-19 15:13:10 +01:00
|
|
|
|
2012-07-23 13:52:48 +02:00
|
|
|
class AskSpec extends AkkaSpec {
|
2012-01-19 15:13:10 +01:00
|
|
|
|
|
|
|
|
"The “ask” pattern" must {
|
|
|
|
|
|
|
|
|
|
"return broken promises on DeadLetters" in {
|
2012-07-23 13:52:48 +02:00
|
|
|
implicit val timeout = Timeout(5 seconds)
|
2012-01-19 15:13:10 +01:00
|
|
|
val dead = system.actorFor("/system/deadLetters")
|
2012-01-23 15:59:18 +01:00
|
|
|
val f = dead.ask(42)(1 second)
|
2013-12-17 14:25:56 +01:00
|
|
|
f.isCompleted should be(true)
|
2012-01-19 15:13:10 +01:00
|
|
|
f.value.get match {
|
2012-08-20 15:21:44 +02:00
|
|
|
case Failure(_: AskTimeoutException) ⇒
|
|
|
|
|
case v ⇒ fail(v + " was not Left(AskTimeoutException)")
|
2012-01-19 15:13:10 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"return broken promises on EmptyLocalActorRefs" in {
|
2012-07-23 13:52:48 +02:00
|
|
|
implicit val timeout = Timeout(5 seconds)
|
2012-01-19 15:13:10 +01:00
|
|
|
val empty = system.actorFor("unknown")
|
|
|
|
|
val f = empty ? 3.14
|
2013-12-17 14:25:56 +01:00
|
|
|
f.isCompleted should be(true)
|
2012-01-19 15:13:10 +01:00
|
|
|
f.value.get match {
|
2012-08-20 15:21:44 +02:00
|
|
|
case Failure(_: AskTimeoutException) ⇒
|
|
|
|
|
case v ⇒ fail(v + " was not Left(AskTimeoutException)")
|
2012-01-19 15:13:10 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-23 13:52:48 +02:00
|
|
|
"return broken promises on unsupported ActorRefs" in {
|
|
|
|
|
implicit val timeout = Timeout(5 seconds)
|
|
|
|
|
val f = ask(null: ActorRef, 3.14)
|
2013-12-17 14:25:56 +01:00
|
|
|
f.isCompleted should be(true)
|
2012-07-23 13:52:48 +02:00
|
|
|
intercept[IllegalArgumentException] {
|
|
|
|
|
Await.result(f, remaining)
|
2013-12-17 14:25:56 +01:00
|
|
|
}.getMessage should equal("Unsupported recipient ActorRef type, question not sent to [null]")
|
2012-07-23 13:52:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"return broken promises on 0 timeout" in {
|
|
|
|
|
implicit val timeout = Timeout(0 seconds)
|
2012-08-29 18:00:14 +02:00
|
|
|
val echo = system.actorOf(Props(new Actor { def receive = { case x ⇒ sender ! x } }))
|
2012-07-23 13:52:48 +02:00
|
|
|
val f = echo ? "foo"
|
2013-04-29 22:01:14 +02:00
|
|
|
val expectedMsg = "Timeout length must not be negative, question not sent to [%s]" format echo
|
2012-07-23 13:52:48 +02:00
|
|
|
intercept[IllegalArgumentException] {
|
|
|
|
|
Await.result(f, remaining)
|
2013-12-17 14:25:56 +01:00
|
|
|
}.getMessage should equal(expectedMsg)
|
2012-07-23 13:52:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"return broken promises on < 0 timeout" in {
|
|
|
|
|
implicit val timeout = Timeout(-1000 seconds)
|
2012-08-29 18:00:14 +02:00
|
|
|
val echo = system.actorOf(Props(new Actor { def receive = { case x ⇒ sender ! x } }))
|
2012-07-23 13:52:48 +02:00
|
|
|
val f = echo ? "foo"
|
2013-04-29 22:01:14 +02:00
|
|
|
val expectedMsg = "Timeout length must not be negative, question not sent to [%s]" format echo
|
2012-07-23 13:52:48 +02:00
|
|
|
intercept[IllegalArgumentException] {
|
|
|
|
|
Await.result(f, remaining)
|
2013-12-17 14:25:56 +01:00
|
|
|
}.getMessage should equal(expectedMsg)
|
2012-07-23 13:52:48 +02:00
|
|
|
}
|
|
|
|
|
|
2013-10-16 14:47:17 +02:00
|
|
|
"include target information in AskTimeout" in {
|
|
|
|
|
implicit val timeout = Timeout(0.5 seconds)
|
|
|
|
|
val silentOne = system.actorOf(Props.empty, "silent")
|
|
|
|
|
val f = silentOne ? "noreply"
|
|
|
|
|
intercept[AskTimeoutException] {
|
|
|
|
|
Await.result(f, remaining)
|
2013-12-17 14:25:56 +01:00
|
|
|
}.getMessage.contains("/user/silent") should be(true)
|
2013-10-16 14:47:17 +02:00
|
|
|
}
|
|
|
|
|
|
2013-12-18 17:30:19 -05:00
|
|
|
"include timeout information in AskTimeout" in {
|
|
|
|
|
implicit val timeout = Timeout(0.5 seconds)
|
|
|
|
|
val f = system.actorOf(Props.empty) ? "noreply"
|
|
|
|
|
intercept[AskTimeoutException] {
|
|
|
|
|
Await.result(f, remaining)
|
|
|
|
|
}.getMessage should include(timeout.duration.toMillis.toString)
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-15 15:43:22 +02:00
|
|
|
"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)
|
|
|
|
|
|
2013-12-17 14:25:56 +01:00
|
|
|
Await.result(identityFuture, 5 seconds) should equal(echo)
|
2013-07-15 15:43:22 +02:00
|
|
|
}
|
|
|
|
|
|
2012-01-19 15:13:10 +01:00
|
|
|
}
|
|
|
|
|
|
2013-01-09 01:47:48 +01:00
|
|
|
}
|