TestKit Example - Version 2.4.20

TestKit Example

Ray Roestenburg's example code from his blog adapted to work with Akka 2.x.

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