TestKit Example
Loading

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