Waiting for actor to "become" x in test cases

269 views
Skip to first unread message

Kostas kougios

unread,
May 13, 2015, 2:09:59 AM5/13/15
to akka...@googlegroups.com
Hi,

Currently my actor becomes ready when it receives a msg :

class MyActor {
    private val mediator = DistributedPubSubExtension(context.system).mediator
    mediator ! Subscribe(Subscriptions.Index, self)

...
    override def receive = {
        case SubscribeAck(Subscribe(Subscriptions.Index, None, self)) =>
            context become ready
    }

    def ready =
        createPart.receive orElse
            retrievePart.receive

}

This creates problems in test cases, when I send a msg to my actor:

myActor ! Create(...) // this is received before SubscribeAck and hence it is ignored

My temp solution is to sleep a bit after creating an actor so that I give an opportunity to the mediator to send the SubscribeAck:

        val indexActor = system.actorOf(Props(IndexActor.empty(IntKey, StringValue, cluster.ref)))
        // TODO: replace this with something better. This is required so that the actor becomes ready
        Thread.sleep(5)


Is there a better solution?

Thanks
#

Martynas Mickevičius

unread,
May 16, 2015, 5:12:29 AM5/16/15
to akka...@googlegroups.com
Hi Kostas,

I see two ways in approaching this. First, you could use TestActorRef to test the insides of your actor. In this way you could reach to the underlying instance of your actor and test each Receive block independently in a unit test fashion.

Now another approach is akin to integration testing when you test an actor by sending and expecting messages. In such case the actor either has to signal to the outside by a message that it has become a particular actor, or you have to periodically send messages and wait for a response for a finite period of time.

--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ: http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to akka-user+...@googlegroups.com.
To post to this group, send email to akka...@googlegroups.com.
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.



--
Martynas Mickevičius
TypesafeReactive Apps on the JVM

Konstantinos Kougios

unread,
May 18, 2015, 2:01:03 AM5/18/15
to akka...@googlegroups.com
Thanks Martynas, currently I am trying the TestActorRef approach which works.
You received this message because you are subscribed to a topic in the Google Groups "Akka User List" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/akka-user/VpABoNODtNA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to akka-user+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages