aboutsummaryrefslogtreecommitdiff
path: root/src/test/scala/scala/async/TestLatch.scala
diff options
context:
space:
mode:
authorJason Zaugg <jzaugg@gmail.com>2012-11-05 17:09:04 +0100
committerJason Zaugg <jzaugg@gmail.com>2012-11-05 17:09:04 +0100
commit62674b852dc0fef4cf4b3625939567ee82d6d096 (patch)
tree6a6b34ac8e1a4bedfd62824b5c901c8bca8e356c /src/test/scala/scala/async/TestLatch.scala
parent652028b9dc2000b45302757dc9fd0df26ff66805 (diff)
downloadscala-async-62674b852dc0fef4cf4b3625939567ee82d6d096.tar.gz
scala-async-62674b852dc0fef4cf4b3625939567ee82d6d096.tar.bz2
scala-async-62674b852dc0fef4cf4b3625939567ee82d6d096.zip
Move test utils into a package object, rather than base class.
Diffstat (limited to 'src/test/scala/scala/async/TestLatch.scala')
-rw-r--r--src/test/scala/scala/async/TestLatch.scala36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/test/scala/scala/async/TestLatch.scala b/src/test/scala/scala/async/TestLatch.scala
new file mode 100644
index 0000000..676ea63
--- /dev/null
+++ b/src/test/scala/scala/async/TestLatch.scala
@@ -0,0 +1,36 @@
+package scala.async
+
+import concurrent.{CanAwait, Awaitable}
+import concurrent.duration.Duration
+import java.util.concurrent.{TimeoutException, CountDownLatch, TimeUnit}
+
+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(s"Timeout of ${(atMost.toString)}.")
+ this
+ }
+
+ @throws(classOf[Exception])
+ def result(atMost: Duration)(implicit permit: CanAwait): Unit = {
+ ready(atMost)
+ }
+}