From 7ca3a9699eda29fd1ca3737296daac9bf9ec88f7 Mon Sep 17 00:00:00 2001 From: Michal Knapik Date: Wed, 4 Jun 2014 20:51:12 +0200 Subject: [PATCH] +tes #12681 add EchoActor --- .../throttle/TimerBasedThrottlerSpec.scala | 16 ++++--------- .../code/docs/testkit/PlainWordSpec.scala | 15 ++---------- .../code/docs/testkit/TestKitUsageSpec.scala | 15 ++---------- .../main/scala/akka/testkit/TestActors.scala | 24 +++++++++++++++++++ .../scala/akka/testkit/TestActorsSpec.scala | 20 ++++++++++++++++ 5 files changed, 53 insertions(+), 37 deletions(-) create mode 100644 akka-testkit/src/main/scala/akka/testkit/TestActors.scala create mode 100644 akka-testkit/src/test/scala/akka/testkit/TestActorsSpec.scala diff --git a/akka-contrib/src/test/scala/akka/contrib/throttle/TimerBasedThrottlerSpec.scala b/akka-contrib/src/test/scala/akka/contrib/throttle/TimerBasedThrottlerSpec.scala index e9a341f092..f549dc97f1 100644 --- a/akka-contrib/src/test/scala/akka/contrib/throttle/TimerBasedThrottlerSpec.scala +++ b/akka-contrib/src/test/scala/akka/contrib/throttle/TimerBasedThrottlerSpec.scala @@ -20,12 +20,6 @@ import org.scalatest.BeforeAndAfterAll import akka.testkit._ object TimerBasedThrottlerSpec { - class EchoActor extends Actor { - def receive = { - case x ⇒ sender() ! x - } - } - def println(a: Any) = () //#demo-code @@ -70,7 +64,7 @@ class TimerBasedThrottlerSpec extends TestKit(ActorSystem("TimerBasedThrottlerSp } "keep messages until a target is set" in { - val echo = system.actorOf(Props[TimerBasedThrottlerSpec.EchoActor]) + val echo = system.actorOf(TestActors.echoActorProps) val throttler = system.actorOf(Props(classOf[TimerBasedThrottler], 3 msgsPer (1.second.dilated))) 1 to 6 foreach { throttler ! _ } expectNoMsg(1 second) @@ -81,7 +75,7 @@ class TimerBasedThrottlerSpec extends TestKit(ActorSystem("TimerBasedThrottlerSp } "send messages after a `SetTarget(None)` pause" in { - val echo = system.actorOf(Props[TimerBasedThrottlerSpec.EchoActor]) + val echo = system.actorOf(TestActors.echoActorProps) val throttler = system.actorOf(Props(classOf[TimerBasedThrottler], 3 msgsPer (1.second.dilated))) throttler ! SetTarget(Some(echo)) 1 to 3 foreach { throttler ! _ } @@ -99,7 +93,7 @@ class TimerBasedThrottlerSpec extends TestKit(ActorSystem("TimerBasedThrottlerSp } "keep messages when the target is set to None" in { - val echo = system.actorOf(Props[TimerBasedThrottlerSpec.EchoActor]) + val echo = system.actorOf(TestActors.echoActorProps) val throttler = system.actorOf(Props(classOf[TimerBasedThrottler], 3 msgsPer (1.second.dilated))) throttler ! SetTarget(Some(echo)) 1 to 7 foreach { throttler ! _ } @@ -116,7 +110,7 @@ class TimerBasedThrottlerSpec extends TestKit(ActorSystem("TimerBasedThrottlerSp } "respect the rate (3 msg/s)" in within(1.5 seconds, 2.5 seconds) { - val echo = system.actorOf(Props[TimerBasedThrottlerSpec.EchoActor]) + val echo = system.actorOf(TestActors.echoActorProps) val throttler = system.actorOf(Props(classOf[TimerBasedThrottler], 3 msgsPer (1.second.dilated))) throttler ! SetTarget(Some(echo)) 1 to 7 foreach { throttler ! _ } @@ -124,7 +118,7 @@ class TimerBasedThrottlerSpec extends TestKit(ActorSystem("TimerBasedThrottlerSp } "respect the rate (4 msg/s)" in within(1.5 seconds, 2.5 seconds) { - val echo = system.actorOf(Props[TimerBasedThrottlerSpec.EchoActor]) + val echo = system.actorOf(TestActors.echoActorProps) val throttler = system.actorOf(Props(classOf[TimerBasedThrottler], 4 msgsPer (1.second.dilated))) throttler ! SetTarget(Some(echo)) 1 to 9 foreach { throttler ! _ } diff --git a/akka-docs/rst/scala/code/docs/testkit/PlainWordSpec.scala b/akka-docs/rst/scala/code/docs/testkit/PlainWordSpec.scala index 57f24d5fe0..2abe4fbcca 100644 --- a/akka-docs/rst/scala/code/docs/testkit/PlainWordSpec.scala +++ b/akka-docs/rst/scala/code/docs/testkit/PlainWordSpec.scala @@ -7,19 +7,10 @@ package docs.testkit import akka.actor.ActorSystem import akka.actor.Actor import akka.actor.Props -import akka.testkit.TestKit +import akka.testkit.{ TestActors, TestKit, ImplicitSender } import org.scalatest.WordSpecLike import org.scalatest.Matchers import org.scalatest.BeforeAndAfterAll -import akka.testkit.ImplicitSender - -object MySpec { - class EchoActor extends Actor { - def receive = { - case x => sender() ! x - } - } -} //#implicit-sender class MySpec(_system: ActorSystem) extends TestKit(_system) with ImplicitSender @@ -28,8 +19,6 @@ class MySpec(_system: ActorSystem) extends TestKit(_system) with ImplicitSender def this() = this(ActorSystem("MySpec")) - import MySpec._ - override def afterAll { TestKit.shutdownActorSystem(system) } @@ -37,7 +26,7 @@ class MySpec(_system: ActorSystem) extends TestKit(_system) with ImplicitSender "An Echo actor" must { "send back messages unchanged" in { - val echo = system.actorOf(Props[EchoActor]) + val echo = system.actorOf(TestActors.echoActorProps) echo ! "hello world" expectMsg("hello world") } diff --git a/akka-docs/rst/scala/code/docs/testkit/TestKitUsageSpec.scala b/akka-docs/rst/scala/code/docs/testkit/TestKitUsageSpec.scala index baa316f79b..8e03038282 100644 --- a/akka-docs/rst/scala/code/docs/testkit/TestKitUsageSpec.scala +++ b/akka-docs/rst/scala/code/docs/testkit/TestKitUsageSpec.scala @@ -18,9 +18,7 @@ import akka.actor.Actor import akka.actor.ActorRef import akka.actor.ActorSystem import akka.actor.Props -import akka.testkit.DefaultTimeout -import akka.testkit.ImplicitSender -import akka.testkit.TestKit +import akka.testkit.{ TestActors, DefaultTimeout, ImplicitSender, TestKit } import scala.concurrent.duration._ import scala.collection.immutable @@ -34,7 +32,7 @@ class TestKitUsageSpec with WordSpecLike with Matchers with BeforeAndAfterAll { import TestKitUsageSpec._ - val echoRef = system.actorOf(Props[EchoActor]) + 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) @@ -112,15 +110,6 @@ object TestKitUsageSpec { } """ - /** - * An Actor that echoes everything you send to it - */ - class EchoActor extends Actor { - def receive = { - case msg => sender() ! msg - } - } - /** * An Actor that forwards every message to a next Actor */ diff --git a/akka-testkit/src/main/scala/akka/testkit/TestActors.scala b/akka-testkit/src/main/scala/akka/testkit/TestActors.scala new file mode 100644 index 0000000000..0a9f3a7def --- /dev/null +++ b/akka-testkit/src/main/scala/akka/testkit/TestActors.scala @@ -0,0 +1,24 @@ +/** + * Copyright (C) 2009-2014 Typesafe Inc. + */ +package akka.testkit + +import akka.actor.{ Props, Actor } + +/** + * A collection of common actor patterns used in tests. + */ +object TestActors { + + /** + * EchoActor sends back received messages (unmodified). + */ + class EchoActor extends Actor { + override def receive = { + case message ⇒ sender() ! message + } + } + + val echoActorProps = Props[EchoActor]() + +} diff --git a/akka-testkit/src/test/scala/akka/testkit/TestActorsSpec.scala b/akka-testkit/src/test/scala/akka/testkit/TestActorsSpec.scala new file mode 100644 index 0000000000..bb7e2b4889 --- /dev/null +++ b/akka-testkit/src/test/scala/akka/testkit/TestActorsSpec.scala @@ -0,0 +1,20 @@ +/** + * Copyright (C) 2009-2014 Typesafe Inc. + */ +package akka.testkit + +class TestActorsSpec extends AkkaSpec with ImplicitSender { + + import TestActors.echoActorProps + + "A EchoActor" must { + "send back messages unchanged" in { + val message = "hello world" + val echo = system.actorOf(echoActorProps) + + echo ! message + + expectMsg(message) + } + } +}