Skip to content

Instantly share code, notes, and snippets.

@mosser
Created February 25, 2015 16:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mosser/64482f1a9c7a295f098b to your computer and use it in GitHub Desktop.
Save mosser/64482f1a9c7a295f098b to your computer and use it in GitHub Desktop.
trait Timeout {
import scala.concurrent._
import duration._
import ExecutionContext.Implicits.global
def timeout[T](clock: Int)(block: => T): T = {
val future = Future { block }
Await.result(future, clock.milliseconds)
}
}
import org.specs2.mutable._
import org.junit.runner.RunWith
import org.specs2.runner.JUnitRunner
@RunWith(classOf[JUnitRunner])
class TimeoutTest extends SpecificationWithJUnit with Timeout {
"Timeout Specifications".title
def sleeping(seconds: Int): String = { Thread.sleep(seconds*1000); "beauty" }
"The timeout environment" should {
"execute the given code when in the timebox" in {
timeout(2000) { sleeping(1) } must_== "beauty"
}
"throw a TimeoutException when the given code exceeds the timebox" in {
timeout(500) { sleeping(1) } must throwA[java.util.concurrent.TimeoutException]
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment