jgagnon1 jgagnon1 - 2 months ago 11
Scala Question

Advantages of using ScalaFutures from ScalaTest vs. Await.result

I recently had a discussions with colleagues over the use or whenReady or .futureValue from ScalaFutures in ScalaTest versus Await.result in tests. Besides the readability, consistency in configuration (timeouts) and result scoping when using whenReady, is there any other noticeable advantages of using ScalaFutures methods over Await.result ?

I noticed that ScalaFutures use a "polling" technique instead of blocking like Await, but can someone elaborate over the advantages ?

Answer

When you use Await.result you are blocking the calling thread. The effect of this would be most noticeable if you ran your tests in a single-threaded manner (e.g. with an SBT setting like parallelExecution in Test := false).

The polling done by ScalaFutures is done (afaik) via a scheduler; hence, it doesn't block any thread. As a result you could have a number of concurrently running tests that far exceed the Threads available on whatever ExecutionContext the tests are actually running on.

Comments