diff options
author | Philipp Haller <hallerp@gmail.com> | 2011-12-07 16:53:42 +0100 |
---|---|---|
committer | Philipp Haller <hallerp@gmail.com> | 2011-12-07 16:53:42 +0100 |
commit | 5ccc928f7ec2e79c793ee1b31ca8e91321688749 (patch) | |
tree | 2ec26bd203e639a16f6e025bd06c7f6748e151cd /test/files/jvm | |
parent | 91b6028fd3b728f59fb38cd3e12e25a2120ebb16 (diff) | |
download | scala-5ccc928f7ec2e79c793ee1b31ca8e91321688749.tar.gz scala-5ccc928f7ec2e79c793ee1b31ca8e91321688749.tar.bz2 scala-5ccc928f7ec2e79c793ee1b31ca8e91321688749.zip |
Add tests for future callbacks
Diffstat (limited to 'test/files/jvm')
-rw-r--r-- | test/files/jvm/concurrent-future.check | 16 | ||||
-rw-r--r-- | test/files/jvm/concurrent-future.scala | 118 |
2 files changed, 134 insertions, 0 deletions
diff --git a/test/files/jvm/concurrent-future.check b/test/files/jvm/concurrent-future.check new file mode 100644 index 0000000000..c55e824818 --- /dev/null +++ b/test/files/jvm/concurrent-future.check @@ -0,0 +1,16 @@ +test1: hai world +test1: kthxbye +test2: hai world +test2: awsum thx +test2: kthxbye +test3: hai world +test4: hai world +test4: kthxbye +test5: hai world +test5: kthxbye +test6: hai world +test6: kthxbye +test7: hai world +test7: kthxbye +test8: hai world +test8: im in yr loop diff --git a/test/files/jvm/concurrent-future.scala b/test/files/jvm/concurrent-future.scala new file mode 100644 index 0000000000..9c2f04fb07 --- /dev/null +++ b/test/files/jvm/concurrent-future.scala @@ -0,0 +1,118 @@ +import scala.concurrent.{ executionContext, FutureTimeoutException, ExecutionException, SyncVar } +import executionContext._ + +object Test extends App { + + def once(body: (() => Unit) => Unit) { + val sv = new SyncVar[Boolean] + body(() => sv put true) + sv.take() + } + + def output(num: Int, msg: String) { + println("test" + num + ": " + msg) + } + + def testOnSuccess(): Unit = once { + done => + val f = future { + output(1, "hai world") + } + f onSuccess { _ => + output(1, "kthxbye") + done() + } + } + + def testOnSuccessWhenCompleted(): Unit = once { + done => + val f = future { + output(2, "hai world") + } + f onSuccess { _ => + output(2, "awsum thx") + f onSuccess { _ => + output(2, "kthxbye") + done() + } + } + } + + def testOnSuccessWhenFailed(): Unit = once { + done => + val f = future { + output(3, "hai world") + done() + throw new Exception + } + f onSuccess { _ => + output(3, "onoes") + } + } + + def testOnFailure(): Unit = once { + done => + val f = future { + output(4, "hai world") + throw new Exception + } + f onSuccess { _ => + output(4, "onoes") + done() + } + f onFailure { _ => + output(4, "kthxbye") + done() + } + } + + def testOnFailureWhenSpecialThrowable(num: Int, cause: Throwable): Unit = once { + done => + val f = future { + output(num, "hai world") + throw cause + } + f onSuccess { _ => + output(num, "onoes") + done() + } + f onFailure { + case e: ExecutionException if (e.getCause == cause) => + output(num, "kthxbye") + done() + case _ => + output(num, "onoes") + done() + } + } + + def testOnFailureWhenFutureTimeoutException(): Unit = once { + done => + val f = future { + output(8, "hai world") + throw new FutureTimeoutException(null) + } + f onSuccess { _ => + output(8, "onoes") + done() + } + f onFailure { + case e: FutureTimeoutException => + output(8, "im in yr loop") + done() + case other => + output(8, "onoes: " + other) + done() + } + } + + testOnSuccess() + testOnSuccessWhenCompleted() + testOnSuccessWhenFailed() + testOnFailure() + testOnFailureWhenSpecialThrowable(5, new Error) + testOnFailureWhenSpecialThrowable(6, new scala.util.control.ControlThrowable { }) + testOnFailureWhenSpecialThrowable(7, new InterruptedException) + testOnFailureWhenFutureTimeoutException() + +} |