Skip to content

Instantly share code, notes, and snippets.

@BenWhitehead
Last active August 29, 2015 14:05
Show Gist options
  • Save BenWhitehead/17e561dd2006dbcf0ec3 to your computer and use it in GitHub Desktop.
Save BenWhitehead/17e561dd2006dbcf0ec3 to your computer and use it in GitHub Desktop.
scala future testing
RuntimeException t: Kaboom
IllegalArgumentException t: Kaboom
- testing
import java.util.concurrent.Executors
import org.scalatest.FreeSpec
import scala.concurrent.{Future, Await, ExecutionContext}
import scala.concurrent.duration.DurationInt
import scala.util.Try
/**
* @author Ben Whitehead
*/
class FutureTesting extends FreeSpec {
"testing" in {
implicit val exec = Executors.newFixedThreadPool(16)
implicit val ctx = ExecutionContext.fromExecutorService(exec)
val f: Future[String] = futureTest
Try {Await.result(f, 1.minute)}
ctx.shutdown()
exec.shutdown()
}
def futureTest(implicit ctx: ExecutionContext): Future[String] = {
val f = scala.concurrent.future {
if (true) {
throw new IllegalArgumentException("Kaboom")
}
"Hello"
}
f onFailure { case t: RuntimeException =>
println(s"RuntimeException t: ${t.getMessage}")
assert(condition = true)
}
f onFailure { case t: IllegalArgumentException =>
println(s"IllegalArgumentException t: ${t.getMessage}")
assert(condition = true)
}
f onSuccess { case v =>
println(s"success: $v")
assert(condition = false)
}
f
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment