Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can one verify messages sent to self are delivered when testing Akka actors?

I have an Actor that is similar to the following Actor in function.

case class SupervisingActor() extends Actor {

    protected val processRouter = //round robin router to remote workers
    override def receive = {
        case StartProcessing => { //sent from main or someplace else
            for (some specified number of process actions ){
                processRouter ! WorkInstructions
            }
        }
        case ProcessResults(resultDetails) => {  //sent from the remote workers when they complete their work
            //do something with the results
            if(all of the results have been received){
                //*********************
                self ! EndProcess  //This is the line in question
                //*********************
            }
        }
        case EndProcess {
            //do some reporting
            //shutdown the ActorSystem
        }
    }
}

}

How can I verify the EndProcess message is sent to self in tests?

I'm using scalatest 2.0.M4, Akka 2.0.3 and Scala 1.9.2.

like image 807
Eric Reichert Avatar asked Sep 18 '12 17:09

Eric Reichert


People also ask

How can I send a message to an actor in Akka?

1) Akka Actor tell() Method It works on "fire-forget" approach. You can also use ! (bang) exclamation mark to send message. This is the preferred way of sending messages.

In which message delivery guarantee approach each message is guaranteed delivery to at least one of the components that retrieve messages from the queue?

at-least-once delivery means that for each message handed to the mechanism potentially multiple attempts are made at delivering it, such that at least one succeeds; again, in more casual terms this means that messages may be duplicated but not lost.

Is Akka actor a thread?

The good news is that Akka actors conceptually each have their own light-weight thread, which is completely shielded from the rest of the system. This means that instead of having to synchronize access using locks you can write your actor code without worrying about concurrency at all.

What is actor model in Akka?

Akka Actors The Actor Model provides a higher level of abstraction for writing concurrent and distributed systems. It alleviates the developer from having to deal with explicit locking and thread management, making it easier to write correct concurrent and parallel systems.


1 Answers

An actor sending to itself is very much an intimiate detail of how that actor performs a certain function, hence I would rather test the effect of that message than whether or not that message has been delivered. I’d argue that sending to self is the same as having a private helper method on an object in classical OOP: you also do not test whether that one is invoked, you test whether the right thing happened in the end.

As a side note: you could implement your own message queue type (see https://doc.akka.io/docs/akka/snapshot/mailboxes.html#creating-your-own-mailbox-type) and have that allow the inspection or tracing of message sends. The beauty of this approach is that it can be inserted purely by configuration into the actor under test.

like image 78
Roland Kuhn Avatar answered Oct 24 '22 22:10

Roland Kuhn