pekko/akka-docs/scala/code/ActorDocSpec.scala
Roland f46c6dc533 introducing: MainBus feat. LoggingBus
most tests passing, everything compiling, but docs not updated and nasty
thread-leak preventing me from running the whole test-suite (which is
the reason for this commit: I want to chase down that one first).

- the app.mainbus is classified by Class[_] (currently lookup, will
  possibly change to sub-class-aware) and accepts AnyRef messages
- LoggingBus handles akka.event-handlers from config specially:
  + start them as system services, supervised by SystemGuardian
  + keep their subscriptions in sync when logLevel_= is called
  + send them InitializeLogger(bus) message before subscribing them (so
    they can register for extras like Mute/UnMute)
- two-phased start-up: first phase with actor-less stdout logging, then
  subscription of config loggers, then remove stdout logger (logLevels
  configurable separately)
- MainBusReaper watches registered receivers and unsubscribes them upon
  death (started in phase 2)
- logger factory on Logging object, needs app/bus and log source;
  default instance in app.log
2011-10-27 12:36:22 +02:00

65 lines
1.5 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
//#imports
//#my-actor
class MyActor extends Actor {
val log = Logging(app, this)
def receive = {
case "test" log.info("received test")
case _ log.info("received unknown message")
}
}
//#my-actor
class ActorDocSpec extends AkkaSpec {
"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
}
app.mainbus.publish(TestEvent.Mute(filter))
app.mainbus.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 }
app.mainbus.unsubscribe(testActor)
app.mainbus.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()
}
}