diff options
author | phaller <hallerp@gmail.com> | 2012-11-26 17:28:51 +0100 |
---|---|---|
committer | phaller <hallerp@gmail.com> | 2012-11-26 23:26:16 +0100 |
commit | 9ea2cc44e98c110843780aef09c7d1a695458be3 (patch) | |
tree | 3b7eb7ad3c8e6b983675a020d1f4fe7edb72bdca /src/test/scala | |
parent | 3fd51865dfcb6121f84145f4504abd0f80bf6cca (diff) | |
download | scala-async-9ea2cc44e98c110843780aef09c7d1a695458be3.tar.gz scala-async-9ea2cc44e98c110843780aef09c7d1a695458be3.tar.bz2 scala-async-9ea2cc44e98c110843780aef09c7d1a695458be3.zip |
Fix #42 - Futures created by async are not properly completed with exceptions
This augments the on-complete handler for an async state with await as follows:
if (tr.isFailure)
result$async.complete(tr.asInstanceOf[Try[T]])
else {
<resultName> = tr.get.asInstanceOf[<resultType>]
<nextState>
<mkResumeApply>
}
Diffstat (limited to 'src/test/scala')
-rw-r--r-- | src/test/scala/scala/async/run/exceptions/ExceptionsSpec.scala | 39 | ||||
-rw-r--r-- | src/test/scala/scala/async/run/futures/FutureSpec.scala | 34 |
2 files changed, 58 insertions, 15 deletions
diff --git a/src/test/scala/scala/async/run/exceptions/ExceptionsSpec.scala b/src/test/scala/scala/async/run/exceptions/ExceptionsSpec.scala new file mode 100644 index 0000000..6f9e4ec --- /dev/null +++ b/src/test/scala/scala/async/run/exceptions/ExceptionsSpec.scala @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2012 Typesafe Inc. <http://www.typesafe.com> + */ + +package scala.async +package run +package exceptions + +import scala.async.Async.{async, await} + +import scala.concurrent.{future, ExecutionContext, Await} +import ExecutionContext.Implicits._ +import scala.concurrent.duration._ +import scala.reflect.ClassTag + +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 + +@RunWith(classOf[JUnit4]) +class ExceptionsSpec { + + @Test + def `complete future with exception`() { + val future0 = future[Any] { + "five!".length + } + + val future2 = async { + val a = await(future0.mapTo[Int]) // result: 5 + val b = await((future { (a * 2).toString }).mapTo[Int]) // result: 10 + val c = await(future { (7 * 2).toString }) // result: "14" + b + "-" + c + } + + intercept[ClassCastException] { Await.result(future2, 5.seconds) } + } + +} diff --git a/src/test/scala/scala/async/run/futures/FutureSpec.scala b/src/test/scala/scala/async/run/futures/FutureSpec.scala index 7a486b5..01c8620 100644 --- a/src/test/scala/scala/async/run/futures/FutureSpec.scala +++ b/src/test/scala/scala/async/run/futures/FutureSpec.scala @@ -101,11 +101,12 @@ class FutureSpec { b + "-" + c } - val future2 = for { - a <- future0.mapTo[Int] - b <- (future { (a * 2).toString }).mapTo[Int] - c <- future { (7 * 2).toString } - } yield b + "-" + c + val future2 = async { + val a = await(future0.mapTo[Int]) + val b = await((future { (a * 2).toString }).mapTo[Int]) + val c = await(future { (7 * 2).toString }) + b + "-" + c + } Await.result(future1, defaultTimeout) mustBe ("10-14") //assert(checkType(future1, manifest[String])) @@ -139,20 +140,23 @@ class FutureSpec { @Test def `recover from exceptions`() { val future1 = Future(5) - val future2 = future1 map (_ / 0) - val future3 = future2 map (_.toString) - - val future4 = future1 recover { + val future2 = async { await(future1) / 0 } + val future3 = async { await(future2).toString } + + val future1Recovered = future1 recover { case e: ArithmeticException => 0 - } map (_.toString) + } + val future4 = async { await(future1Recovered).toString } - val future5 = future2 recover { + val future2Recovered = future2 recover { case e: ArithmeticException => 0 - } map (_.toString) + } + val future5 = async { await(future2Recovered).toString } - val future6 = future2 recover { + val future2Recovered2 = future2 recover { case e: MatchError => 0 - } map (_.toString) + } + val future6 = async { await(future2Recovered2).toString } val future7 = future3 recover { case e: ArithmeticException => "You got ERROR" @@ -527,7 +531,7 @@ class FutureSpec { @Test def `should not throw when Await.ready`() { val expected = try Success(5 / 0) catch { case a: ArithmeticException => Failure(a) } - val f = future(5).map(_ / 0) + val f = async { await(future(5)) / 0 } Await.ready(f, defaultTimeout).value.get.toString mustBe expected.toString } |