2011-10-10 11:12:34 +02:00
|
|
|
/**
|
2013-01-09 01:47:48 +01:00
|
|
|
* Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
|
2011-10-10 11:12:34 +02:00
|
|
|
*/
|
|
|
|
|
package akka.testkit
|
|
|
|
|
|
2012-06-21 16:09:14 +02:00
|
|
|
import language.{ postfixOps, reflectiveCalls }
|
|
|
|
|
|
2013-12-03 09:18:26 +01:00
|
|
|
import org.scalatest.{ WordSpecLike, BeforeAndAfterAll }
|
2011-10-10 11:12:34 +02:00
|
|
|
import org.scalatest.matchers.MustMatchers
|
2013-12-03 09:18:26 +01:00
|
|
|
import akka.actor.ActorSystem
|
2011-11-03 18:56:20 +01:00
|
|
|
import akka.event.{ Logging, LoggingAdapter }
|
2012-09-21 14:50:06 +02:00
|
|
|
import scala.concurrent.duration._
|
2013-12-03 09:18:26 +01:00
|
|
|
import scala.concurrent.Future
|
2012-06-29 16:06:26 +02:00
|
|
|
import com.typesafe.config.{ Config, ConfigFactory }
|
2012-08-29 18:00:14 +02:00
|
|
|
import akka.dispatch.Dispatchers
|
2013-04-22 20:39:32 +02:00
|
|
|
import akka.testkit.TestEvent._
|
2011-10-10 11:12:34 +02:00
|
|
|
|
2012-09-12 15:12:13 +02:00
|
|
|
object AkkaSpec {
|
2011-12-05 10:41:36 +01:00
|
|
|
val testConf: Config = ConfigFactory.parseString("""
|
2011-11-15 11:34:39 +01:00
|
|
|
akka {
|
2013-02-01 08:02:53 +01:00
|
|
|
loggers = ["akka.testkit.TestEventListener"]
|
2011-11-15 11:34:39 +01:00
|
|
|
loglevel = "WARNING"
|
2011-11-28 14:59:41 +01:00
|
|
|
stdout-loglevel = "WARNING"
|
2011-11-15 11:34:39 +01:00
|
|
|
actor {
|
|
|
|
|
default-dispatcher {
|
2012-02-01 10:10:13 +01:00
|
|
|
executor = "fork-join-executor"
|
|
|
|
|
fork-join-executor {
|
|
|
|
|
parallelism-min = 8
|
|
|
|
|
parallelism-factor = 2.0
|
2012-09-04 10:58:39 +02:00
|
|
|
parallelism-max = 8
|
2012-01-30 16:34:25 +01:00
|
|
|
}
|
2011-11-15 11:34:39 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-09-06 15:35:59 +02:00
|
|
|
""")
|
2011-11-15 11:34:39 +01:00
|
|
|
|
|
|
|
|
def mapToConfig(map: Map[String, Any]): Config = {
|
|
|
|
|
import scala.collection.JavaConverters._
|
|
|
|
|
ConfigFactory.parseMap(map.asJava)
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-24 13:56:50 +02:00
|
|
|
def getCallerName(clazz: Class[_]): String = {
|
2013-05-30 07:48:53 +02:00
|
|
|
val s = (Thread.currentThread.getStackTrace map (_.getClassName) drop 1)
|
|
|
|
|
.dropWhile(_ matches "(java.lang.Thread|.*AkkaSpec.?$)")
|
2012-05-24 13:56:50 +02:00
|
|
|
val reduced = s.lastIndexWhere(_ == clazz.getName) match {
|
|
|
|
|
case -1 ⇒ s
|
|
|
|
|
case z ⇒ s drop (z + 1)
|
|
|
|
|
}
|
|
|
|
|
reduced.head.replaceFirst(""".*\.""", "").replaceAll("[^a-zA-Z_0-9]", "_")
|
2011-11-30 22:19:48 +01:00
|
|
|
}
|
|
|
|
|
|
2011-11-15 11:34:39 +01:00
|
|
|
}
|
|
|
|
|
|
2012-09-12 15:12:13 +02:00
|
|
|
abstract class AkkaSpec(_system: ActorSystem)
|
2013-08-19 12:06:17 +02:00
|
|
|
extends TestKit(_system) with WordSpecLike with MustMatchers with BeforeAndAfterAll with WatchedByCoroner {
|
2011-10-20 20:45:02 +02:00
|
|
|
|
2012-06-28 11:32:11 +02:00
|
|
|
def this(config: Config) = this(ActorSystem(AkkaSpec.getCallerName(getClass),
|
|
|
|
|
ConfigFactory.load(config.withFallback(AkkaSpec.testConf))))
|
2011-11-30 22:19:48 +01:00
|
|
|
|
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))
|
|
|
|
|
|
2012-05-24 13:56:50 +02:00
|
|
|
def this() = this(ActorSystem(AkkaSpec.getCallerName(getClass), AkkaSpec.testConf))
|
2011-11-30 22:19:48 +01:00
|
|
|
|
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 {
|
2013-02-28 15:45:12 +13:00
|
|
|
startCoroner
|
2011-10-20 20:45:02 +02:00
|
|
|
atStartup()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final override def afterAll {
|
2013-01-03 16:38:18 +01:00
|
|
|
beforeTermination()
|
2013-05-02 17:12:36 +02:00
|
|
|
shutdown(system)
|
2013-01-03 17:17:12 +01:00
|
|
|
afterTermination()
|
2013-02-28 15:45:12 +13:00
|
|
|
stopCoroner()
|
2011-10-20 20:45:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected def atStartup() {}
|
|
|
|
|
|
2013-01-03 16:38:18 +01:00
|
|
|
protected def beforeTermination() {}
|
|
|
|
|
|
2013-01-03 17:17:12 +01:00
|
|
|
protected def afterTermination() {}
|
2011-10-10 11:12:34 +02:00
|
|
|
|
2012-08-29 18:00:14 +02:00
|
|
|
def spawn(dispatcherId: String = Dispatchers.DefaultDispatcherId)(body: ⇒ Unit): Unit =
|
|
|
|
|
Future(body)(system.dispatchers.lookup(dispatcherId))
|
2013-02-28 15:45:12 +13:00
|
|
|
|
|
|
|
|
override def expectedTestDuration: FiniteDuration = 60 seconds
|
|
|
|
|
|
2013-04-23 15:05:27 +02:00
|
|
|
def muteDeadLetters(messageClasses: Class[_]*)(sys: ActorSystem = system): Unit =
|
2013-04-22 20:39:32 +02:00
|
|
|
if (!sys.log.isDebugEnabled) {
|
2013-04-23 15:05:27 +02:00
|
|
|
def mute(clazz: Class[_]): Unit =
|
|
|
|
|
sys.eventStream.publish(Mute(DeadLettersFilter(clazz)(occurrences = Int.MaxValue)))
|
|
|
|
|
if (messageClasses.isEmpty) mute(classOf[AnyRef])
|
|
|
|
|
else messageClasses foreach mute
|
2013-04-22 20:39:32 +02:00
|
|
|
}
|
|
|
|
|
|
2011-10-28 17:15:10 +02:00
|
|
|
}
|