Package akka.testkit

Class TestKit

  • All Implemented Interfaces:
    TestKitBase
    Direct Known Subclasses:
    MultiNodeSpec, TestProbe

    public class TestKit
    extends java.lang.Object
    implements TestKitBase
    Test kit for testing actors. Inheriting from this class 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.

    
     class Test extends TestKit(ActorSystem()) {
       try {
    
         val test = system.actorOf(Props[SomeActor])
    
           within (1.second) {
             test ! SomeWork
             expectMsg(Result1) // bounded to 1 second
             expectMsg(Result2) // bounded to the remainder of the 1 second
           }
    
         } finally {
           system.terminate()
         }
    
       } finally {
         system.terminate()
       }
     }
     

    Beware of two points:

    - the ActorSystem passed into the constructor needs to be shutdown, otherwise thread pools and memory will be leaked - this class is not thread-safe (only one actor with one queue, one stack of within blocks); it is expected that the code is executed from a constructor as shown above, which makes this a non-issue, otherwise take care not to run tests within a single test class instance in parallel.

    It should be noted that for CI servers and the like all maximum Durations are scaled using their Duration.dilated method, which uses the TestKitExtension.Settings.TestTimeFactor settable via akka.conf entry "akka.test.timefactor".

    Since:
    1.1
    • Constructor Detail

    • Method Detail

      • testActorId

        public static java.util.concurrent.atomic.AtomicInteger testActorId()
        INTERNAL API
      • awaitCond

        public static boolean awaitCond​(scala.Function0<java.lang.Object> p,
                                        scala.concurrent.duration.Duration max,
                                        scala.concurrent.duration.Duration interval,
                                        boolean noThrow)
        Await until the given condition evaluates to true or the timeout expires, whichever comes first.
      • awaitCond$default$3

        public static scala.concurrent.duration.Duration awaitCond$default$3()
      • awaitCond$default$4

        public static boolean awaitCond$default$4()
      • now

        public static scala.concurrent.duration.Duration now()
        Obtain current timestamp as Duration for relative measurements (using System.nanoTime).
      • shutdownActorSystem

        public static void shutdownActorSystem​(ActorSystem actorSystem,
                                               scala.concurrent.duration.Duration duration,
                                               boolean verifySystemShutdown)
        Shut down an actor system and wait for termination. On failure debug output will be logged about the remaining actors in the system.

        If verifySystemShutdown is true, then an exception will be thrown on failure.

      • shutdownActorSystem$default$2

        public static scala.concurrent.duration.Duration shutdownActorSystem$default$2()
      • shutdownActorSystem$default$3

        public static boolean shutdownActorSystem$default$3()
      • testActor

        public ActorRef testActor()
        Description copied from interface: TestKitBase
        ActorRef of the test actor. Access is provided to enable e.g. registration as message target.
        Specified by:
        testActor in interface TestKitBase