Consider the following unit test example:
class MySpec extends AsyncFlatSpec {
"this" should "fail after some timeout" in {
val resultFuture: Future[_] = Promise().future
for (result <- resultFuture) yield {
assert(result == ???) // some assertions
assert(result == ???) // some assertions
assert(result == ???) // some assertions
}
}
}
The problem
If resultFuture
never completes the test suite never finishes either.
As I understand it's due to the way how SerialExecutionContext
is implemented.
The question
Is there any "nice" way how to setup a timeout for this kind of tests, so that if the future isn't complete the test just fails, instead of blocking the entire test suite for eternity?
UPDATE and clarification
While solutions https://stackoverflow.com/a/65746143/96766 and https://stackoverflow.com/a/65749840/96766 (posted by @matthias-berndt and @tomer-shetah) work for the case of blocked thread, it's not exactly what I'm looking for.
Let me make an important clarification to the question. In my case the future isn't eventually complete, but never complete. For example, when a Future
is obtained from the Promise
that is never resolved (nobody calls success
nor failure
on it). In that case the proposed solutions still block infinitely.
Is there a way to work this around for AsyncSpec
without resorting to using a real pool-based execution context and Await
-ing on the future?
Use
eventually
fromscalatest
Eventually