AskTimeoutException in test code

204 views
Skip to first unread message

Bruce

unread,
May 1, 2012, 1:54:02 PM5/1/12
to akka...@googlegroups.com
Hi,

I've got an AskTimeoutException in some test code that I'm not sure how to fix.  

[error] Could not run test com.myfirm.abcTest.ABCTestSpec: java.util.concurrent.TimeoutException: Futures timed out after [5000] milliseconds
[error] Error: Total 0, Failed 0, Errors 0, Passed 0, Skipped 0
[error] Error during tests:
[error] com.myfirm.abcTest.ABCTestSpec
[error] {file:/../../../MyProject}MyProject/test:test: Tests unsuccessful
[error] Total time: 7 s, completed May 1, 2012 1:42:44 PM


I'm basically hacking some code together just to figure out what I have available to me w.r.t. testing.  Also, I haven't tried to use akka TestKit or ScalaTest prior to this, so please excuse any glaring omissions.  So far, I have the following do-nothing code.  Is it just a matter of changing the timeout in the configuration?  If so, which value do I change?  Also, is there going to be an issue with accessing the underlying actor both through it's actorRef and directly?

Thanks in advance,
Bruce

import org.scalatest._
import org.scalatest.matchers._
import akka.actor._
import akka.actor.Actor._
import akka.util.duration._
import akka.testkit._
import java.util.concurrent.TimeUnit
import com.typesafe.config._
import util.Random
import com.myfirm.messages._
import com.myfirm.abc._


class ABCTestSpec extends FlatSpec with ShouldMatchers {
implicit val system = ActorSystem("system", ConfigFactory.load())

// system.actorOf(Props(new ABC),"abc")

val abcRef = TestActorRef[ABC]
val abc = abcRef.underlyingActor


"This" should "do that" in {
   abcRef ! "blah"
   abc.computeValue("XYZ")
   abc.valueMap("XYZ") should be(0.0)
}
}

Roland Kuhn

unread,
May 1, 2012, 3:25:28 PM5/1/12
to akka...@googlegroups.com
Hi Bruce,

On May 1, 2012, at 19:54 , Bruce wrote:

I've got an AskTimeoutException in some test code that I'm not sure how to fix.  

[error] Could not run test com.myfirm.abcTest.ABCTestSpec: java.util.concurrent.TimeoutException: Futures timed out after [5000] milliseconds

“Could not run test” means that it happened in the constructor, you can verify by typing “last” in the SBT shell after this failure.

[error] Error: Total 0, Failed 0, Errors 0, Passed 0, Skipped 0
[error] Error during tests:
[error] com.myfirm.abcTest.ABCTestSpec
[error] {file:/../../../MyProject}MyProject/test:test: Tests unsuccessful
[error] Total time: 7 s, completed May 1, 2012 1:42:44 PM


I'm basically hacking some code together just to figure out what I have available to me w.r.t. testing.  Also, I haven't tried to use akka TestKit or ScalaTest prior to this, so please excuse any glaring omissions.  So far, I have the following do-nothing code.  Is it just a matter of changing the timeout in the configuration?

Yes, .underlyingActor uses ask() internally because creating an actor is asynchronous.

 If so, which value do I change?

akka.test.default-timeout. Looking at it this way, there should probably be a different setting just for this.

 Also, is there going to be an issue with accessing the underlying actor both through it's actorRef and directly?

No, that is exactly what it is made for. TestActorRef changes the default to CallingThreadDispatcher, which means that sending to the ref is synchronous and you can directly inspect the changed state.

Regards,

Roland


Thanks in advance,
Bruce

import org.scalatest._
import org.scalatest.matchers._
import akka.actor._
import akka.actor.Actor._
import akka.util.duration._
import akka.testkit._
import java.util.concurrent.TimeUnit
import com.typesafe.config._
import util.Random
import com.myfirm.messages._
import com.myfirm.abc._


class ABCTestSpec extends FlatSpec with ShouldMatchers {
implicit val system = ActorSystem("system", ConfigFactory.load())
// system.actorOf(Props(new ABC),"abc")
val abcRef = TestActorRef[ABC]
val abc = abcRef.underlyingActor

"This" should "do that" in {
   abcRef ! "blah"
   abc.computeValue("XYZ")
   abc.valueMap("XYZ") should be(0.0)
}
}

--
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To view this discussion on the web visit https://groups.google.com/d/msg/akka-user/-/5TDuwcBqBbsJ.
To post to this group, send email to akka...@googlegroups.com.
To unsubscribe from this group, send email to akka-user+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/akka-user?hl=en.

Roland Kuhn
Typesafe – The software stack for applications that scale.
twitter: @rolandkuhn


Bruce

unread,
May 2, 2012, 1:23:07 PM5/2/12
to akka...@googlegroups.com
Hi Roland,

I've confirmed via using a simple actor that everything works properly, but when I change to my real actor it times out.  I can't see the timeout parameter defined anywhere in application.conf, and I'm not sure how to change it.  Can you please guide me through this?  Is it just a matter of adding akka.test.default-timeout to the application.conf?  If so, where should it go?

Thanks again,
Bruce
Hi Bruce,

To unsubscribe from this group, send email to akka-user+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/akka-user?hl=en.

Roland Kuhn

unread,
May 2, 2012, 2:11:13 PM5/2/12
to akka...@googlegroups.com
Hi Bruce,

it would not hurt to read http://doc.akka.io/docs/akka/2.0.1/general/configuration.html, you should be able to figure it out from there (if not we’d have to improve that page).

Regards,

Roland

To view this discussion on the web visit https://groups.google.com/d/msg/akka-user/-/fBLLlsJ8PIoJ.

To post to this group, send email to akka...@googlegroups.com.
To unsubscribe from this group, send email to akka-user+...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/akka-user?hl=en.

Bruce

unread,
May 2, 2012, 3:16:11 PM5/2/12
to akka...@googlegroups.com
Thanks Roland.  I read the configuration docs, and was able to change the value.  The test is still timing out, even if I set the value to 20s, which shows me that there is something else going on.

Bruce

unread,
May 2, 2012, 3:57:23 PM5/2/12
to akka...@googlegroups.com
I found the following error in the log file.  I have my actor defined within a helper object AND I'm using actorOf(Props(new MyActor)).  The "OR" in the error message suggests I should only be doing one of these.  In any event, getting closer...

2012-05-02 15:49:42,276 INFO [test-akka.actor.default-dispatcher-1] a.e.s.Slf4jEventHandler [Slf4jEventHandler.scala:67] Slf4jEventHandler started
2012-05-02 15:49:42,286 DEBUG [test-akka.actor.default-dispatcher-1] a.e.EventStream [Slf4jEventHandler.scala:63] logger log1-Slf4jEventHandler started
2012-05-02 15:49:42,286 DEBUG [test-akka.actor.default-dispatcher-1] a.e.EventStream [Slf4jEventHandler.scala:63] Default Loggers started
2012-05-02 15:49:42,377 DEBUG [test-akka.actor.default-dispatcher-3] a.a.LocalActorRefProvider$Guardian [Slf4jEventHandler.scala:63] now supervising TestActor[akka://test/user/$$a]
2012-05-02 15:49:42,426 ERROR [test-akka.actor.default-dispatcher-3] a.t.TestActorRef$$anon$1 [Slf4jEventHandler.scala:47] error while creating actor
akka.actor.ActorInitializationException: Could not instantiate Actor
Make sure Actor is NOT defined inside a class/trait,
if so put it outside the class/trait, f.e. in a companion object,
OR try to change: 'actorOf(Props[MyActor]' to 'actorOf(Props(new MyActor)'.

Bruce

unread,
May 2, 2012, 5:12:00 PM5/2/12
to akka...@googlegroups.com
Partial success, but I'm not quite there yet.  If I have the following code:

class Junk extends Actor {

   val log = Logging(context.system,this)


   def colour = "Blue"


   def receive = {

      case msg => println("blah")

   }

}


object Junk {

   val config = ConfigFactory.load()

   val system = ActorSystem("system", config)

   val junk = system.actorOf((Props(new Junk)),"junk")

}


class JunkSpec extends TestKit(ActorSystem("test")) with FlatSpec with ShouldMatchers {

   val junkRef = TestActorRef[Junk]

   val junk = junkRef.underlyingActor


   "The colour" should "be Blue" in {

junkRef ! "blah"

junk.colour should be ("Blue")

   }

}


The run is successful, and has the following output:

blah
[info] JunkSpec:
[info] The colour 
[info] - should be Blue
[info] Passed: : Total 1, Failed 0, Errors 0, Passed 1, Skipped 0
[success] Total time: 17 s, completed May 2, 2012 4:53:56 PM

However, if I pass arguments into the actor class

class Junk(num:Int) extends Actor {

   val log = Logging(context.system,this)


   def colour = "Blue"


   def receive = {

    case msg => println("blah")

   }

}


object Junk {

   val config = ConfigFactory.load()

   val system = ActorSystem("system", config)

   val junk = system.actorOf((Props(new Junk(10))),"junk")

}


then the test fails

[error] Error: Total 0, Failed 0, Errors 0, Passed 0, Skipped 0
[error] Error during tests:
[error] com.myFirm.junkTest.JunkSpec
[error] {file:/_/_/_/MyProject/}MyProject/test:test: Tests unsuccessful
[error] Total time: 18 s, completed May 2, 2012 4:57:36 PM

With the following log output:

012-05-02 17:05:17,500 INFO [test-akka.actor.default-dispatcher-2] a.e.s.Slf4jEventHandler [Slf4jEventHandler.scala:67] Slf4jEventHandler started
2012-05-02 17:05:17,505 DEBUG [test-akka.actor.default-dispatcher-2] a.e.EventStream [Slf4jEventHandler.scala:63] logger log1-Slf4jEventHandler started
2012-05-02 17:05:17,505 DEBUG [test-akka.actor.default-dispatcher-2] a.e.EventStream [Slf4jEventHandler.scala:63] Default Loggers started
2012-05-02 17:05:17,526 DEBUG [test-akka.actor.default-dispatcher-1] a.a.LocalActorRefProvider$Guardian [Slf4jEventHandler.scala:63] now supervising TestActor[akka://test/user/$$a]
2012-05-02 17:05:17,562 ERROR [test-akka.actor.default-dispatcher-1] a.t.TestActorRef$$anon$1 [Slf4jEventHandler.scala:47] error while creating actor
akka.actor.ActorInitializationException: Could not instantiate Actor
Make sure Actor is NOT defined inside a class/trait,
if so put it outside the class/trait, f.e. in a companion object,
OR try to change: 'actorOf(Props[MyActor]' to 'actorOf(Props(new MyActor)'.
java.lang.NoSuchMethodException: com.myFirm.junk.Junk.<init>()

I can see that the problem is with the last line.  Can someone please explain it to me?

Thanks,
Bruce

√iktor Ҡlang

unread,
May 2, 2012, 5:23:01 PM5/2/12
to akka...@googlegroups.com
Hi Bruce,

You didn't change: val junkRef = TestActorRef[Junk]

That'll try to create the Junk without any constructor parameters, but you have no default constructor in Junk anymore and therefor it goes bye-bye.

Cheers,

To view this discussion on the web visit https://groups.google.com/d/msg/akka-user/-/ZE7UnnaBE94J.

To post to this group, send email to akka...@googlegroups.com.
To unsubscribe from this group, send email to akka-user+...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/akka-user?hl=en.



--
Viktor Klang

Akka Tech Lead
Typesafe - The software stack for applications that scale

Twitter: @viktorklang

Bruce

unread,
May 2, 2012, 6:19:30 PM5/2/12
to akka...@googlegroups.com
Gotcha.  Thanks Viktor, it works now.

Cheers,
Bruce


On Wednesday, 2 May 2012 17:23:01 UTC-4, √ wrote:
Hi Bruce,

You didn't change: val junkRef = TestActorRef[Junk]

That'll try to create the Junk without any constructor parameters, but you have no default constructor in Junk anymore and therefor it goes bye-bye.

Cheers,
Reply all
Reply to author
Forward
0 new messages