TestKit实例(Scala)

这是Ray Roestenburg 在 他的博客 中的示例代码,作了改动以兼容 Akka 2.x。

  1. import scala.util.Random
  2. import org.scalatest.BeforeAndAfterAll
  3. import org.scalatest.WordSpecLike
  4. import org.scalatest.Matchers
  5. import com.typesafe.config.ConfigFactory
  6. import akka.actor.Actor
  7. import akka.actor.ActorRef
  8. import akka.actor.ActorSystem
  9. import akka.actor.Props
  10. import akka.testkit.{ TestActors, DefaultTimeout, ImplicitSender, TestKit }
  11. import scala.concurrent.duration._
  12. import scala.collection.immutable
  13. /**
  14. * a Test to show some TestKit examples
  15. */
  16. class TestKitUsageSpec
  17. extends TestKit(ActorSystem("TestKitUsageSpec",
  18. ConfigFactory.parseString(TestKitUsageSpec.config)))
  19. with DefaultTimeout with ImplicitSender
  20. with WordSpecLike with Matchers with BeforeAndAfterAll {
  21. import TestKitUsageSpec._
  22. val echoRef = system.actorOf(TestActors.echoActorProps)
  23. val forwardRef = system.actorOf(Props(classOf[ForwardingActor], testActor))
  24. val filterRef = system.actorOf(Props(classOf[FilteringActor], testActor))
  25. val randomHead = Random.nextInt(6)
  26. val randomTail = Random.nextInt(10)
  27. val headList = immutable.Seq().padTo(randomHead, "0")
  28. val tailList = immutable.Seq().padTo(randomTail, "1")
  29. val seqRef =
  30. system.actorOf(Props(classOf[SequencingActor], testActor, headList, tailList))
  31. override def afterAll {
  32. shutdown()
  33. }
  34. "An EchoActor" should {
  35. "Respond with the same message it receives" in {
  36. within(500 millis) {
  37. echoRef ! "test"
  38. expectMsg("test")
  39. }
  40. }
  41. }
  42. "A ForwardingActor" should {
  43. "Forward a message it receives" in {
  44. within(500 millis) {
  45. forwardRef ! "test"
  46. expectMsg("test")
  47. }
  48. }
  49. }
  50. "A FilteringActor" should {
  51. "Filter all messages, except expected messagetypes it receives" in {
  52. var messages = Seq[String]()
  53. within(500 millis) {
  54. filterRef ! "test"
  55. expectMsg("test")
  56. filterRef ! 1
  57. expectNoMsg
  58. filterRef ! "some"
  59. filterRef ! "more"
  60. filterRef ! 1
  61. filterRef ! "text"
  62. filterRef ! 1
  63. receiveWhile(500 millis) {
  64. case msg: String => messages = msg +: messages
  65. }
  66. }
  67. messages.length should be(3)
  68. messages.reverse should be(Seq("some", "more", "text"))
  69. }
  70. }
  71. "A SequencingActor" should {
  72. "receive an interesting message at some point " in {
  73. within(500 millis) {
  74. ignoreMsg {
  75. case msg: String => msg != "something"
  76. }
  77. seqRef ! "something"
  78. expectMsg("something")
  79. ignoreMsg {
  80. case msg: String => msg == "1"
  81. }
  82. expectNoMsg
  83. ignoreNoMsg
  84. }
  85. }
  86. }
  87. }
  88. object TestKitUsageSpec {
  89. // Define your test specific configuration here
  90. val config = """
  91. akka {
  92. loglevel = "WARNING"
  93. }
  94. """
  95. /**
  96. * An Actor that forwards every message to a next Actor
  97. */
  98. class ForwardingActor(next: ActorRef) extends Actor {
  99. def receive = {
  100. case msg => next ! msg
  101. }
  102. }
  103. /**
  104. * An Actor that only forwards certain messages to a next Actor
  105. */
  106. class FilteringActor(next: ActorRef) extends Actor {
  107. def receive = {
  108. case msg: String => next ! msg
  109. case _ => None
  110. }
  111. }
  112. /**
  113. * An actor that sends a sequence of messages with a random head list, an
  114. * interesting value and a random tail list. The idea is that you would
  115. * like to test that the interesting value is received and that you cant
  116. * be bothered with the rest
  117. */
  118. class SequencingActor(next: ActorRef, head: immutable.Seq[String],
  119. tail: immutable.Seq[String]) extends Actor {
  120. def receive = {
  121. case msg => {
  122. head foreach { next ! _ }
  123. next ! msg
  124. tail foreach { next ! _ }
  125. }
  126. }
  127. }
  128. }