=typ #24725 disallow deferred behaviors as postStop hooks (#25123)

This commit is contained in:
Konrad `ktoso` Malawski 2018-07-19 10:26:01 +09:00 committed by GitHub
parent 578f32d322
commit f4fbbf9312
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 3 deletions

View file

@ -5,8 +5,10 @@
package akka.actor.typed.scaladsl
import akka.Done
import akka.actor.typed.{ PostStop, TypedAkkaSpecWithShutdown }
import akka.actor.testkit.typed.scaladsl.ActorTestKit
import akka.actor.testkit.typed.TE
import akka.actor.testkit.typed.scaladsl.{ ActorTestKit, TestProbe }
import akka.actor.typed.{ Behavior, PostStop, SupervisorStrategy, Terminated, TypedAkkaSpecWithShutdown }
import akka.testkit.EventFilter
import scala.concurrent.Promise
@ -58,4 +60,19 @@ class StopSpec extends ActorTestKit with TypedAkkaSpecWithShutdown {
}
"PostStop" should {
"immediately throw when a deferred behavior (setup) is passed in as postStop" in {
val ex = intercept[IllegalArgumentException] {
Behaviors.stopped(
// illegal:
Behaviors.setup[String] { _
throw TE("boom!")
}
)
}
ex.getMessage should include("Behavior used as `postStop` behavior in Stopped(...) was a deferred one ")
}
}
}

View file

@ -233,7 +233,23 @@ object Behavior {
* that PostStop can be sent to previous behavior from `finishTerminate`.
*/
private[akka] class StoppedBehavior[T](val postStop: OptionVal[Behavior[T]]) extends Behavior[T] {
override def toString = "Stopped"
validatePostStop(postStop)
@throws[IllegalArgumentException]
private final def validatePostStop(postStop: OptionVal[Behavior[T]]): Unit = {
postStop match {
case OptionVal.Some(b: DeferredBehavior[_])
throw new IllegalArgumentException(s"Behavior used as `postStop` behavior in Stopped(...) was a deferred one [${b.toString}], which is not supported (it would never be evaluated).")
case _ // all good
}
}
override def toString = "Stopped" + {
postStop match {
case OptionVal.Some(_) "(postStop)"
case _ "()"
}
}
}
/**