From b76a016cb961b5fa9469f7998c9fe8683ef4269a Mon Sep 17 00:00:00 2001 From: phaller Date: Tue, 30 Oct 2012 15:54:25 +0100 Subject: Add test for creating several futures followed by several awaits --- test/files/run/block1.check | 1 + test/files/run/block1/MinimalScalaTest.scala | 102 +++++++++++++++++++++++++++ test/files/run/block1/block1.scala | 48 +++++++++++++ 3 files changed, 151 insertions(+) create mode 100644 test/files/run/block1.check create mode 100644 test/files/run/block1/MinimalScalaTest.scala create mode 100644 test/files/run/block1/block1.scala diff --git a/test/files/run/block1.check b/test/files/run/block1.check new file mode 100644 index 0000000..a82948b --- /dev/null +++ b/test/files/run/block1.check @@ -0,0 +1 @@ +between two awaits diff --git a/test/files/run/block1/MinimalScalaTest.scala b/test/files/run/block1/MinimalScalaTest.scala new file mode 100644 index 0000000..91de1fc --- /dev/null +++ b/test/files/run/block1/MinimalScalaTest.scala @@ -0,0 +1,102 @@ +import language.reflectiveCalls +import language.postfixOps +import language.implicitConversions + +import scala.reflect.{ ClassTag, classTag } + +import scala.collection.mutable +import scala.concurrent.{ Future, Awaitable, CanAwait } +import java.util.concurrent.{ TimeoutException, CountDownLatch, TimeUnit } +import scala.concurrent.duration.Duration + + + +trait Output { + val buffer = new StringBuilder + + def bufferPrintln(a: Any): Unit = buffer.synchronized { + buffer.append(a.toString + "\n") + } +} + + +trait MinimalScalaTest extends Output { + + val throwables = mutable.ArrayBuffer[Throwable]() + + def check() { + if (throwables.nonEmpty) println(buffer.toString) + } + + implicit def stringops(s: String) = new { + + def should[U](snippets: =>U): U = { + bufferPrintln(s + " should:") + snippets + } + + def in[U](snippet: =>U): Unit = { + try { + bufferPrintln("- " + s) + snippet + bufferPrintln("[OK] Test passed.") + } catch { + case e: Throwable => + bufferPrintln("[FAILED] " + e) + bufferPrintln(e.getStackTrace().mkString("\n")) + throwables += e + } + } + + } + + implicit def objectops(obj: Any) = new { + + def mustBe(other: Any) = assert(obj == other, obj + " is not " + other) + def mustEqual(other: Any) = mustBe(other) + + } + + def intercept[T <: Throwable: ClassTag](body: =>Any): T = { + try { + body + throw new Exception("Exception of type %s was not thrown".format(classTag[T])) + } catch { + case t: Throwable => + if (classTag[T].runtimeClass != t.getClass) throw t + else t.asInstanceOf[T] + } + } + + def checkType[T: ClassTag, S](in: Future[T], refclasstag: ClassTag[S]): Boolean = classTag[T] == refclasstag +} + + +object TestLatch { + val DefaultTimeout = Duration(5, TimeUnit.SECONDS) + + def apply(count: Int = 1) = new TestLatch(count) +} + + +class TestLatch(count: Int = 1) extends Awaitable[Unit] { + private var latch = new CountDownLatch(count) + + def countDown() = latch.countDown() + def isOpen: Boolean = latch.getCount == 0 + def open() = while (!isOpen) countDown() + def reset() = latch = new CountDownLatch(count) + + @throws(classOf[TimeoutException]) + def ready(atMost: Duration)(implicit permit: CanAwait) = { + val opened = latch.await(atMost.toNanos, TimeUnit.NANOSECONDS) + if (!opened) throw new TimeoutException("Timeout of %s." format (atMost.toString)) + this + } + + @throws(classOf[Exception]) + def result(atMost: Duration)(implicit permit: CanAwait): Unit = { + ready(atMost) + } + +} diff --git a/test/files/run/block1/block1.scala b/test/files/run/block1/block1.scala new file mode 100644 index 0000000..d6ea67e --- /dev/null +++ b/test/files/run/block1/block1.scala @@ -0,0 +1,48 @@ +/** + * Copyright (C) 2012 Typesafe Inc. + */ + +import language.{ reflectiveCalls, postfixOps } +import scala.concurrent.{ Future, ExecutionContext, future, Await } +import scala.concurrent.duration._ +import scala.async.Async.{ async, await } + + +object Test extends App { + + Block1Spec.check() + +} + + +class Test1Class { + import ExecutionContext.Implicits.global + + def m1(x: Int): Future[Int] = future { + Thread.sleep(1000) + x + 2 + } + + def m4(y: Int): Future[Int] = async { + val f1 = m1(y) + val f2 = m1(y + 2) + val x1 = await(f1) + println("between two awaits") + val x2 = await(f2) + x1 + x2 + } +} + + +object Block1Spec extends MinimalScalaTest { + + "An async method" should { + "support a simple await" in { + val o = new Test1Class + val fut = o.m4(10) + val res = Await.result(fut, 2 seconds) + res mustBe(26) + } + } + +} -- cgit v1.2.3