Skip to content

Instantly share code, notes, and snippets.

@etorreborre
Last active December 30, 2015 21:09
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 etorreborre/7885638 to your computer and use it in GitHub Desktop.
Save etorreborre/7885638 to your computer and use it in GitHub Desktop.
An Examples timeout trait for specs2 2.3.4 (this is a follow-up to this example: https://github.com/etorreborre/specs2/commit/e63582c3bb5a706140ecc05ce85693982113ad9e)
/**
* This trait can be used to add a global time out to each example or for a specific one:
*
* - for each example mix-in the trait
* - for a single example import the object and use the upTo context:
*
* my example must terminate in a reasonable amount of time ${upTo(3.seconds)(e1)}
*/
trait ExamplesTimeout extends AroundExample with MustMatchers with TerminationMatchers with CommandLineArguments with TimeConversions {
lazy val commandLineTimeOut = arguments.commandLine.int("timeout").map(_.millis)
lazy val defaultTimeOut = 1.minute
def timeout = commandLineTimeOut.getOrElse(defaultTimeOut)
def around[T : AsResult](t: =>T) = upTo(timeout).around(t)
def upTo(to: Duration) = new Around {
def around[T : AsResult](t: =>T) = {
lazy val result = t
val termination = result must terminate[T](retries = 10, sleep = (to.inMillis / 10).millis).orSkip((ko: String) => "TIMEOUT: "+to)
if (!termination.toResult.isSkipped) AsResult(result)
else termination.toResult
}
}
}
object ExamplesTimeout extends ExamplesTimeout
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment