Created
February 25, 2015 16:22
-
-
Save mosser/64482f1a9c7a295f098b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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