Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
AKKA UNIT TESTING
HITCHHIKER GUID TO
HITCHHIKER GUID TO AKKA TESTING
ABOUT ME
▸ Aprox 7 year hands on with scala & akka
▸ Part of Indeni team
▸ mailto: 123avi@...
HITCHHIKER GUID TO AKKA TESTING
WHAT WILL BE COVERED
▸ Akka basics
▸ Settings (sbt)
▸ Simple actors app
▸ Actor messages t...
HITCHHIKER GUID TO AKKA TESTING
THE ACTOR-MESSAGING CONCEPT IS NOT NEW
▸ Formalized in 1973 by Carl Hewitt and refined by G...
HITCHHIKER GUID TO AKKA TESTING
WHAT IS AKKA FRAMEWORK - TACHLES
▸ Event driving, messaging
style, concurrent, fault-toler...
HITCHHIKER GUID TO AKKA TESTING
SETTINGS - BUILD.SBT
▸ Dependencies
libraryDependencies ++= Seq(
"com.typesafe.akka" %% "a...
HITCHHIKER GUID TO AKKA TESTING
AKKA TESTKIT
▸ Library to simplify unit tests on actors
▸ Can be extended by your test cla...
HITCHHIKER GUID TO AKKA TESTING
TEST EVERYTHING
HITCHHIKER GUID TO AKKA TESTING
MIXING IMPLICIT SENDER
▸ Use implicit sender to get “sender” replies
def receive {
case _ ...
HITCHHIKER GUID TO AKKA TESTING
TESTACTORREF
▸ TestActorRef - a special reference that comes with the
Akka TestKit.
▸ Test...
HITCHHIKER GUID TO AKKA TESTING
STICK TO BEHAVIOURAL TESTING
▸ Use messages.
▸ Single responsibility principle.
▸ Use prob...
HITCHHIKER GUID TO AKKA TESTING
DEMO TIME
HITCHHIKER GUID TO AKKA TESTING
USE BUILT IN ASSERTIONS
▸ Expecting messages
▸ Fishing for messages
def expectMsg[T](d: Du...
HITCHHIKER GUID TO AKKA TESTING
BUILT IN ASSERTIONS
▸ Waiting
▸ Death watching
def awaitCond(p: => Boolean, max: Duration,...
HITCHHIKER GUID TO AKKA TESTING
MORE BUILT IN ASSERTIONS …
expectMsg[T](d: Duration, msg: T): T
expectMsgPF[T](d: Duration...
HITCHHIKER GUID TO AKKA TESTING
FURTHER READING
▸ Scala test - select your flavor
▸ Testing Actor System
▸ Injecting test p...
Upcoming SlideShare
Loading in …5
×
Upcoming SlideShare
Adonai ufal
Next
Download to read offline and view in fullscreen.

1

Share

Download to read offline

Akka testing

Download to read offline

Talk about akka-testing at xpiryense scala meetup

Related Books

Free with a 30 day trial from Scribd

See all

Related Audiobooks

Free with a 30 day trial from Scribd

See all

Akka testing

  1. 1. AKKA UNIT TESTING HITCHHIKER GUID TO
  2. 2. HITCHHIKER GUID TO AKKA TESTING ABOUT ME ▸ Aprox 7 year hands on with scala & akka ▸ Part of Indeni team ▸ mailto: 123avi@gmail.com ▸ 052-3459959
  3. 3. HITCHHIKER GUID TO AKKA TESTING WHAT WILL BE COVERED ▸ Akka basics ▸ Settings (sbt) ▸ Simple actors app ▸ Actor messages testing ▸ UnderlyingActor ▸ Supervision testing
  4. 4. HITCHHIKER GUID TO AKKA TESTING THE ACTOR-MESSAGING CONCEPT IS NOT NEW ▸ Formalized in 1973 by Carl Hewitt and refined by Gul Agha in mid 80s. ▸ The first major adoption is done by Ericsson in mid 80s. ▸ Invented Erlang and later open-sourced in 90s. ▸ Built a distributed, concurrent, and fault-tolerant telcom system which has 99.9999999% uptime
  5. 5. HITCHHIKER GUID TO AKKA TESTING WHAT IS AKKA FRAMEWORK - TACHLES ▸ Event driving, messaging style, concurrent, fault-tolerant ▸ Provides higher abstraction for concurrency and parallelism ▸ No shared state ▸ Messages are kept in mailbox and processed in order ▸ Massive scalable and lighting fast because of the small call stack.
  6. 6. HITCHHIKER GUID TO AKKA TESTING SETTINGS - BUILD.SBT ▸ Dependencies libraryDependencies ++= Seq( "com.typesafe.akka" %% "akka-actor" % "2.4.8", "com.typesafe.akka" %% "akka-testkit" % "2.4.8" % "test", "org.scalatest" %% "scalatest" % "2.2.5" % "test" )
  7. 7. HITCHHIKER GUID TO AKKA TESTING AKKA TESTKIT ▸ Library to simplify unit tests on actors ▸ Can be extended by your test class ▸ Inheriting from this trait enables reception of replies from actors, which are queued by an internal actor and can be examined using the expectMsg... methods. Assertions and bounds concerning timing are available in the form of within blocks.
  8. 8. HITCHHIKER GUID TO AKKA TESTING TEST EVERYTHING
  9. 9. HITCHHIKER GUID TO AKKA TESTING MIXING IMPLICIT SENDER ▸ Use implicit sender to get “sender” replies def receive { case _ => sender ! “hi } class FooActorSpec extends TestKit(ActorSystem("MySpec")) with ImplicitSender foo ! “do something” expectMsg(“hi”) //without implicit sender foo tell( “do something”, self) expectMsg(“hi”)
  10. 10. HITCHHIKER GUID TO AKKA TESTING TESTACTORREF ▸ TestActorRef - a special reference that comes with the Akka TestKit. ▸ TestActorRef allows interaction with internal actor if needed via .underlyingActor ▸ obtain reference to the actor val actorRef = TestActorRef[MyActor] val actor = actorRef.underlyingActor actorRef.underlyingActor.someInner
  11. 11. HITCHHIKER GUID TO AKKA TESTING STICK TO BEHAVIOURAL TESTING ▸ Use messages. ▸ Single responsibility principle. ▸ Use probes ▸ make your tests readable and clear
  12. 12. HITCHHIKER GUID TO AKKA TESTING DEMO TIME
  13. 13. HITCHHIKER GUID TO AKKA TESTING USE BUILT IN ASSERTIONS ▸ Expecting messages ▸ Fishing for messages def expectMsg[T](d: Duration, msg: T): T def expectMsgPF[T](d: Duration) (pf: PartialFunction[Any, T]): T def expectNoMsg(d: Duration) // blocks def receiveN(n: Int, d: Duration): Seq[AnyRef] def receiveWhile[T](max: Duration, idle: Duration, n: Int) (pf: PartialFunction[Any, T]): Seq[T] def fishForMessage(max: Duration, hint: String) (pf: PartialFunction[Any, Boolean]): Any
  14. 14. HITCHHIKER GUID TO AKKA TESTING BUILT IN ASSERTIONS ▸ Waiting ▸ Death watching def awaitCond(p: => Boolean, max: Duration, interval: Duration) def eventually[T](fun: => T) (implicit config: PatienceConfig): T //not part of testkit val probe = TestProbe() probe watch target target ! PoisonPill probe.expectTerminated(target)
  15. 15. HITCHHIKER GUID TO AKKA TESTING MORE BUILT IN ASSERTIONS … expectMsg[T](d: Duration, msg: T): T expectMsgPF[T](d: Duration)(pf: PartialFunction[Any, T]): T expectMsgClass[T](d: Duration, c: Class[T]): T expectMsgType[T: Manifest](d: Duration) expectMsgAnyOf[T](d: Duration, obj: T*): T expectMsgAnyClassOf[T](d: Duration, obj: Class[_ <: T]*): T expectMsgAllOf[T](d: Duration, obj: T*): Seq[T] expectMsgAllClassOf[T](d: Duration, c: Class[_ <: T]*): Seq[T] expectMsgAllConformingOf[T](d: Duration, c: Class[_ <: T]*): Seq[T] expectNoMsg(d: Duration) receiveN(n: Int, d: Duration): Seq[AnyRef] fishForMessage(max: Duration, hint: String)(pf: PartialFunction[Any, Boolean]): Any receiveOne(d: Duration): AnyRef receiveWhile[T](max: Duration, idle: Duration, messages: Int)
  16. 16. HITCHHIKER GUID TO AKKA TESTING FURTHER READING ▸ Scala test - select your flavor ▸ Testing Actor System ▸ Injecting test probe for child actors ▸ Unit testing akka actors with testkit ▸ ScalaCheck user guide ▸ ScalaCheck examples ▸ Mocks are not Stubs ▸ Akka docs - multi-node-testing
  • AviLevi3

    Nov. 27, 2016

Talk about akka-testing at xpiryense scala meetup

Views

Total views

263

On Slideshare

0

From embeds

0

Number of embeds

0

Actions

Downloads

5

Shares

0

Comments

0

Likes

1

×