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,
which is taken from one of the following locations in order of precedence:
#. explicitly given timeout as in ``actor.?("hello")(timeout = 12 millis)``
#. implicit argument of type :class:`akka.actor.Timeout`, e.g.
1. explicitly given timeout as in:
::
.. includecode:: code/akka/docs/actor/ActorDocSpec.scala#using-explicit-timeout
import akka.actor.Timeout
import akka.util.duration._
2. implicit argument of type :class:`akka.util.Timeout`, e.g.
implicit val timeout = Timeout(12 millis)
val future = actor ? "hello"
.. includecode:: code/akka/docs/actor/ActorDocSpec.scala#using-implicit-timeout
See :ref:`futures-scala` for more information on how to await or query a
future.

View file

@ -20,6 +20,8 @@ import org.scalatest.matchers.MustMatchers
import akka.testkit._
import akka.util._
import akka.util.duration._
import akka.actor.Actor.Receive
import akka.dispatch.Await
//#my-actor
class MyActor extends Actor {
@ -238,6 +240,27 @@ class ActorDocSpec extends AkkaSpec(Map("akka.loglevel" -> "INFO")) {
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 {
//#receive-timeout
import akka.actor.ReceiveTimeout