DOC: Extracted sample for explicit and implicit timeout with ask. Correction of akka.util.Timeout

This commit is contained in:
Patrik Nordwall 2011-12-20 08:12:20 +01:00
parent 8da41aaef9
commit e82ea3c8b0
2 changed files with 27 additions and 7 deletions

View file

@ -328,16 +328,13 @@ message.
If the actor does not complete the future, it will expire after the timeout period, If the actor does not complete the future, it will expire after the timeout period,
which is taken from one of the following locations in order of precedence: which is taken from one of the following locations in order of precedence:
#. explicitly given timeout as in ``actor.?("hello")(timeout = 12 millis)`` 1. explicitly given timeout as in:
#. implicit argument of type :class:`akka.actor.Timeout`, e.g.
:: .. includecode:: code/akka/docs/actor/ActorDocSpec.scala#using-explicit-timeout
import akka.actor.Timeout 2. implicit argument of type :class:`akka.util.Timeout`, e.g.
import akka.util.duration._
implicit val timeout = Timeout(12 millis) .. includecode:: code/akka/docs/actor/ActorDocSpec.scala#using-implicit-timeout
val future = actor ? "hello"
See :ref:`futures-scala` for more information on how to await or query a See :ref:`futures-scala` for more information on how to await or query a
future. future.

View file

@ -20,6 +20,8 @@ import org.scalatest.matchers.MustMatchers
import akka.testkit._ import akka.testkit._
import akka.util._ import akka.util._
import akka.util.duration._ import akka.util.duration._
import akka.actor.Actor.Receive
import akka.dispatch.Await
//#my-actor //#my-actor
class MyActor extends Actor { class MyActor extends Actor {
@ -238,6 +240,27 @@ class ActorDocSpec extends AkkaSpec(Map("akka.loglevel" -> "INFO")) {
system.stop(myActor) system.stop(myActor)
} }
"using implicit timeout" in {
val myActor = system.actorOf(Props(new FirstActor))
//#using-implicit-timeout
import akka.util.duration._
import akka.util.Timeout
implicit val timeout = Timeout(500 millis)
val future = myActor ? "hello"
//#using-implicit-timeout
Await.result(future, timeout.duration) must be("hello")
}
"using explicit timeout" in {
val myActor = system.actorOf(Props(new FirstActor))
//#using-explicit-timeout
import akka.util.duration._
val future = myActor ? ("hello", timeout = 500 millis)
//#using-explicit-timeout
Await.result(future, 500 millis) must be("hello")
}
"using receiveTimeout" in { "using receiveTimeout" in {
//#receive-timeout //#receive-timeout
import akka.actor.ReceiveTimeout import akka.actor.ReceiveTimeout