summaryrefslogtreecommitdiff
path: root/test/files/jvm/future-spec
diff options
context:
space:
mode:
authorHeather Miller <heather.miller@epfl.ch>2012-08-04 21:55:35 +0200
committerHeather Miller <heather.miller@epfl.ch>2012-08-04 21:55:35 +0200
commit3cb0e784a05db7d0b542cec9bf4c5fbf3772a6cf (patch)
tree36c1a6040e0f0aa6e64c0eef6fb55bea61bf4141 /test/files/jvm/future-spec
parentab63cca87f68d80aff0ff6cd83ecd85b9e1d0c7a (diff)
downloadscala-3cb0e784a05db7d0b542cec9bf4c5fbf3772a6cf.tar.gz
scala-3cb0e784a05db7d0b542cec9bf4c5fbf3772a6cf.tar.bz2
scala-3cb0e784a05db7d0b542cec9bf4c5fbf3772a6cf.zip
Basing Futures on Try instead of Either
Diffstat (limited to 'test/files/jvm/future-spec')
-rw-r--r--test/files/jvm/future-spec/FutureTests.scala19
-rw-r--r--test/files/jvm/future-spec/PromiseTests.scala14
-rw-r--r--test/files/jvm/future-spec/TryTests.scala119
-rw-r--r--test/files/jvm/future-spec/main.scala4
4 files changed, 139 insertions, 17 deletions
diff --git a/test/files/jvm/future-spec/FutureTests.scala b/test/files/jvm/future-spec/FutureTests.scala
index 30e1a722bf..31bb8c4e44 100644
--- a/test/files/jvm/future-spec/FutureTests.scala
+++ b/test/files/jvm/future-spec/FutureTests.scala
@@ -6,6 +6,7 @@ import scala.concurrent.util.duration._
import scala.concurrent.util.Duration.Inf
import scala.collection._
import scala.runtime.NonLocalReturnControl
+import scala.util.{Try,Success,Failure}
@@ -197,7 +198,7 @@ object FutureTests extends MinimalScalaTest {
} andThen {
case _ => q.add(2)
} andThen {
- case Right(0) => q.add(Int.MaxValue)
+ case Success(0) => q.add(Int.MaxValue)
} andThen {
case _ => q.add(3);
}
@@ -260,13 +261,13 @@ object FutureTests extends MinimalScalaTest {
}
val futures = (0 to 9) map {
- idx => async(idx, idx * 200)
+ idx => async(idx, idx * 20)
}
val folded = Future.fold(futures)(0)(_ + _)
Await.result(folded, timeout) mustBe (45)
val futuresit = (0 to 9) map {
- idx => async(idx, idx * 200)
+ idx => async(idx, idx * 20)
}
val foldedit = Future.fold(futures)(0)(_ + _)
Await.result(foldedit, timeout) mustBe (45)
@@ -279,7 +280,7 @@ object FutureTests extends MinimalScalaTest {
add
}
def futures = (0 to 9) map {
- idx => async(idx, idx * 200)
+ idx => async(idx, idx * 20)
}
val folded = futures.foldLeft(Future(0)) {
case (fr, fa) => for (r <- fr; a <- fa) yield (r + a)
@@ -295,7 +296,7 @@ object FutureTests extends MinimalScalaTest {
add
}
def futures = (0 to 9) map {
- idx => async(idx, idx * 100)
+ idx => async(idx, idx * 10)
}
val folded = Future.fold(futures)(0)(_ + _)
intercept[IllegalArgumentException] {
@@ -326,7 +327,7 @@ object FutureTests extends MinimalScalaTest {
"shouldReduceResults" in {
def async(idx: Int) = future {
- Thread.sleep(idx * 200)
+ Thread.sleep(idx * 20)
idx
}
val timeout = 10000 millis
@@ -348,7 +349,7 @@ object FutureTests extends MinimalScalaTest {
}
val timeout = 10000 millis
def futures = (1 to 10) map {
- idx => async(idx, idx * 100)
+ idx => async(idx, idx * 10)
}
val failed = Future.reduce(futures)(_ + _)
intercept[IllegalArgumentException] {
@@ -472,7 +473,7 @@ object FutureTests extends MinimalScalaTest {
p1.future.isCompleted mustBe (false)
f4.isCompleted mustBe (false)
- p1 complete Right("Hello")
+ p1 complete Success("Hello")
Await.ready(latch(7), TestLatch.DefaultTimeout)
@@ -509,7 +510,7 @@ object FutureTests extends MinimalScalaTest {
}
"should not throw when Await.ready" in {
- val expected = try Right(5 / 0) catch { case a: ArithmeticException => Left(a) }
+ val expected = try Success(5 / 0) catch { case a: ArithmeticException => Failure(a) }
val f = future(5).map(_ / 0)
Await.ready(f, defaultTimeout).value.get.toString mustBe expected.toString
}
diff --git a/test/files/jvm/future-spec/PromiseTests.scala b/test/files/jvm/future-spec/PromiseTests.scala
index d15bb31f36..d9aaa1d5ed 100644
--- a/test/files/jvm/future-spec/PromiseTests.scala
+++ b/test/files/jvm/future-spec/PromiseTests.scala
@@ -6,7 +6,7 @@ import scala.concurrent.util.duration._
import scala.concurrent.util.Duration.Inf
import scala.collection._
import scala.runtime.NonLocalReturnControl
-
+import scala.util.{Try,Success,Failure}
object PromiseTests extends MinimalScalaTest {
@@ -48,27 +48,27 @@ object PromiseTests extends MinimalScalaTest {
"A successful Promise" should {
val result = "test value"
- val promise = Promise[String]().complete(Right(result))
+ val promise = Promise[String]().complete(Success(result))
promise.isCompleted mustBe (true)
futureWithResult(_(promise.future, result))
}
"A failed Promise" should {
val message = "Expected Exception"
- val promise = Promise[String]().complete(Left(new RuntimeException(message)))
+ val promise = Promise[String]().complete(Failure(new RuntimeException(message)))
promise.isCompleted mustBe (true)
futureWithException[RuntimeException](_(promise.future, message))
}
"An interrupted Promise" should {
val message = "Boxed InterruptedException"
- val future = Promise[String]().complete(Left(new InterruptedException(message))).future
+ val future = Promise[String]().complete(Failure(new InterruptedException(message))).future
futureWithException[ExecutionException](_(future, message))
}
"A NonLocalReturnControl failed Promise" should {
val result = "test value"
- val future = Promise[String]().complete(Left(new NonLocalReturnControl[String]("test", result))).future
+ val future = Promise[String]().complete(Failure(new NonLocalReturnControl[String]("test", result))).future
futureWithResult(_(future, result))
}
@@ -76,7 +76,7 @@ object PromiseTests extends MinimalScalaTest {
"be completed" in { f((future, _) => future.isCompleted mustBe (true)) }
- "contain a value" in { f((future, result) => future.value mustBe (Some(Right(result)))) }
+ "contain a value" in { f((future, result) => future.value mustBe (Some(Success(result)))) }
"return when ready with 'Await.ready'" in { f((future, result) => Await.ready(future, defaultTimeout).isCompleted mustBe (true)) }
@@ -159,7 +159,7 @@ object PromiseTests extends MinimalScalaTest {
"contain a value" in {
f((future, message) => {
- future.value.get.left.get.getMessage mustBe (message)
+ future.value.get.failed.get.getMessage mustBe (message)
})
}
diff --git a/test/files/jvm/future-spec/TryTests.scala b/test/files/jvm/future-spec/TryTests.scala
new file mode 100644
index 0000000000..9d749c44ba
--- /dev/null
+++ b/test/files/jvm/future-spec/TryTests.scala
@@ -0,0 +1,119 @@
+
+// This is a port of the com.twitter.util Try spec.
+// --
+// It lives in the future-spec directory simply because it requires a specs-like
+// DSL which has already been minimally implemented for the future spec tests.
+
+import scala.util.{Try,Success,Failure}
+
+object TryTests extends MinimalScalaTest {
+ class MyException extends Exception
+ val e = new Exception("this is an exception")
+
+ "Try()" should {
+ "catch exceptions and lift into the Try type" in {
+ Try[Int](1) mustEqual Success(1)
+ Try[Int] { throw e } mustEqual Failure(e)
+ }
+ }
+
+ "Try" should {
+ "recoverWith" in {
+ val myException = new MyException
+ Success(1) recoverWith { case _ => Success(2) } mustEqual Success(1)
+ Failure(e) recoverWith { case _ => Success(2) } mustEqual Success(2)
+ Failure(e) recoverWith { case _ => Failure(e) } mustEqual Failure(e)
+ }
+
+ "getOrElse" in {
+ Success(1) getOrElse 2 mustEqual 1
+ Failure(e) getOrElse 2 mustEqual 2
+ }
+
+ "orElse" in {
+ Success(1) orElse Success(2) mustEqual Success(1)
+ Failure(e) orElse Success(2) mustEqual Success(2)
+ }
+
+ "map" in {
+ "when there is no exception" in {
+ Success(1) map(1+) mustEqual Success(2)
+ Failure[Int](e) map(1+) mustEqual Failure(e)
+ }
+
+ "when there is an exception" in {
+ Success(1) map(_ => throw e) mustEqual Failure(e)
+
+ val e2 = new Exception
+ Failure[Int](e) map(_ => throw e2) mustEqual Failure(e)
+ }
+ }
+
+ "flatMap" in {
+ "when there is no exception" in {
+ Success(1) flatMap(x => Success(1 + x)) mustEqual Success(2)
+ Failure[Int](e) flatMap(x => Success(1 + x)) mustEqual Failure(e)
+ }
+
+ "when there is an exception" in {
+ Success(1).flatMap[Int](_ => throw e) mustEqual Failure(e)
+
+ val e2 = new Exception
+ Failure[Int](e).flatMap[Int](_ => throw e2) mustEqual Failure(e)
+ }
+ }
+
+ "flatten" in {
+ "is a Success(Success)" in {
+ Success(Success(1)).flatten mustEqual Success(1)
+ }
+
+ "is a Success(Failure)" in {
+ val e = new Exception
+ Success(Failure(e)).flatten mustEqual Failure(e)
+ }
+
+ "is a Throw" in {
+ val e = new Exception
+ Failure[Try[Int]](e).flatten mustEqual Failure(e)
+ }
+ }
+
+ "for" in {
+ "with no Failure values" in {
+ val result = for {
+ i <- Success(1)
+ j <- Success(1)
+ } yield (i + j)
+ result mustEqual Success(2)
+ }
+
+ "with Failure values" in {
+ "throws before" in {
+ val result = for {
+ i <- Failure[Int](e)
+ j <- Success(1)
+ } yield (i + j)
+ result mustEqual Failure(e)
+ }
+
+ "throws after" in {
+ val result = for {
+ i <- Success(1)
+ j <- Failure[Int](e)
+ } yield (i + j)
+ result mustEqual Failure(e)
+ }
+
+ "returns the FIRST Failure" in {
+ val e2 = new Exception
+ val result = for {
+ i <- Failure[Int](e)
+ j <- Failure[Int](e2)
+ } yield (i + j)
+ result mustEqual Failure(e)
+ }
+ }
+ }
+ }
+} \ No newline at end of file
diff --git a/test/files/jvm/future-spec/main.scala b/test/files/jvm/future-spec/main.scala
index 45b59d5d20..57183d8cea 100644
--- a/test/files/jvm/future-spec/main.scala
+++ b/test/files/jvm/future-spec/main.scala
@@ -12,6 +12,7 @@ object Test {
def main(args: Array[String]) {
FutureTests.check()
PromiseTests.check()
+ TryTests.check()
}
}
@@ -47,7 +48,7 @@ trait MinimalScalaTest extends Output {
snippet
bufferPrintln("[OK] Test passed.")
} catch {
- case e =>
+ case e: Throwable =>
bufferPrintln("[FAILED] " + e)
bufferPrintln(e.getStackTrace().mkString("\n"))
throwables += e
@@ -59,6 +60,7 @@ trait MinimalScalaTest extends Output {
implicit def objectops(obj: Any) = new {
def mustBe(other: Any) = assert(obj == other, obj + " is not " + other)
+ def mustEqual(other: Any) = mustBe(other)
}