Java unit test and calling thread dispatcher for actor hierarchy

342 views
Skip to first unread message

Romain Gilles

unread,
Jan 3, 2014, 8:23:15 PM1/3/14
to akka...@googlegroups.com
Hi all,
I'm a beginner in akka. And I found it great so first of all thank you for this great job!
I'm would like to test my actor in Java and I would like to use the unit test approach as describe in the documentation. Therefore I'm using the TestActorRef.create(...) method to create my Actor under testing.
But unfortunately this actor create child actor for its context: getContext().actorOf(...).
I the second case the actor creation does not use the Calling thread dispatcher strategy. 
Is there a way to make this strategy global for all actor creation? For the moment I use a workaround by delegating the creation of the actor to a third component that I switch the implementation for the test cases.

Thanks in advance,

Romain.

Roland Kuhn

unread,
Jan 4, 2014, 8:31:45 AM1/4/14
to akka-user
Hi Romain,

There are two ways to achieve that: you can use .withDispatcher(context.props.dispatcher) when creating the child, or you can use a configuration file in your tests (typically src/test/resources/application.conf) which configures the CallingThreadDispatcher for "/*/childName" if the children are named uniformly; otherwise you can add the deployment section to the configuration you use when creating the ActorSystem in your test:

final Config config = ConfigFactory.parseString(
"  akka.actor.deployment {\n" +
"    /*/* {\n" +
"      dispatcher = \"akka.test.calling-thread-dispatcher\"\n" +
"    }}");
final ActorSystem system = ActorSystem.create("MyTest", config.withFallback(ConfigFactory.load()));

Regards,

Roland


Thanks in advance,

Romain.

--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ: http://akka.io/faq/
>>>>>>>>>> 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/groups/opt_out.



Dr. Roland Kuhn
Akka Tech Lead
Typesafe – Reactive apps on the JVM.
twitter: @rolandkuhn


Romain Gilles

unread,
Jan 5, 2014, 8:22:07 AM1/5/14
to akka...@googlegroups.com
Hi Roland,

First of all thank you for your help and sorry for the delay...
So I decide to use the third option because it look like less intrusive but even if I configure this it does not works:

    @BeforeClass
    public static void setupClass() throws Exception {
        final Config config = ConfigFactory.parseString(
                "  akka.actor.deployment {\n" +
                        "    default {\n" +
                        "        dispatcher = \"akka.test.calling-thread-dispatcher\"" +
                        "    }\n" +
                        "    default-dispatcher {\n" +
                        "        type=\"akka.testkit.CallingThreadDispatcher\"\n" +
                        "    }\n" +
                        "    \"/*/*\"  {\n" +
                        "        dispatcher = \"akka.test.calling-thread-dispatcher\"\n" +
                        "    }\n" +
                        "}");
        system = ActorSystem.create("test", config.withFallback(ConfigFactory.load()));
    }

So I fallback to the first option where I provide a constructor with an optional dispatcher Id and then us it on props at actor creation time on the context.

Any Ideas?

Thanks in advance,

Romain.

Roland Kuhn

unread,
Jan 5, 2014, 10:52:28 AM1/5/14
to akka-user
Hi Romain,

what exactly does not work? Please share a self-contained excerpt from your tests together with the expected and the actual output.

Regards,

Roland

Romain Gilles

unread,
Jan 6, 2014, 9:31:36 AM1/6/14
to akka...@googlegroups.com
Hi Roland,

Ok, I will try to isolate my code in a simple sample to show you if I can.
What I see is that if I run my unit test a couple of time I get failed test and when I look more closely I see that the thread-pool base dispatcher is used instead of the calling thread one.
Just for information I'm using the 2.3-M2 version.

Thanks,

Romain
Reply all
Reply to author
Forward
0 new messages