So I have some server code with an endpoint, where I want to run stuff asynchronously. To that end I'm using futures. There are tasks that lead to the result, but there are also expensive IO tasks that don't, so I perform them in a fire and forget Future like so:
import scala.concurrent.Future
val ec = scala.concurrent.ExecutionContext.global
Future {
Thread.sleep(10000)
println("Done")
}(ec)
println("Exiting now")
Output of that is:
import scala.concurrent.Future
ec: scala.concurrent.ExecutionContextExecutor = scala.concurrent.impl.ExecutionContextImpl@2315ca48
res0: scala.concurrent.Future[Unit] = scala.concurrent.impl.Promise$DefaultPromise@7d1e50cb
Exiting now
res1: Unit = ()
Normally on the server this is not that big of a problem, because the context keeps running as it receives other tasks and so the IO can finish. But when I test, the test finishes and the tasks inside start throwing, because the services they use become unavailable.
So the question is: How do I wait for these fire and forget futures without also blocking my main execution?