Having a need to test some methods which can cause a timeout, I want to create a helper function for that. It should return a Future that never completes:
def neverCompletes[T]: Future[T] = { ... }
But I wonder, how can I do that? I could that like the following:
def neverCompletes[T]: Future[T] = {
val p = Promise[T]()
future {
while(true) { }
} onComplete {
case x =>
p complete x // needed?
println("something went wrong!!!") // something is wrong...
}
p.future
}
But there should be a better way to achieve that out there. I also not sure whether p complete x // needed? is needed there.
Update:
In Scala 2.12 there will be a method Future.never that returns a future that never completes.
Easy thing. Just create a Promise and return its Future without ever completing it:
def neverCompletes[T]: Future[T] = Promise[T].future
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With