diff options
author | Jason Zaugg <jzaugg@gmail.com> | 2012-11-05 16:20:48 +0100 |
---|---|---|
committer | Jason Zaugg <jzaugg@gmail.com> | 2012-11-05 16:32:01 +0100 |
commit | 57a5012514cf691fddf184cc85967e39cdc540b6 (patch) | |
tree | d4d2226c54179947f028bbfb1cc4bb9f2abb07d7 /src/test/scala/scala/async/run/block0/AsyncSpec.scala | |
parent | 3f36c1ea4b95ba046fa378ade19ca368e6e5c21b (diff) | |
download | scala-async-57a5012514cf691fddf184cc85967e39cdc540b6.tar.gz scala-async-57a5012514cf691fddf184cc85967e39cdc540b6.tar.bz2 scala-async-57a5012514cf691fddf184cc85967e39cdc540b6.zip |
Improve test infrastructure
- Convert tests to use JUnit
- For the 'run' tests, just use plain-old-test-cases
- Add a sample 'neg' test to use ToolBoxes to compile code snippets on the fly.
Diffstat (limited to 'src/test/scala/scala/async/run/block0/AsyncSpec.scala')
-rw-r--r-- | src/test/scala/scala/async/run/block0/AsyncSpec.scala | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/src/test/scala/scala/async/run/block0/AsyncSpec.scala b/src/test/scala/scala/async/run/block0/AsyncSpec.scala new file mode 100644 index 0000000..de58bb1 --- /dev/null +++ b/src/test/scala/scala/async/run/block0/AsyncSpec.scala @@ -0,0 +1,61 @@ +/** + * Copyright (C) 2012 Typesafe Inc. <http://www.typesafe.com> + */ + +package scala.async +package run +package block0 + +import language.{reflectiveCalls, postfixOps} +import scala.concurrent.{Future, ExecutionContext, future, Await} +import scala.concurrent.duration._ +import scala.async.Async.{async, await} +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 + + +class Test1Class { + + 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) + val x = await(f) + x + 2 + } + + def m3(y: Int): Future[Int] = async { + val f1 = m1(y) + val x1 = await(f1) + val f2 = m1(y + 2) + val x2 = await(f2) + x1 + x2 + } +} + + +@RunWith(classOf[JUnit4]) +class AsyncSpec extends MinimalScalaTest { + + @Test + def `simple await` { + val o = new Test1Class + val fut = o.m2(10) + val res = Await.result(fut, 2 seconds) + res mustBe (14) + } + + @Test + def `several awaits in sequence` { + val o = new Test1Class + val fut = o.m3(10) + val res = Await.result(fut, 4 seconds) + res mustBe (26) + } +} |