• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Testing akka-actors
 

Testing akka-actors

on

  • 1,826 views

This presentation is about Testing Akka Actors. We discussed in this presention how to test akka actors using testkit provided by akka framework.

This presentation is about Testing Akka Actors. We discussed in this presention how to test akka actors using testkit provided by akka framework.

Statistics

Views

Total Views
1,826
Views on SlideShare
674
Embed Views
1,152

Actions

Likes
1
Downloads
23
Comments
0

5 Embeds 1,152

http://blog.knoldus.com 1132
http://localhost 12
http://feedwrangler.net 5
http://knoldus.wordpress.com 2
http://www.tuicool.com 1

Accessibility

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Testing akka-actors Testing akka-actors Presentation Transcript

    • Testing Akka ActorsTesting Akka ActorsPiyush MishraSoftware ConsultantKnoldus Software LLPPiyush MishraSoftware ConsultantKnoldus Software LLP
    • Topics CoveredTopics CoveredBasics of AkkaSettings in build.sbtDefining an of Akka ActorCreation of Akka ActorActorRefTesting Akka ActorsIntegration testing using testkitBasics of AkkaSettings in build.sbtDefining an of Akka ActorCreation of Akka ActorActorRefTesting Akka ActorsIntegration testing using testkit
    • What is AkkaWhat is AkkaAkka is framework for writing event driving, concurrent,fault-tolerant and scalable application.a) It uses actor model to build concurrent, fault-tolerantand scalable application.b) Actors are the higher abstraction for concurrency andparallelism.c) They are very lightweight event-driven processes(approximately 2.7 million actors per GB RAM).d) Akka provides supervisor stategy to build fault toleranceApplications.Akka is framework for writing event driving, concurrent,fault-tolerant and scalable application.a) It uses actor model to build concurrent, fault-tolerantand scalable application.b) Actors are the higher abstraction for concurrency andparallelism.c) They are very lightweight event-driven processes(approximately 2.7 million actors per GB RAM).d) Akka provides supervisor stategy to build fault toleranceApplications.
    • Settings in build.sbtSettings in build.sbtIn order to test Akka actors we need to add following twodependencies in build.scala or build.sbtlibraryDependencies ++= Seq("com.typesafe.akka" % "akka-actor" % "2.0.5","com.typesafe.akka" % "akka-testkit" % "2.0.5")In order to test Akka actors we need to add following twodependencies in build.scala or build.sbtlibraryDependencies ++= Seq("com.typesafe.akka" % "akka-actor" % "2.0.5","com.typesafe.akka" % "akka-testkit" % "2.0.5")
    • Defining an Akka actorDefining an Akka actorpackage com.Testingimport akka.actor.Actorclass SimpleActor extends Actor {def receive = {case msg: String =>case _ => println("Got other than string")}}package com.Testingimport akka.actor.Actorclass SimpleActor extends Actor {def receive = {case msg: String =>case _ => println("Got other than string")}}
    • Creating an actorCreating an actorpackage com.Testingimport akka.actor.ActorSystemimport akka.actor.Propsobject CreateActor extends App {val system = ActorSystem("testing")val actor = system.actorOf(Props[SimpleActor])actor ! "hello"}package com.Testingimport akka.actor.ActorSystemimport akka.actor.Propsobject CreateActor extends App {val system = ActorSystem("testing")val actor = system.actorOf(Props[SimpleActor])actor ! "hello"}
    • What is ActorRefWhat is ActorRefActorRef is the Immutable and serializable handle to an actorwhich may or may not reside on the local host or inside thesame ActorSystem .We can not call methods on actorRef.ActorRef is the Immutable and serializable handle to an actorwhich may or may not reside on the local host or inside thesame ActorSystem .We can not call methods on actorRef.
    • Testing with TestActorRefTesting with TestActorRefThis special type of reference is designed specifically for testpurposes and allows access to the actor in two ways: eitherby obtaining a reference to the underlying actor instance, orby invoking or querying the actors behaviour (receive).This special type of reference is designed specifically for testpurposes and allows access to the actor in two ways: eitherby obtaining a reference to the underlying actor instance, orby invoking or querying the actors behaviour (receive).
    • Obtaining a Reference to an ActorObtaining a Reference to an Actorimport akka.testkit.TestActorRefval actorRef = TestActorRef[MyActor]val actor = actorRef.underlyingActorimport akka.testkit.TestActorRefval actorRef = TestActorRef[MyActor]val actor = actorRef.underlyingActor
    • Testing the Actors BehaviorTesting the Actors Behaviorimplicit val timeout = Timeout(5 seconds)implicit val system = ActorSystem("Unit")val actorRef = TestActorRef(new SimpleActor)val result = Await.result((actorRef ? 42), 5seconds).asInstanceOf[Int]result must be(42)implicit val timeout = Timeout(5 seconds)implicit val system = ActorSystem("Unit")val actorRef = TestActorRef(new SimpleActor)val result = Await.result((actorRef ? 42), 5seconds).asInstanceOf[Int]result must be(42)
    • Integration test with TestkitIntegration test with TestkitTestkit is used for integration testing of an actor. This toolkitprovider a ImplicitSender which is actually a test actor whichdispatches messages to actors under test.ContinuedTestkit is used for integration testing of an actor. This toolkitprovider a ImplicitSender which is actually a test actor whichdispatches messages to actors under test.Continued
    • Integration test with TestkitIntegration test with Testkitclass MySpec(_system: ActorSystem) extends TestKit(_system) withImplicitSender with WordSpec with MustMatchers with BeforeAndAfterAll {def this() = this(ActorSystem("MySpec"))import MySpec._override def afterAll {system.shutdown()}"An Echo actor" must {"send back messages unchanged" in {val echo = system.actorOf(Props[EchoActor])echo ! "hello world"expectMsg("hello world")}class MySpec(_system: ActorSystem) extends TestKit(_system) withImplicitSender with WordSpec with MustMatchers with BeforeAndAfterAll {def this() = this(ActorSystem("MySpec"))import MySpec._override def afterAll {system.shutdown()}"An Echo actor" must {"send back messages unchanged" in {val echo = system.actorOf(Props[EchoActor])echo ! "hello world"expectMsg("hello world")}
    • Timing AssertionsTiming AssertionsTiming: certain events must not happen immediately (like a timer), others needto happen before a deadline.The block given to within must complete after a Duration which is between minand max, where the former defaults to zero. The deadline calculated by addingthe max parameter to the blocks start time is implicitly available within the blockto all examination methodsTiming: certain events must not happen immediately (like a timer), others needto happen before a deadline.The block given to within must complete after a Duration which is between minand max, where the former defaults to zero. The deadline calculated by addingthe max parameter to the blocks start time is implicitly available within the blockto all examination methods
    • Timing AssertionsTiming Assertionsclass Timing(_system: ActorSystem) extends TestKit(_system) withImplicitSender with MustMatchers with WordSpec {def this() = this(ActorSystem("MySpec"))import MySpec._"An simple actor" must {"send reply within 2 seconds" in {val worker = system.actorOf(Props[SimpleActor])within(200 millis) {worker ! 4expectMsg(4)expectNoMsg // will block for the rest of the 200msThread.sleep(300) // will NOT make this block fail}}}}class Timing(_system: ActorSystem) extends TestKit(_system) withImplicitSender with MustMatchers with WordSpec {def this() = this(ActorSystem("MySpec"))import MySpec._"An simple actor" must {"send reply within 2 seconds" in {val worker = system.actorOf(Props[SimpleActor])within(200 millis) {worker ! 4expectMsg(4)expectNoMsg // will block for the rest of the 200msThread.sleep(300) // will NOT make this block fail}}}}
    • Using ProbesUsing ProbesWhen the actors under test are supposed to send variousmessages to different destinations. approach is to use it forcreation of simple probe actors to be inserted in the messageflows. To make this more powerful and convenient, there is aconcrete implementation called TestProbe.The functionality is best explained using a small exampleWhen the actors under test are supposed to send variousmessages to different destinations. approach is to use it forcreation of simple probe actors to be inserted in the messageflows. To make this more powerful and convenient, there is aconcrete implementation called TestProbe.The functionality is best explained using a small example
    • Using ProbesUsing Probesclass Probe(_system: ActorSystem) extends TestKit(_system) with ImplicitSender with WordSpecwith MustMatchers with BeforeAndAfterAll {def this() = this(ActorSystem("MySpec"))import MySpec._override def afterAll {system.shutdown()}"An DoubleEcho actor" must {"send reply both probes" in {val probe1 = TestProbe()val probe2 = TestProbe()val actor = system.actorOf(Props[MyDoubleEcho])actor ! (probe1.ref, probe2.ref)actor ! "hello"probe1.expectMsg(500 millis, "hello")probe2.expectMsg(500 millis, "hello")}}}class Probe(_system: ActorSystem) extends TestKit(_system) with ImplicitSender with WordSpecwith MustMatchers with BeforeAndAfterAll {def this() = this(ActorSystem("MySpec"))import MySpec._override def afterAll {system.shutdown()}"An DoubleEcho actor" must {"send reply both probes" in {val probe1 = TestProbe()val probe2 = TestProbe()val actor = system.actorOf(Props[MyDoubleEcho])actor ! (probe1.ref, probe2.ref)actor ! "hello"probe1.expectMsg(500 millis, "hello")probe2.expectMsg(500 millis, "hello")}}}
    • ThanksThanks