+tes #12681 add EchoActor
This commit is contained in:
parent
0e2f356d23
commit
7ca3a9699e
5 changed files with 53 additions and 37 deletions
|
|
@ -20,12 +20,6 @@ import org.scalatest.BeforeAndAfterAll
|
||||||
import akka.testkit._
|
import akka.testkit._
|
||||||
|
|
||||||
object TimerBasedThrottlerSpec {
|
object TimerBasedThrottlerSpec {
|
||||||
class EchoActor extends Actor {
|
|
||||||
def receive = {
|
|
||||||
case x ⇒ sender() ! x
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
def println(a: Any) = ()
|
def println(a: Any) = ()
|
||||||
|
|
||||||
//#demo-code
|
//#demo-code
|
||||||
|
|
@ -70,7 +64,7 @@ class TimerBasedThrottlerSpec extends TestKit(ActorSystem("TimerBasedThrottlerSp
|
||||||
}
|
}
|
||||||
|
|
||||||
"keep messages until a target is set" in {
|
"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)))
|
val throttler = system.actorOf(Props(classOf[TimerBasedThrottler], 3 msgsPer (1.second.dilated)))
|
||||||
1 to 6 foreach { throttler ! _ }
|
1 to 6 foreach { throttler ! _ }
|
||||||
expectNoMsg(1 second)
|
expectNoMsg(1 second)
|
||||||
|
|
@ -81,7 +75,7 @@ class TimerBasedThrottlerSpec extends TestKit(ActorSystem("TimerBasedThrottlerSp
|
||||||
}
|
}
|
||||||
|
|
||||||
"send messages after a `SetTarget(None)` pause" in {
|
"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)))
|
val throttler = system.actorOf(Props(classOf[TimerBasedThrottler], 3 msgsPer (1.second.dilated)))
|
||||||
throttler ! SetTarget(Some(echo))
|
throttler ! SetTarget(Some(echo))
|
||||||
1 to 3 foreach { throttler ! _ }
|
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 {
|
"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)))
|
val throttler = system.actorOf(Props(classOf[TimerBasedThrottler], 3 msgsPer (1.second.dilated)))
|
||||||
throttler ! SetTarget(Some(echo))
|
throttler ! SetTarget(Some(echo))
|
||||||
1 to 7 foreach { throttler ! _ }
|
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) {
|
"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)))
|
val throttler = system.actorOf(Props(classOf[TimerBasedThrottler], 3 msgsPer (1.second.dilated)))
|
||||||
throttler ! SetTarget(Some(echo))
|
throttler ! SetTarget(Some(echo))
|
||||||
1 to 7 foreach { throttler ! _ }
|
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) {
|
"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)))
|
val throttler = system.actorOf(Props(classOf[TimerBasedThrottler], 4 msgsPer (1.second.dilated)))
|
||||||
throttler ! SetTarget(Some(echo))
|
throttler ! SetTarget(Some(echo))
|
||||||
1 to 9 foreach { throttler ! _ }
|
1 to 9 foreach { throttler ! _ }
|
||||||
|
|
|
||||||
|
|
@ -7,19 +7,10 @@ package docs.testkit
|
||||||
import akka.actor.ActorSystem
|
import akka.actor.ActorSystem
|
||||||
import akka.actor.Actor
|
import akka.actor.Actor
|
||||||
import akka.actor.Props
|
import akka.actor.Props
|
||||||
import akka.testkit.TestKit
|
import akka.testkit.{ TestActors, TestKit, ImplicitSender }
|
||||||
import org.scalatest.WordSpecLike
|
import org.scalatest.WordSpecLike
|
||||||
import org.scalatest.Matchers
|
import org.scalatest.Matchers
|
||||||
import org.scalatest.BeforeAndAfterAll
|
import org.scalatest.BeforeAndAfterAll
|
||||||
import akka.testkit.ImplicitSender
|
|
||||||
|
|
||||||
object MySpec {
|
|
||||||
class EchoActor extends Actor {
|
|
||||||
def receive = {
|
|
||||||
case x => sender() ! x
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//#implicit-sender
|
//#implicit-sender
|
||||||
class MySpec(_system: ActorSystem) extends TestKit(_system) with ImplicitSender
|
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"))
|
def this() = this(ActorSystem("MySpec"))
|
||||||
|
|
||||||
import MySpec._
|
|
||||||
|
|
||||||
override def afterAll {
|
override def afterAll {
|
||||||
TestKit.shutdownActorSystem(system)
|
TestKit.shutdownActorSystem(system)
|
||||||
}
|
}
|
||||||
|
|
@ -37,7 +26,7 @@ class MySpec(_system: ActorSystem) extends TestKit(_system) with ImplicitSender
|
||||||
"An Echo actor" must {
|
"An Echo actor" must {
|
||||||
|
|
||||||
"send back messages unchanged" in {
|
"send back messages unchanged" in {
|
||||||
val echo = system.actorOf(Props[EchoActor])
|
val echo = system.actorOf(TestActors.echoActorProps)
|
||||||
echo ! "hello world"
|
echo ! "hello world"
|
||||||
expectMsg("hello world")
|
expectMsg("hello world")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,9 +18,7 @@ import akka.actor.Actor
|
||||||
import akka.actor.ActorRef
|
import akka.actor.ActorRef
|
||||||
import akka.actor.ActorSystem
|
import akka.actor.ActorSystem
|
||||||
import akka.actor.Props
|
import akka.actor.Props
|
||||||
import akka.testkit.DefaultTimeout
|
import akka.testkit.{ TestActors, DefaultTimeout, ImplicitSender, TestKit }
|
||||||
import akka.testkit.ImplicitSender
|
|
||||||
import akka.testkit.TestKit
|
|
||||||
import scala.concurrent.duration._
|
import scala.concurrent.duration._
|
||||||
import scala.collection.immutable
|
import scala.collection.immutable
|
||||||
|
|
||||||
|
|
@ -34,7 +32,7 @@ class TestKitUsageSpec
|
||||||
with WordSpecLike with Matchers with BeforeAndAfterAll {
|
with WordSpecLike with Matchers with BeforeAndAfterAll {
|
||||||
import TestKitUsageSpec._
|
import TestKitUsageSpec._
|
||||||
|
|
||||||
val echoRef = system.actorOf(Props[EchoActor])
|
val echoRef = system.actorOf(TestActors.echoActorProps)
|
||||||
val forwardRef = system.actorOf(Props(classOf[ForwardingActor], testActor))
|
val forwardRef = system.actorOf(Props(classOf[ForwardingActor], testActor))
|
||||||
val filterRef = system.actorOf(Props(classOf[FilteringActor], testActor))
|
val filterRef = system.actorOf(Props(classOf[FilteringActor], testActor))
|
||||||
val randomHead = Random.nextInt(6)
|
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
|
* An Actor that forwards every message to a next Actor
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
24
akka-testkit/src/main/scala/akka/testkit/TestActors.scala
Normal file
24
akka-testkit/src/main/scala/akka/testkit/TestActors.scala
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
/**
|
||||||
|
* Copyright (C) 2009-2014 Typesafe Inc. <http://www.typesafe.com>
|
||||||
|
*/
|
||||||
|
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]()
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,20 @@
|
||||||
|
/**
|
||||||
|
* Copyright (C) 2009-2014 Typesafe Inc. <http://www.typesafe.com>
|
||||||
|
*/
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue