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 akka.config.Configuration
|
|
|
|
//#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(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
|
|
}
|
|
app.eventStream.publish(TestEvent.Mute(filter))
|
|
app.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 }
|
|
|
|
app.eventStream.unsubscribe(testActor)
|
|
app.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()
|
|
}
|
|
}
|