2011-10-10 11:12:34 +02:00
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2009-2011 Typesafe Inc. <http://www.typesafe.com>
|
|
|
|
|
*/
|
|
|
|
|
package akka.testkit
|
|
|
|
|
|
2011-11-16 19:34:37 +01:00
|
|
|
import org.scalatest.{ WordSpec, BeforeAndAfterAll, Tag }
|
2011-10-10 11:12:34 +02:00
|
|
|
import org.scalatest.matchers.MustMatchers
|
2011-11-16 17:18:36 +01:00
|
|
|
import akka.actor.{ ActorSystem, ActorSystemImpl }
|
2011-10-10 15:45:55 +02:00
|
|
|
import akka.actor.{ Actor, ActorRef, Props }
|
2011-10-11 16:05:48 +02:00
|
|
|
import akka.dispatch.MessageDispatcher
|
2011-11-03 18:56:20 +01:00
|
|
|
import akka.event.{ Logging, LoggingAdapter }
|
2011-10-28 17:15:10 +02:00
|
|
|
import akka.util.duration._
|
|
|
|
|
import akka.dispatch.FutureTimeoutException
|
2011-11-15 11:34:39 +01:00
|
|
|
import com.typesafe.config.Config
|
|
|
|
|
import com.typesafe.config.ConfigFactory
|
2011-12-05 15:18:22 +01:00
|
|
|
import akka.actor.PoisonPill
|
2011-12-05 18:52:32 +01:00
|
|
|
import java.util.concurrent.LinkedBlockingQueue
|
|
|
|
|
import akka.actor.CreateChild
|
|
|
|
|
import akka.actor.DeadLetter
|
2011-10-10 11:12:34 +02:00
|
|
|
|
2011-11-16 19:34:37 +01:00
|
|
|
object TimingTest extends Tag("timing")
|
|
|
|
|
|
2011-11-15 11:34:39 +01:00
|
|
|
object AkkaSpec {
|
2011-11-29 11:50:22 +01:00
|
|
|
val testConf = {
|
|
|
|
|
val cfg = ConfigFactory.parseString("""
|
2011-11-15 11:34:39 +01:00
|
|
|
akka {
|
|
|
|
|
event-handlers = ["akka.testkit.TestEventListener"]
|
|
|
|
|
loglevel = "WARNING"
|
2011-11-28 14:59:41 +01:00
|
|
|
stdout-loglevel = "WARNING"
|
2011-11-15 11:34:39 +01:00
|
|
|
actor {
|
|
|
|
|
default-dispatcher {
|
2011-11-30 15:16:20 +01:00
|
|
|
core-pool-size-factor = 2
|
2011-11-15 11:34:39 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-11-29 11:50:22 +01:00
|
|
|
""")
|
|
|
|
|
ConfigFactory.load(cfg)
|
|
|
|
|
}
|
2011-11-15 11:34:39 +01:00
|
|
|
|
|
|
|
|
def mapToConfig(map: Map[String, Any]): Config = {
|
|
|
|
|
import scala.collection.JavaConverters._
|
|
|
|
|
ConfigFactory.parseMap(map.asJava)
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-30 22:19:48 +01:00
|
|
|
def getCallerName: String = {
|
|
|
|
|
val s = Thread.currentThread.getStackTrace map (_.getClassName) drop 1 dropWhile (_ matches ".*AkkaSpec.?$")
|
|
|
|
|
s.head.replaceFirst(""".*\.""", "").replaceAll("[^a-zA-Z_0-9]", "_")
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-15 11:34:39 +01:00
|
|
|
}
|
|
|
|
|
|
2011-11-30 22:19:48 +01:00
|
|
|
abstract class AkkaSpec(_system: ActorSystem)
|
2011-11-22 13:04:10 +01:00
|
|
|
extends TestKit(_system) with WordSpec with MustMatchers with BeforeAndAfterAll {
|
2011-10-20 20:45:02 +02:00
|
|
|
|
2011-11-30 22:19:48 +01:00
|
|
|
def this(config: Config) = this(ActorSystem(AkkaSpec.getCallerName, config.withFallback(AkkaSpec.testConf)))
|
|
|
|
|
|
2011-12-02 13:31:48 +01:00
|
|
|
def this(s: String) = this(ConfigFactory.parseString(s))
|
2011-11-30 22:19:48 +01:00
|
|
|
|
|
|
|
|
def this(configMap: Map[String, _]) = this(AkkaSpec.mapToConfig(configMap))
|
|
|
|
|
|
|
|
|
|
def this() = this(ActorSystem(AkkaSpec.getCallerName, AkkaSpec.testConf))
|
|
|
|
|
|
2011-11-18 11:59:43 +01:00
|
|
|
val log: LoggingAdapter = Logging(system, this.getClass)
|
2011-10-27 12:23:01 +02:00
|
|
|
|
2011-10-20 20:45:02 +02:00
|
|
|
final override def beforeAll {
|
|
|
|
|
atStartup()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final override def afterAll {
|
2011-11-16 17:18:36 +01:00
|
|
|
system.stop()
|
|
|
|
|
try system.asInstanceOf[ActorSystemImpl].terminationFuture.await(5 seconds) catch {
|
2011-11-30 15:16:20 +01:00
|
|
|
case _: FutureTimeoutException ⇒ system.log.warning("Failed to stop [{}] within 5 seconds", system.name)
|
2011-10-28 17:15:10 +02:00
|
|
|
}
|
2011-10-20 20:45:02 +02:00
|
|
|
atTermination()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected def atStartup() {}
|
|
|
|
|
|
|
|
|
|
protected def atTermination() {}
|
2011-10-10 11:12:34 +02:00
|
|
|
|
2011-11-16 17:18:36 +01:00
|
|
|
def actorOf(props: Props): ActorRef = system.actorOf(props)
|
2011-10-10 15:45:55 +02:00
|
|
|
|
2011-10-18 17:56:23 +02:00
|
|
|
def actorOf[T <: Actor](clazz: Class[T]): ActorRef = actorOf(Props(clazz))
|
2011-10-10 15:45:55 +02:00
|
|
|
|
2011-10-18 17:56:23 +02:00
|
|
|
def actorOf[T <: Actor: Manifest]: ActorRef = actorOf(manifest[T].erasure.asInstanceOf[Class[_ <: Actor]])
|
2011-10-10 15:45:55 +02:00
|
|
|
|
2011-10-18 17:56:23 +02:00
|
|
|
def actorOf[T <: Actor](factory: ⇒ T): ActorRef = actorOf(Props(factory))
|
2011-10-10 15:45:55 +02:00
|
|
|
|
2011-10-11 16:05:48 +02:00
|
|
|
def spawn(body: ⇒ Unit)(implicit dispatcher: MessageDispatcher) {
|
2011-10-18 17:56:23 +02:00
|
|
|
actorOf(Props(ctx ⇒ { case "go" ⇒ try body finally ctx.self.stop() }).withDispatcher(dispatcher)) ! "go"
|
2011-10-11 16:05:48 +02:00
|
|
|
}
|
2011-10-28 17:15:10 +02:00
|
|
|
}
|
|
|
|
|
|
2011-11-03 18:56:20 +01:00
|
|
|
@org.junit.runner.RunWith(classOf[org.scalatest.junit.JUnitRunner])
|
2011-10-28 17:15:10 +02:00
|
|
|
class AkkaSpecSpec extends WordSpec with MustMatchers {
|
2011-12-05 15:18:22 +01:00
|
|
|
|
2011-10-28 17:15:10 +02:00
|
|
|
"An AkkaSpec" must {
|
2011-12-05 15:18:22 +01:00
|
|
|
|
2011-10-28 17:15:10 +02:00
|
|
|
"terminate all actors" in {
|
2011-12-05 18:52:32 +01:00
|
|
|
// verbose config just for demonstration purposes, please leave in in case of debugging
|
2011-11-15 11:34:39 +01:00
|
|
|
import scala.collection.JavaConverters._
|
|
|
|
|
val conf = Map(
|
2011-11-12 22:37:12 +01:00
|
|
|
"akka.actor.debug.lifecycle" -> true, "akka.actor.debug.event-stream" -> true,
|
2011-11-15 11:34:39 +01:00
|
|
|
"akka.loglevel" -> "DEBUG", "akka.stdout-loglevel" -> "DEBUG")
|
2011-12-05 18:52:32 +01:00
|
|
|
val system = ActorSystem("AkkaSpec1", ConfigFactory.parseMap(conf.asJava).withFallback(AkkaSpec.testConf))
|
2011-11-17 12:36:35 +01:00
|
|
|
val spec = new AkkaSpec(system) {
|
|
|
|
|
val ref = Seq(testActor, system.actorOf(Props.empty, "name"))
|
2011-10-28 17:15:10 +02:00
|
|
|
}
|
2011-11-23 19:03:56 +01:00
|
|
|
spec.ref foreach (_.isTerminated must not be true)
|
2011-11-17 12:36:35 +01:00
|
|
|
system.stop()
|
2011-11-23 19:03:56 +01:00
|
|
|
spec.awaitCond(spec.ref forall (_.isTerminated), 2 seconds)
|
2011-10-28 17:15:10 +02:00
|
|
|
}
|
2011-12-05 15:18:22 +01:00
|
|
|
|
|
|
|
|
"must stop correctly when sending PoisonPill to rootGuardian" in {
|
2011-12-05 18:52:32 +01:00
|
|
|
val system = ActorSystem("AkkaSpec2", AkkaSpec.testConf)
|
2011-12-05 15:18:22 +01:00
|
|
|
val spec = new AkkaSpec(system) {}
|
|
|
|
|
val latch = new TestLatch(1)(system)
|
|
|
|
|
system.registerOnTermination(latch.countDown())
|
|
|
|
|
|
|
|
|
|
system.actorFor("/") ! PoisonPill
|
|
|
|
|
|
|
|
|
|
latch.await(2 seconds)
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-05 18:52:32 +01:00
|
|
|
"must enqueue unread messages from testActor to deadLetters" in {
|
|
|
|
|
val system = ActorSystem("AkkaSpec2", AkkaSpec.testConf)
|
|
|
|
|
|
|
|
|
|
var locker = Seq.empty[DeadLetter]
|
|
|
|
|
implicit val timeout = system.settings.ActorTimeout
|
2011-12-05 21:03:05 +01:00
|
|
|
implicit val davyJones = (system.actorFor("/") ? CreateChild(Props(new Actor {
|
2011-12-05 18:52:32 +01:00
|
|
|
def receive = {
|
|
|
|
|
case m: DeadLetter ⇒ locker :+= m
|
|
|
|
|
}
|
2011-12-05 21:03:05 +01:00
|
|
|
}), "davyJones")).as[ActorRef].get
|
2011-12-05 18:52:32 +01:00
|
|
|
|
2011-12-05 21:03:05 +01:00
|
|
|
system.eventStream.subscribe(davyJones, classOf[DeadLetter])
|
2011-12-05 18:52:32 +01:00
|
|
|
|
|
|
|
|
val probe = new TestProbe(system)
|
|
|
|
|
probe.ref ! 42
|
|
|
|
|
|
|
|
|
|
val latch = new TestLatch(1)(system)
|
|
|
|
|
system.registerOnTermination(latch.countDown())
|
|
|
|
|
system.stop()
|
|
|
|
|
latch.await(2 seconds)
|
|
|
|
|
|
|
|
|
|
// this will typically also contain log messages which were sent after the logger shutdown
|
2011-12-05 21:03:05 +01:00
|
|
|
locker must contain(DeadLetter(42, davyJones, probe.ref))
|
2011-12-05 18:52:32 +01:00
|
|
|
}
|
|
|
|
|
|
2011-10-28 17:15:10 +02:00
|
|
|
}
|
2011-11-09 14:56:05 +01:00
|
|
|
}
|
|
|
|
|
|