* All default values removed from code and loaded from akka-actor-reference.conf, located in src/main/resources (included in jar) * Default test configuration included in AkkaSpec instead of using akka.test.conf, avoids problems when running test (in IDE) and forgetting to use -Dakka.mode=test. * System.properties used first, if availble * Next step will be to split akka-actor-reference.conf in separate -reference for each module
66 lines
1.6 KiB
Scala
66 lines
1.6 KiB
Scala
package akka.docs.stm
|
|
|
|
import org.scalatest.{ BeforeAndAfterAll, WordSpec }
|
|
import org.scalatest.matchers.MustMatchers
|
|
import akka.testkit._
|
|
import akka.util.duration._
|
|
|
|
//#imports
|
|
import akka.actor.Actor
|
|
import akka.event.Logging
|
|
import com.typesafe.config.Config
|
|
|
|
//#imports
|
|
|
|
//#my-actor
|
|
class MyActor extends Actor {
|
|
val log = Logging(system, this)
|
|
def receive = {
|
|
case "test" ⇒ log.info("received test")
|
|
case _ ⇒ log.info("received unknown message")
|
|
}
|
|
}
|
|
//#my-actor
|
|
|
|
class ActorDocSpec extends AkkaSpec(Map("akka.loglevel" -> "INFO")) {
|
|
|
|
"creating actor with AkkaSpec.actorOf" in {
|
|
//#creating-actorOf
|
|
val myActor = actorOf[MyActor]
|
|
//#creating-actorOf
|
|
|
|
// testing the actor
|
|
|
|
// TODO: convert docs to AkkaSpec(Configuration(...))
|
|
val filter = EventFilter.custom {
|
|
case e: Logging.Info ⇒ true
|
|
case _ ⇒ false
|
|
}
|
|
system.eventStream.publish(TestEvent.Mute(filter))
|
|
system.eventStream.subscribe(testActor, classOf[Logging.Info])
|
|
|
|
myActor ! "test"
|
|
expectMsgPF(1 second) { case Logging.Info(_, "received test") ⇒ true }
|
|
|
|
myActor ! "unknown"
|
|
expectMsgPF(1 second) { case Logging.Info(_, "received unknown message") ⇒ true }
|
|
|
|
system.eventStream.unsubscribe(testActor)
|
|
system.eventStream.publish(TestEvent.UnMute(filter))
|
|
|
|
myActor.stop()
|
|
}
|
|
|
|
"creating actor with constructor" in {
|
|
class MyActor(arg: String) extends Actor {
|
|
def receive = { case _ ⇒ () }
|
|
}
|
|
|
|
//#creating-constructor
|
|
// allows passing in arguments to the MyActor constructor
|
|
val myActor = actorOf(new MyActor("..."))
|
|
//#creating-constructor
|
|
|
|
myActor.stop()
|
|
}
|
|
}
|