Skip to content

Instantly share code, notes, and snippets.

@ivportilla
Created February 16, 2017 20:47
Show Gist options
  • Save ivportilla/3da524e934a69156499037b7aef0bc97 to your computer and use it in GitHub Desktop.
Save ivportilla/3da524e934a69156499037b7aef0bc97 to your computer and use it in GitHub Desktop.
Blocking future test
test("blocking in Future") {
var threadsBlock: mutable.Set[String] = scala.collection.mutable.Set()
var threads: mutable.Set[String] = scala.collection.mutable.Set()
def currentThread = Thread.currentThread().getName
/*def nonBlockingFuture = Future{
threads += currentThread
Thread.sleep(1000)
}*/
def blockingFuture = Future{
blocking {
threadsBlock += currentThread
Thread.sleep(1000)
}
}
/*val futures = (1 to 20).map(_ => nonBlockingFuture)
val resolvedFutures = Future.sequence(futures)
Await.ready(resolvedFutures, Duration.Inf)*/
val futuresBlock = (1 to 20).map(_ => blockingFuture)
val resolvedFuturesBlock = Future.sequence(futuresBlock)
Await.ready(resolvedFuturesBlock, Duration.Inf)
println(threadsBlock.size)
assert(threadsBlock.size > threads.size)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment