Last active
August 29, 2015 14:05
-
-
Save BenWhitehead/17e561dd2006dbcf0ec3 to your computer and use it in GitHub Desktop.
scala future testing
This file contains 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
RuntimeException t: Kaboom | |
IllegalArgumentException t: Kaboom | |
- testing |
This file contains 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 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