aboutsummaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authorJason Zaugg <jzaugg@gmail.com>2012-11-06 10:14:23 -0800
committerJason Zaugg <jzaugg@gmail.com>2012-11-06 10:14:23 -0800
commit5ff56c529b32c9930684432188117658be4bba68 (patch)
tree5b5360d4c0bd06394e32d0646655cfbbba6026b4 /src/test
parente5e7b9a6a150507b46f701afae4c02ef60c9918d (diff)
parent11fe7a0cb359d59752d7ce442a9ccaad31ad157d (diff)
downloadscala-async-5ff56c529b32c9930684432188117658be4bba68.tar.gz
scala-async-5ff56c529b32c9930684432188117658be4bba68.tar.bz2
scala-async-5ff56c529b32c9930684432188117658be4bba68.zip
Merge pull request #5 from phaller/wip-match
Support await inside match expressions
Diffstat (limited to 'src/test')
-rw-r--r--src/test/scala/scala/async/run/match0/Match0.scala73
1 files changed, 73 insertions, 0 deletions
diff --git a/src/test/scala/scala/async/run/match0/Match0.scala b/src/test/scala/scala/async/run/match0/Match0.scala
new file mode 100644
index 0000000..3c7e297
--- /dev/null
+++ b/src/test/scala/scala/async/run/match0/Match0.scala
@@ -0,0 +1,73 @@
+/**
+ * Copyright (C) 2012 Typesafe Inc. <http://www.typesafe.com>
+ */
+
+package scala.async
+package run
+package match0
+
+import language.{reflectiveCalls, postfixOps}
+import scala.concurrent.{Future, ExecutionContext, future, Await}
+import scala.concurrent.duration._
+import scala.async.Async.{async, await}
+import org.junit.runner.RunWith
+import org.junit.runners.JUnit4
+import org.junit.Test
+
+
+class TestMatchClass {
+
+ import ExecutionContext.Implicits.global
+
+ def m1(x: Int): Future[Int] = future {
+ Thread.sleep(1000)
+ x + 2
+ }
+
+ def m2(y: Int): Future[Int] = async {
+ val f = m1(y)
+ var z = 0
+ y match {
+ case 10 =>
+ val x1 = await(f)
+ z = x1 + 2
+ case 20 =>
+ val x2 = await(f)
+ z = x2 - 2
+ }
+ z
+ }
+
+ def m3(y: Int): Future[Int] = async {
+ val f = m1(y)
+ var z = 0
+ y match {
+ case 0 =>
+ val x2 = await(f)
+ z = x2 - 2
+ case 1 =>
+ val x1 = await(f)
+ z = x1 + 2
+ }
+ z
+ }
+}
+
+
+@RunWith(classOf[JUnit4])
+class MatchSpec {
+
+ @Test def `support await in a simple match expression`() {
+ val o = new TestMatchClass
+ val fut = o.m2(10) // matches first case
+ val res = Await.result(fut, 2 seconds)
+ res mustBe (14)
+ }
+
+ @Test def `support await in a simple match expression 2`() {
+ val o = new TestMatchClass
+ val fut = o.m3(1) // matches second case
+ val res = Await.result(fut, 2 seconds)
+ res mustBe (5)
+ }
+}