TestKit Example (Scala)
Loading

TestKit Example (Scala)

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.WordSpec
  5. import org.scalatest.matchers.ShouldMatchers
  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.DefaultTimeout
  14. import akka.testkit.ImplicitSender
  15. import akka.testkit.TestKit
  16. import scala.concurrent.duration._
  17.  
  18. /**
  19. * a Test to show some TestKit examples
  20. */
  21. class TestKitUsageSpec
  22. extends TestKit(ActorSystem("TestKitUsageSpec",
  23. ConfigFactory.parseString(TestKitUsageSpec.config)))
  24. with DefaultTimeout with ImplicitSender
  25. with WordSpec with ShouldMatchers with BeforeAndAfterAll {
  26. import TestKitUsageSpec._
  27.  
  28. val echoRef = system.actorOf(Props(new EchoActor))
  29. val forwardRef = system.actorOf(Props(new ForwardingActor(testActor)))
  30. val filterRef = system.actorOf(Props(new FilteringActor(testActor)))
  31. val randomHead = Random.nextInt(6)
  32. val randomTail = Random.nextInt(10)
  33. val headList = Seq().padTo(randomHead, "0")
  34. val tailList = Seq().padTo(randomTail, "1")
  35. val seqRef =
  36. system.actorOf(Props(new SequencingActor(testActor, headList, tailList)))
  37.  
  38. override def afterAll {
  39. system.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 echoes everything you send to it
  108. */
  109. class EchoActor extends Actor {
  110. def receive = {
  111. case msg sender ! msg
  112. }
  113. }
  114.  
  115. /**
  116. * An Actor that forwards every message to a next Actor
  117. */
  118. class ForwardingActor(next: ActorRef) extends Actor {
  119. def receive = {
  120. case msg next ! msg
  121. }
  122. }
  123.  
  124. /**
  125. * An Actor that only forwards certain messages to a next Actor
  126. */
  127. class FilteringActor(next: ActorRef) extends Actor {
  128. def receive = {
  129. case msg: String next ! msg
  130. case _ None
  131. }
  132. }
  133.  
  134. /**
  135. * An actor that sends a sequence of messages with a random head list, an
  136. * interesting value and a random tail list. The idea is that you would
  137. * like to test that the interesting value is received and that you cant
  138. * be bothered with the rest
  139. */
  140. class SequencingActor(next: ActorRef, head: Seq[String], tail: Seq[String])
  141. extends Actor {
  142. def receive = {
  143. case msg {
  144. head foreach { next ! _ }
  145. next ! msg
  146. tail foreach { next ! _ }
  147. }
  148. }
  149. }
  150. }