- Introduction
- 1. 引言
- 2. 概述
- 3. Actors
- 4. Futures与Agents
- 5. 网络
- 6. 实用工具
- 7. 如何使用:常用模式
- 8. 实验模块
- 9. Akka开发者信息
- 10. 工程信息
- 11. 附加信息
- Published using GitBook
AKKA 2.3.6 Scala 文档
TestKit实例(Scala)
这是Ray Roestenburg 在 他的博客 中的示例代码,作了改动以兼容 Akka 2.x。
import scala.util.Randomimport org.scalatest.BeforeAndAfterAllimport org.scalatest.WordSpecLikeimport org.scalatest.Matchersimport com.typesafe.config.ConfigFactoryimport akka.actor.Actorimport akka.actor.ActorRefimport akka.actor.ActorSystemimport akka.actor.Propsimport akka.testkit.{ TestActors, DefaultTimeout, ImplicitSender, TestKit }import scala.concurrent.duration._import scala.collection.immutable/*** a Test to show some TestKit examples*/class TestKitUsageSpecextends TestKit(ActorSystem("TestKitUsageSpec",ConfigFactory.parseString(TestKitUsageSpec.config)))with DefaultTimeout with ImplicitSenderwith WordSpecLike with Matchers with BeforeAndAfterAll {import TestKitUsageSpec._val echoRef = system.actorOf(TestActors.echoActorProps)val forwardRef = system.actorOf(Props(classOf[ForwardingActor], testActor))val filterRef = system.actorOf(Props(classOf[FilteringActor], testActor))val randomHead = Random.nextInt(6)val randomTail = Random.nextInt(10)val headList = immutable.Seq().padTo(randomHead, "0")val tailList = immutable.Seq().padTo(randomTail, "1")val seqRef =system.actorOf(Props(classOf[SequencingActor], testActor, headList, tailList))override def afterAll {shutdown()}"An EchoActor" should {"Respond with the same message it receives" in {within(500 millis) {echoRef ! "test"expectMsg("test")}}}"A ForwardingActor" should {"Forward a message it receives" in {within(500 millis) {forwardRef ! "test"expectMsg("test")}}}"A FilteringActor" should {"Filter all messages, except expected messagetypes it receives" in {var messages = Seq[String]()within(500 millis) {filterRef ! "test"expectMsg("test")filterRef ! 1expectNoMsgfilterRef ! "some"filterRef ! "more"filterRef ! 1filterRef ! "text"filterRef ! 1receiveWhile(500 millis) {case msg: String => messages = msg +: messages}}messages.length should be(3)messages.reverse should be(Seq("some", "more", "text"))}}"A SequencingActor" should {"receive an interesting message at some point " in {within(500 millis) {ignoreMsg {case msg: String => msg != "something"}seqRef ! "something"expectMsg("something")ignoreMsg {case msg: String => msg == "1"}expectNoMsgignoreNoMsg}}}}object TestKitUsageSpec {// Define your test specific configuration hereval config = """akka {loglevel = "WARNING"}"""/*** An Actor that forwards every message to a next Actor*/class ForwardingActor(next: ActorRef) extends Actor {def receive = {case msg => next ! msg}}/*** An Actor that only forwards certain messages to a next Actor*/class FilteringActor(next: ActorRef) extends Actor {def receive = {case msg: String => next ! msgcase _ => None}}/*** An actor that sends a sequence of messages with a random head list, an* interesting value and a random tail list. The idea is that you would* like to test that the interesting value is received and that you cant* be bothered with the rest*/class SequencingActor(next: ActorRef, head: immutable.Seq[String],tail: immutable.Seq[String]) extends Actor {def receive = {case msg => {head foreach { next ! _ }next ! msgtail foreach { next ! _ }}}}}
