2018-10-29 17:19:37 +08:00
|
|
|
/*
|
2019-01-02 18:55:26 +08:00
|
|
|
* Copyright (C) 2009-2019 Lightbend Inc. <https://www.lightbend.com>
|
2012-01-01 20:48:03 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package akka.pattern
|
|
|
|
|
|
2012-06-21 16:09:14 +02:00
|
|
|
import language.postfixOps
|
|
|
|
|
|
2012-12-18 14:31:18 +01:00
|
|
|
import akka.testkit.{ TestLatch, AkkaSpec }
|
2012-06-29 16:06:26 +02:00
|
|
|
import akka.actor.{ Props, Actor }
|
2012-07-04 15:25:30 +02:00
|
|
|
import scala.concurrent.{ Future, Promise, Await }
|
2012-09-21 14:50:06 +02:00
|
|
|
import scala.concurrent.duration._
|
2012-01-01 20:48:03 +01:00
|
|
|
|
|
|
|
|
object PatternSpec {
|
2014-03-07 13:20:01 +01:00
|
|
|
final case class Work(duration: Duration)
|
2012-01-01 20:48:03 +01:00
|
|
|
class TargetActor extends Actor {
|
|
|
|
|
def receive = {
|
2019-02-09 15:25:39 +01:00
|
|
|
case (testLatch: TestLatch, duration: FiniteDuration) =>
|
2012-12-18 14:31:18 +01:00
|
|
|
Await.ready(testLatch, duration)
|
2012-01-01 20:48:03 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-23 14:39:21 +02:00
|
|
|
class PatternSpec extends AkkaSpec("akka.actor.serialize-messages = off") {
|
2012-07-17 17:21:08 +02:00
|
|
|
implicit val ec = system.dispatcher
|
2012-01-01 20:48:03 +01:00
|
|
|
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)
|
2015-01-16 11:09:59 +01:00
|
|
|
Await.result(result, 6 seconds) should ===(true)
|
2012-01-01 20:48:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"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-12-18 14:31:18 +01: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-12-18 14:31:18 +01:00
|
|
|
val latch = TestLatch()
|
2014-03-11 11:23:12 +01:00
|
|
|
target ! ((latch, remainingOrDefault))
|
|
|
|
|
intercept[AskTimeoutException] { Await.result(gracefulStop(target, 500 millis), remainingOrDefault) }
|
2012-12-18 14:31:18 +01:00
|
|
|
latch.open()
|
2012-01-01 20:48:03 +01:00
|
|
|
}
|
|
|
|
|
}
|
2012-04-25 18:34:16 +02:00
|
|
|
|
|
|
|
|
"pattern.after" must {
|
|
|
|
|
"be completed successfully eventually" in {
|
2013-12-03 09:18:26 +01:00
|
|
|
// TODO after is unfortunately shadowed by ScalaTest, fix as part of #3759
|
2017-12-03 16:45:38 +03:00
|
|
|
val f = akka.pattern.after(1 second, using = system.scheduler)(Future.successful(5))
|
2012-04-25 18:34:16 +02:00
|
|
|
|
2012-07-04 15:25:30 +02:00
|
|
|
val r = Future.firstCompletedOf(Seq(Promise[Int]().future, f))
|
2015-01-16 11:09:59 +01:00
|
|
|
Await.result(r, remainingOrDefault) should ===(5)
|
2012-04-25 18:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"be completed abnormally eventually" in {
|
2013-12-03 09:18:26 +01:00
|
|
|
// TODO after is unfortunately shadowed by ScalaTest, fix as part of #3759
|
2017-12-03 16:45:38 +03:00
|
|
|
val f = akka.pattern.after(1 second, using = system.scheduler)(Future.failed(new IllegalStateException("Mexico")))
|
2012-04-25 18:34:16 +02:00
|
|
|
|
2012-07-04 15:25:30 +02:00
|
|
|
val r = Future.firstCompletedOf(Seq(Promise[Int]().future, f))
|
2015-01-16 11:09:59 +01:00
|
|
|
intercept[IllegalStateException] { Await.result(r, remainingOrDefault) }.getMessage should ===("Mexico")
|
2012-04-25 18:34:16 +02:00
|
|
|
}
|
|
|
|
|
}
|
2012-01-01 20:48:03 +01:00
|
|
|
}
|