Last active
December 16, 2015 20:49
-
-
Save msulima/5495242 to your computer and use it in GitHub Desktop.
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 scala.concurrent.duration._ | |
import scala.concurrent.{Await, Future} | |
import scala.concurrent.ExecutionContext.Implicits.global | |
import org.scalatest.FunSuite | |
class ErrorsTest extends FunSuite { | |
test("Future can recover from error") { | |
val numberFuture: Future[Int] = generateNumber() | |
val addedFuture: Future[Int] = numberFuture.map(addThree).recover({ | |
case exception: IllegalStateException => -1 | |
}) | |
val added: Int = Await.result(addedFuture, 1 seconds) | |
assert(added == -1) | |
} | |
private def generateNumber(): Future[Int] = Future { | |
Thread.sleep(100) | |
throw new IllegalStateException() | |
} | |
def addThree(value: Int): Int = | |
value + 3 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment