A future that never ends

Having the need to test some methods that may cause a timeout, I want to create an auxiliary function for this. He must return a Future that never completes:

 def neverCompletes[T]: Future[T] = { ... } 

But I wonder how can I do this? I could do this:

 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 must be a better way to achieve this. I'm also not sure if p complete x // needed? .

+7
scala concurrency future
source share
1 answer

Update:

The Future.never method will appear in Scala 2.12, which returns a future that never ends.


Easy thing. Just create a Promise and return it Future without executing it:

 def neverCompletes[T]: Future[T] = Promise[T].future 
+24
source share

All Articles