- create ActorSystemImpl trait to make ActorSystem fully abstract - add Java API for constructing (ActorSystem.create(...)) - only go through factory methods because .start() has become necessary - rename all user-facing occurrences of “app” to “system” (Actor trait and TestKit/AkkaSpec) - pass ActorSystemImpl to ActorRefs upon creation, which means that actorOf() and friends need such an argument, which must be provided to the ActorRefProvider by the ActorRefFactory implementation
66 lines
1.7 KiB
Scala
66 lines
1.7 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 akka.config.Configuration
|
|
|
|
//#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(Configuration("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()
|
|
}
|
|
}
|