On Sun, Apr 26, 2015 at 1:05 AM, <[email protected]> wrote:

> Hello, everybody.
> I'm using a trait to override certain properties of an actor for testing
> purposes.
>
> trait TestingActor {
>   this: RealActor =>
>
>   private val someParameter =
>     Duration(context.system.settings.config.getDuration("some.parameter")
>
>   private val someOtherParameter = context.system.settings.config getInt
> "some.otherParameter"
>
>   override def createChildActor() =
>     context.actorOf(RealChildActor.props(someParameter, someOtherParameter
> ), "child-actor")
> }
>
> The trait is then mixed in inside the test suite like this:
>
> val testingActor = actor("test-case")(new RealActor(Int.MaxValue) with
> TestingActor)
>
> This allows the test to create another actor using the parameters defined
> in the test application.conf to make the tests run quicker by defining
> small period of times for events that have to happen throughout the actor's
> lifecycle.
>
> *The weird behavior is the following*: someParameter value is defined
> when the actor is instantiated but it's null when createChildActor is
> invoked. I've put a couple of console outputs in the trait to find this
> out. This behavior causes a NullPointerException that prevents the tests
> from passing.
>
> Defining someParameter as a def or simply putting the config lookup
> directly in the createChildActor method worked around the problem but as
> these tests are part of a suit provided for a course I've followed,
> I shouldn't be tampering with them. Furthermore, the people who followed
> the course with me didn't have the same problem (and couldn't help me find
> a solution, unfortunately).
>
> I've also noticed that someOtherParameter seems to have the same problem,
> as its value is actually the one in the test application.conf when the
> actor is instantiated but it's 0 when createChildActor is called.
>
> To spice things up a little bit, I hoped to have solved the problem by
> running sbt clean, but this even weirder thing happened: for a few times,
> cleaning up and recompiling made my tests go green just once, failing on
> subsequent sbt test runs without touching any code at all. After two or
> three times this clean up/recompile trick didn't work any longer, and now
> my tests always fail because of this problem. I'm *really* puzzled.
>
> I've tried to run my tests with activator (based on sbt 0.13.7-M5) and sbt
> 0.13.8, 0.13.7 and 0.13.6. I've also tried to compile everything with both
> Scala 2.11.2 and Scala 2.11.6.
> I'm running on OS X Yosemite 10.10.2 (Build 14C1514).
>
> Does anybody have any idea of what is going on? Thanks to all of you in
> advance.
>

You should be aware that when you extend traits, there is a certain
initialization order. So if the createChildActor() is called from another
composed trait during initialization, then it might refer to the field that
corresponds to a trait that has not yet been initialized. You can use lazy
vals, early initializers, or simple defs to get around this problem. (quite
an old post, but it explains the problem:
http://daily-scala.blogspot.co.at/2010/03/nullpointer-when-mixed-traits-warning.html
)

This is Scala and not Akka related though.

-Endre


>  --
> >>>>>>>>>> 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 [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/akka-user.
> For more options, visit https://groups.google.com/d/optout.
>



-- 
Akka Team
Typesafe - Reactive apps on the JVM
Blog: letitcrash.com
Twitter: @akkateam

-- 
>>>>>>>>>>      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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to