2012-01-01 20:48:03 +01:00
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package akka.pattern
|
|
|
|
|
|
2012-06-21 16:09:14 +02:00
|
|
|
import language.postfixOps
|
|
|
|
|
|
2012-01-01 20:48:03 +01:00
|
|
|
import akka.testkit.AkkaSpec
|
2012-06-29 16:06:26 +02:00
|
|
|
import akka.actor.{ Props, Actor }
|
|
|
|
|
import scala.concurrent.Await
|
2012-06-29 13:33:20 +02:00
|
|
|
import scala.concurrent.util.Duration
|
|
|
|
|
import scala.concurrent.util.duration._
|
2012-06-29 16:06:26 +02:00
|
|
|
import akka.dispatch.{ Future, Promise }
|
2012-01-01 20:48:03 +01:00
|
|
|
|
|
|
|
|
object PatternSpec {
|
|
|
|
|
case class Work(duration: Duration)
|
|
|
|
|
class TargetActor extends Actor {
|
|
|
|
|
def receive = {
|
2012-06-29 13:33:20 +02:00
|
|
|
case Work(duration) ⇒ Thread.sleep(duration.toMillis)
|
2012-01-01 20:48:03 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@org.junit.runner.RunWith(classOf[org.scalatest.junit.JUnitRunner])
|
|
|
|
|
class PatternSpec extends AkkaSpec {
|
|
|
|
|
|
|
|
|
|
import PatternSpec._
|
|
|
|
|
|
2012-01-01 21:27:52 +01:00
|
|
|
"pattern.gracefulStop" must {
|
2012-01-01 20:48:03 +01:00
|
|
|
|
|
|
|
|
"provide Future for stopping an actor" in {
|
|
|
|
|
val target = system.actorOf(Props[TargetActor])
|
|
|
|
|
val result = gracefulStop(target, 5 seconds)
|
|
|
|
|
Await.result(result, 6 seconds) must be(true)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"complete Future when actor already terminated" in {
|
|
|
|
|
val target = system.actorOf(Props[TargetActor])
|
|
|
|
|
Await.ready(gracefulStop(target, 5 seconds), 6 seconds)
|
|
|
|
|
Await.ready(gracefulStop(target, 1 millis), 1 second)
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-18 14:55:38 +02:00
|
|
|
"complete Future with AskTimeoutException when actor not terminated within timeout" in {
|
2012-01-01 20:48:03 +01:00
|
|
|
val target = system.actorOf(Props[TargetActor])
|
2012-01-01 21:27:52 +01:00
|
|
|
target ! Work(250 millis)
|
2012-05-18 14:55:38 +02:00
|
|
|
intercept[AskTimeoutException] { Await.result(gracefulStop(target, 10 millis), 200 millis) }
|
2012-01-01 20:48:03 +01:00
|
|
|
}
|
|
|
|
|
}
|
2012-04-25 18:34:16 +02:00
|
|
|
|
|
|
|
|
"pattern.after" must {
|
|
|
|
|
"be completed successfully eventually" in {
|
|
|
|
|
val f = after(1 second, using = system.scheduler)(Promise.successful(5))
|
|
|
|
|
|
|
|
|
|
val r = Future.firstCompletedOf(Seq(Promise[Int](), f))
|
|
|
|
|
Await.result(r, remaining) must be(5)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"be completed abnormally eventually" in {
|
|
|
|
|
val f = after(1 second, using = system.scheduler)(Promise.failed(new IllegalStateException("Mexico")))
|
|
|
|
|
|
|
|
|
|
val r = Future.firstCompletedOf(Seq(Promise[Int](), f))
|
|
|
|
|
intercept[IllegalStateException] { Await.result(r, remaining) }.getMessage must be("Mexico")
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-01-01 20:48:03 +01:00
|
|
|
}
|