Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Future which never completes

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.

like image 522
Incerteza Avatar asked Nov 28 '13 12:11

Incerteza


1 Answers

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
like image 121
drexin Avatar answered Sep 20 '22 08:09

drexin