* This can't go into 2.2.x since ScalaTest 1.9.2-SNAP2 has source incompatible changes and the dependecy in akka-multi-node-node-testkit would force people to upgrade.
101 lines
3.1 KiB
Scala
101 lines
3.1 KiB
Scala
/**
|
|
* Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
|
|
*/
|
|
package akka.testkit
|
|
|
|
import language.{ postfixOps, reflectiveCalls }
|
|
|
|
import org.scalatest.{ WordSpecLike, BeforeAndAfterAll, Tag }
|
|
import org.scalatest.matchers.MustMatchers
|
|
import akka.actor.{ Actor, Props, ActorSystem, PoisonPill, DeadLetter, ActorSystemImpl }
|
|
import akka.event.{ Logging, LoggingAdapter }
|
|
import scala.concurrent.duration._
|
|
import scala.concurrent.{ Await, Future }
|
|
import com.typesafe.config.{ Config, ConfigFactory }
|
|
import java.util.concurrent.TimeoutException
|
|
import akka.dispatch.Dispatchers
|
|
import akka.pattern.ask
|
|
import akka.testkit.TestEvent._
|
|
|
|
object AkkaSpec {
|
|
val testConf: Config = ConfigFactory.parseString("""
|
|
akka {
|
|
loggers = ["akka.testkit.TestEventListener"]
|
|
loglevel = "WARNING"
|
|
stdout-loglevel = "WARNING"
|
|
actor {
|
|
default-dispatcher {
|
|
executor = "fork-join-executor"
|
|
fork-join-executor {
|
|
parallelism-min = 8
|
|
parallelism-factor = 2.0
|
|
parallelism-max = 8
|
|
}
|
|
}
|
|
}
|
|
}
|
|
""")
|
|
|
|
def mapToConfig(map: Map[String, Any]): Config = {
|
|
import scala.collection.JavaConverters._
|
|
ConfigFactory.parseMap(map.asJava)
|
|
}
|
|
|
|
def getCallerName(clazz: Class[_]): String = {
|
|
val s = (Thread.currentThread.getStackTrace map (_.getClassName) drop 1)
|
|
.dropWhile(_ matches "(java.lang.Thread|.*AkkaSpec.?$)")
|
|
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]", "_")
|
|
}
|
|
|
|
}
|
|
|
|
abstract class AkkaSpec(_system: ActorSystem)
|
|
extends TestKit(_system) with WordSpecLike with MustMatchers with BeforeAndAfterAll with WatchedByCoroner {
|
|
|
|
def this(config: Config) = this(ActorSystem(AkkaSpec.getCallerName(getClass),
|
|
ConfigFactory.load(config.withFallback(AkkaSpec.testConf))))
|
|
|
|
def this(s: String) = this(ConfigFactory.parseString(s))
|
|
|
|
def this(configMap: Map[String, _]) = this(AkkaSpec.mapToConfig(configMap))
|
|
|
|
def this() = this(ActorSystem(AkkaSpec.getCallerName(getClass), AkkaSpec.testConf))
|
|
|
|
val log: LoggingAdapter = Logging(system, this.getClass)
|
|
|
|
final override def beforeAll {
|
|
startCoroner
|
|
atStartup()
|
|
}
|
|
|
|
final override def afterAll {
|
|
beforeTermination()
|
|
shutdown(system)
|
|
afterTermination()
|
|
stopCoroner()
|
|
}
|
|
|
|
protected def atStartup() {}
|
|
|
|
protected def beforeTermination() {}
|
|
|
|
protected def afterTermination() {}
|
|
|
|
def spawn(dispatcherId: String = Dispatchers.DefaultDispatcherId)(body: ⇒ Unit): Unit =
|
|
Future(body)(system.dispatchers.lookup(dispatcherId))
|
|
|
|
override def expectedTestDuration: FiniteDuration = 60 seconds
|
|
|
|
def muteDeadLetters(messageClasses: Class[_]*)(sys: ActorSystem = system): Unit =
|
|
if (!sys.log.isDebugEnabled) {
|
|
def mute(clazz: Class[_]): Unit =
|
|
sys.eventStream.publish(Mute(DeadLettersFilter(clazz)(occurrences = Int.MaxValue)))
|
|
if (messageClasses.isEmpty) mute(classOf[AnyRef])
|
|
else messageClasses foreach mute
|
|
}
|
|
|
|
}
|