score:15
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.
score:0
In the past, I have overridden the implementation for !
so that I could add debug/logging. Just call super.! when you're done, and be extra careful not to do anything that would throw an exception.
score:0
I had the same issue with an FSM actor. I tried setting up a custom mailbox as per the accepted answer but a few minutes didn't get it working. I also attempted to override the tell operator as per another answer but that was not possible as self is a final val. Eventually I just replaced:
self ! whatever
with:
sendToSelf(whatever)
and added that method into the actor as:
// test can override this
protected def sendToSelf(msg: Any) {
self ! msg
}
then in the test overrode the method to capture the self sent message and sent it back into the fsm to complete the work:
@transient var sent: Seq[Any] = Seq.empty
val fsm = TestFSMRef(new MyActor(x,yz) {
override def sendToSelf(msg: Any) {
sent = sent :+ msg
}
})
// yes this is clunky but it works
var wait = 100
while( sent.isEmpty && wait > 0 ){
Thread.sleep(10)
wait = wait - 10
}
fsm ! sent.head
Source: stackoverflow.com
Related Query
- How can one verify messages sent to self are delivered when testing Akka actors?
- How to recover messages in Akka Actors now that Durable Mailboxes are removed?
- How can I resolve conflicting actor systems while testing akka-http and akka actors at the same spec file?
- Testing Akka Typed actors - How can I get an ActorContext?
- How can I get Scalatest to give me one stats summary when testing in a loop
- How are akka actors implemented on underlying threads?
- How to mock child Actors for testing an Akka system?
- How are Akka messages ordered if messages are not reliable?
- In sbt, how can I cross-build with dependencies that are not required in one version?
- How can I reload akka scheduler when Play framework restart
- How to disable Akka error messages when client actor disconnect from remote actor?
- How can I send messages to a remote actor via CLI with Akka remoting?
- How to delete duplicates from a file when other fields are varying. We have to just remove the duplicates based on one column
- How to prioritize messages sent between actors in Scala?
- how can we get the result from various actors and then send the result from all the actors to one response in play 2.2.0?
- Scala: how can I perform actions when test are over?
- How does Akka 2.4.x work in A Cluster Application when I loose one of my nodes
- Ask Akka actor for a result only when all the messages are processed
- Receptionist not performing deduplication when there are multiple "subscribers" on one node in distributed pubsub Topic in akka typed
- How Akka Actors are clearing resources?
- How to determine when message was sent in Akka
- scala akka - Number of threads grows indefinitly even when actors are killed
- How to set up Akka actors to process messages in serial and in parallel based on predicate?
- Can akka actors pass messages back to the client a scenerio like this
- Scala / AKKA - how many actors are "working"?
- What are the suitable problems for Akka vs Gridgain? When is one more suitable than the other?
- My log messages are working for one file with akka typed, but not for classic actor
- Is it possible to know how many messages are in an actors mailbox?
- How can I throttle messages to the IO(Tcp) actor in Akka
- akka streams with spark streaming: messages are not delivered to actor; getting dead letters
More Query from same tag
- reducing an Array of Float using scala.math.max
- Play framework, Display special characters
- What's the *right* way to handle a POST in FP?
- No Encoder found for org.locationtech.jts.geom.Point
- Stream#filter Runs out of Memory for 1,000,000 items
- Spark job with Async HTTP call
- How to filter a mixed-node graph on neighbor vertex types
- Scala match both String and Array
- error: constructor cannot be instantiated to expected type
- scala: recursive function composition
- How can I reflectively call a method on a Scala object from Java?
- Error: value scala is not a package
- Maven - How to get the parent pom file dependency in sbt project
- saveAsTextFile method in spark
- Sort dataframe by element of vector
- Spark SQL has no SparkSqlParser.scala file when compiling in intelliJ idea
- Scala IDE for .NET Developers
- Spark split a column value into multiple rows
- Akka Flow hangs when making http requests via connection pool
- How to implement a custom tail recursive map for lists in Scala
- regarding train-test split of data in spark scala
- Quick way to insert a structured Map[String,Any] object into mongodb
- Sum of even-numbered items in Seq
- wrong stop statement declaration after define object
- ADT subtyping in Scala
- Parallel API requests using Spark and scala
- Supervise Any Non-Child Actor?
- Design a generic trait in Scala
- elastic4s - search in multiple fields
- How to extract hostname and port from URL string?