joesan
joesan

Reputation: 15385

Scala Test Async and Await methods

I have a service that makes use of the Scala Async library. I'm using this library primarily to time my database calls. The method that I want to test contains multiple calls to the database using the async await mechanism. A pseudo code of what I have is as below:

def myDbMethod() = async {
  val firstCall = await(call the db and get the result)

  val secondCall = await(all the db and get the result)

  val thirdCall = await(all the db and get the result)
  ...
}

In my Scala test unit test, I have

Await.result(myDbMethod(), 10.seconds)

I was just trying to debug myMethod by running my unit test which would return with a test success even before getting into the secondCall. I mean I had breakpoints in all the 3 calls to the database, but the IntelliJ debugger would just exit out as soon as it finishes the first call to the database. Why is this? How can I test this behaviour using IntelliJ debugger?

Upvotes: 0

Views: 2066

Answers (2)

4lex1v
4lex1v

Reputation: 21557

I'm not sure that my answer would suffice your expectations, but it's a known issue. The problem is that async/await is quite a complicated macro, which does heavy transformations on the trees (you can check the output by enabling -Xprint:<phase_name_after_typer> flag). Unfortunately neither of existing IDEs (I'm working with Intellij and Ensime) can debug it, but I'm not familiar with their internals to explain why they can't in details.

From my experience I couldn't find any neat pros over the native for-comprehension, so you can stick with th native syntax or explicit flatmap calls, which is nicely debuggable.

Upvotes: 2

Andreas Neumann
Andreas Neumann

Reputation: 10894

This construct could be used for depdendant asynchronous calls. async / await adds some sugar to make that easier, but to formalate that by hand you can do it like this:

def dbMethodDependant : Future[T] = for {
  res1 <- firstCall
  res2 <- secondCall(res2)
  res3 <- thirdCall(res3)
} yield res3

Await.result(dbMethodDependant, forever)

Upvotes: 0

Related Questions