Testing Actor Systems

Without any surprises, our very first step is to create an ActorSystem. Finally, we close our actor system by calling the usual system.terminate(). DonutStockWorkerActor simply calls the findStock() operation when it receives a CheckStock message.
Testing Actor Systems
Thereafter, we mixin the traits WordSpecLike, BeforeAndAfterAll and Matchers from scalatest. To complete our general structure for testing Akka Actors, we implement the afterAll() method to gracefully close our Actor System by calling TestKit.shutdownActorSystem() method. To illustrate this idea, we create another test, which sends a StopBaking event using the familiar Akka Tell Pattern. This test relies on the previous StartBaking event to have been reified within our automata. Similar to the previous code snippet, we use the handy awaitCond(), and the stateName property from TestFSMRef, to verify that the state has subsequently been flipped to the BakingStates.Stop state. When writing any software, especially code that will be used in a production context, you should aim to be a responsible developer and provide a bare minimum of software testing such as Unit Tests.

Tracing Actor Invocations

Any time you need to verify that an actor shuts down in a given scenario, you can use Watch in your tests. This pair nicely with ExpectTerminated(), which expects a Terminated message for a given IActorRef. The TestActor acts as the implicit sender of all messages sent to any of your actors during unit tests.

In the case of SOAP addresses, it’s modeling the DNS and the rest of the URL mapping. Behaviors also freed the actor model from implementation details, e.g., the Smalltalk-72 token stream interpreter. However, the efficient implementation of systems described by the actor model require extensive optimization. The idea that underpins the Screenplay Pattern is to capture your domain language and use your acceptance tests as an opportunity to demonstrate

Screenplay Pattern

how actors interacting with your system accomplish their goals. Abilities enable actors to perform interactions with the system under test.

  • For instance, an e-commerce website would want to get insights as to why certain customers are bouncing off from their shopping cart basket?
  • For the purpose of this tutorial, you can think of the ActorSystem as a ‘black’ box that will eventually hold your actors, and allow you to interact with them.
  • In the other case, the invocation is simply processed
    immediately on the current thread.
  • In comparison, the pattern-matching-based code makes it more immediately apparent what cases are being considered and how each is being handled.
  • This is the initial step of creating an Actor System named DonutStoreActorSystem.

The expected result of sending a message to this actor is that the vector contained in the message ends up sorted. As is the way, setting up a new project is rarely a trivial thing to do. It’s much easier to do for a C++ library project than it was for sbt (Scala’s built tool), though.

Browse versions

Following the PosionPill message, we will try and send another Info message to the actor. The diagram below from the official Akka documentation provides a good visual overview of the actor hierarchy as supported by Akka. We create the DonutStockActor into our DonutStoreActorSystem by calling the actorOf() method. We continue our discussion on the Akka Ask Pattern, and will show another handy utility named pipeTo(). It attaches to a Future operation by registering the Future andThen callback to allow you to easily send the result back to the sender.
Testing Actor Systems
Note that we are referencing our fixed-thread-pool dispatcher using the withDispatcher(…) method. A reminder that we created a custom dispatcher using a fixed number of threads in application.conf. To peek into Akka’s default dispatcher configuration, you need to access the akka.actor.default-dispatcher what is actor config. At the heart of the actor system is Akka’s dispatcher which, to quote the official Akka documentation, is what makes Akka Actors “tick”. The dispatcher, though, requires an executor to provide it with threads necessary to run operations in a non-blocking fashion.
Testing Actor Systems
To run this test within IntelliJ is similar to the previous ones by simply Right click somewhere within the class DonutQueryParametersTest and selecting the Run … We go ahead and initialise an instance of DonutBakingActor using the system.actorOf() method. I hope that the previous examples have presented a good case for using Akka when working with Finite State Machines.
Testing Actor Systems
This new trait expects two type parameters, namely one for states, and the other for mutable data. We then kick off the Actor’s initial state and data by calling the startWith(…) method. Rather than making use of the become() and unbecome() methods, the LoggingFSM trait provides a more refined when() closure.
Having said that, let us examine the inner-workings of trait DonutConfig. For the automatic wiring of the application.conf file into the above- mentioned classes, you are required to add import pureconfig.generic.auto._. Earlier on, we looked at the handling of JSON payload within an Akka HTTP server. As a reminder, other than adding the necessary Routing paths at the server side, we also demonstrated how to manually test or trigger sending a JSON payload over an HTTP POST request using the handy curl command. To illustrate this, we introduce yet another path(“ingredients-to-case-class”).

Leave a Comment

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

Scroll to Top