2011-10-10 11:12:34 +02:00
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2009-2011 Typesafe Inc. <http://www.typesafe.com>
|
|
|
|
|
*/
|
|
|
|
|
package akka.testkit
|
|
|
|
|
|
2011-10-12 11:35:41 +02:00
|
|
|
import akka.config.Configuration
|
2011-10-20 20:45:02 +02:00
|
|
|
import org.scalatest.{ WordSpec, BeforeAndAfterAll }
|
2011-10-10 11:12:34 +02:00
|
|
|
import org.scalatest.matchers.MustMatchers
|
2011-11-10 20:08:00 +01:00
|
|
|
import akka.actor.ActorSystem
|
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-10-10 11:12:34 +02:00
|
|
|
|
2011-11-10 20:08:00 +01:00
|
|
|
abstract class AkkaSpec(_application: ActorSystem = ActorSystem())
|
2011-10-20 20:45:02 +02:00
|
|
|
extends TestKit(_application) with WordSpec with MustMatchers with BeforeAndAfterAll {
|
|
|
|
|
|
2011-11-10 20:48:50 +01:00
|
|
|
val log: LoggingAdapter = Logging(app.eventStream, this)
|
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 {
|
|
|
|
|
app.stop()
|
2011-10-28 17:15:10 +02:00
|
|
|
try app.terminationFuture.await(5 seconds) catch {
|
|
|
|
|
case _: FutureTimeoutException ⇒ app.log.warning("failed to stop within 5 seconds")
|
|
|
|
|
}
|
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-10 20:08:00 +01:00
|
|
|
def this(config: Configuration) = this(new ActorSystem(getClass.getSimpleName, ActorSystem.defaultConfig ++ config))
|
2011-10-12 11:35:41 +02:00
|
|
|
|
2011-10-18 17:56:23 +02:00
|
|
|
def actorOf(props: Props): ActorRef = app.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 {
|
|
|
|
|
"An AkkaSpec" must {
|
|
|
|
|
"terminate all actors" in {
|
2011-11-10 20:08:00 +01:00
|
|
|
import ActorSystem.defaultConfig
|
|
|
|
|
val app = ActorSystem("test", defaultConfig ++ Configuration(
|
2011-10-28 17:15:10 +02:00
|
|
|
"akka.actor.debug.lifecycle" -> true, "akka.loglevel" -> "DEBUG"))
|
|
|
|
|
val spec = new AkkaSpec(app) {
|
|
|
|
|
val ref = Seq(testActor, app.actorOf(Props.empty, "name"))
|
|
|
|
|
}
|
|
|
|
|
spec.ref foreach (_ must not be 'shutdown)
|
|
|
|
|
app.stop()
|
|
|
|
|
spec.awaitCond(spec.ref forall (_.isShutdown), 2 seconds)
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-11-09 14:56:05 +01:00
|
|
|
}
|
|
|
|
|
|