diff options
Diffstat (limited to 'test/files/jvm')
-rw-r--r-- | test/files/jvm/scala-concurrent-tck.scala | 290 |
1 files changed, 274 insertions, 16 deletions
diff --git a/test/files/jvm/scala-concurrent-tck.scala b/test/files/jvm/scala-concurrent-tck.scala index b3470d275d..f0ca438774 100644 --- a/test/files/jvm/scala-concurrent-tck.scala +++ b/test/files/jvm/scala-concurrent-tck.scala @@ -147,59 +147,185 @@ trait FutureCallbacks extends TestBase { trait FutureCombinators extends TestBase { - // map: stub def testMapSuccess(): Unit = once { done => - done() + val f = future { 5 } + val g = f map { x => "result: " + x } + g onSuccess { + case s => + done() + assert(s == "result: 5") + } + g onFailure { + case _ => + done() + assert(false) + } } def testMapFailure(): Unit = once { done => - done() + val f = future { + throw new Exception("exception message") + } + val g = f map { x => "result: " + x } + g onSuccess { + case _ => + done() + assert(false) + } + g onFailure { + case t => + done() + assert(t.getMessage() == "exception message") + } } - // flatMap: stub def testFlatMapSuccess(): Unit = once { done => - done() + val f = future { 5 } + val g = f flatMap { _ => future { 10 } } + g onSuccess { + case x => + done() + assert(x == 10) + } + g onFailure { + case _ => + done() + assert(false) + } } def testFlatMapFailure(): Unit = once { done => - done() + val f = future { + throw new Exception("exception message") + } + val g = f flatMap { _ => future { 10 } } + g onSuccess { + case _ => + done() + assert(false) + } + g onFailure { + case t => + done() + assert(t.getMessage() == "exception message") + } } - // filter: stub def testFilterSuccess(): Unit = once { done => - done() + val f = future { 4 } + val g = f filter { _ % 2 == 0 } + g onSuccess { + case x: Int => + done() + assert(x == 4) + } + g onFailure { + case _ => + done() + assert(false) + } } def testFilterFailure(): Unit = once { done => - done() + val f = future { 4 } + val g = f filter { _ % 2 == 1 } + g onSuccess { + case x: Int => + done() + assert(false) + } + g onFailure { + case e: NoSuchElementException => + done() + assert(true) + case _ => + done() + assert(false) + } } - // collect: stub def testCollectSuccess(): Unit = once { done => - done() + val f = future { -5 } + val g = f collect { + case x if x < 0 => -x + } + g onSuccess { + case x: Int => + done() + assert(x == 5) + } + g onFailure { + case _ => + done() + assert(false) + } } def testCollectFailure(): Unit = once { done => - done() + val f = future { -5 } + val g = f collect { + case x if x > 0 => x * 2 + } + g onSuccess { + case _ => + done() + assert(false) + } + g onFailure { + case e: NoSuchElementException => + done() + assert(true) + case _ => + done() + assert(false) + } } - // foreach: stub def testForeachSuccess(): Unit = once { done => - done() + val p = promise[Int]() + val f = future[Int] { 5 } + f foreach { x => p.success(x * 2) } + val g = p.future + + g.onSuccess { + case res: Int => + done() + assert(res == 10) + } + g.onFailure { + case _ => + done() + assert(false) + } } def testForeachFailure(): Unit = once { done => - done() + val p = promise[Int]() + val f = future[Int] { throw new Exception } + f foreach { x => p.success(x * 2) } + f onFailure { case _ => p.failure(new Exception) } + val g = p.future + + g.onSuccess { + case _ => + done() + assert(false) + } + g.onFailure { + case _ => + done() + assert(true) + } } def testRecoverSuccess(): Unit = once { @@ -237,6 +363,132 @@ trait FutureCombinators extends TestBase { } } + def testRecoverWithSuccess(): Unit = once { + done => + val cause = new RuntimeException + val f = future { + throw cause + } recoverWith { + case re: RuntimeException => + future { "recovered" } + } onSuccess { + case x => + done() + assert(x == "recovered") + } onFailure { case any => + done() + assert(false) + } + } + + def testRecoverWithFailure(): Unit = once { + done => + val cause = new RuntimeException + val f = future { + throw cause + } recoverWith { + case te: TimeoutException => + future { "timeout" } + } onSuccess { + case x => + done() + assert(false) + } onFailure { case any => + done() + assert(any == cause) + } + } + + def testZipSuccess(): Unit = once { + done => + val f = future { 5 } + val g = future { 6 } + val h = f zip g + h onSuccess { + case (l: Int, r: Int) => + done() + assert(l+r == 11) + } + h onFailure { + case _ => + done() + assert(false) + } + } + + def testZipFailureLeft(): Unit = once { + done => + val cause = new Exception("exception message") + val f = future { throw cause } + val g = future { 6 } + val h = f zip g + h onSuccess { + case _ => + done() + assert(false) + } + h onFailure { + case e: Exception => + done() + assert(e.getMessage == "exception message") + } + } + + def testZipFailureRight(): Unit = once { + done => + val cause = new Exception("exception message") + val f = future { 5 } + val g = future { throw cause } + val h = f zip g + h onSuccess { + case _ => + done() + assert(false) + } + h onFailure { + case e: Exception => + done() + assert(e.getMessage == "exception message") + } + } + + def testFallbackTo(): Unit = once { + done => + val f = future { sys.error("failed") } + val g = future { 5 } + val h = f fallbackTo g + + h onSuccess { + case x: Int => + done() + assert(x == 5) + } + h onFailure { + case _ => + done() + assert(false) + } + } + + def testFallbackToFailure(): Unit = once { + done => + val cause = new Exception + val f = future { throw cause } + val g = future { sys.error("failed") } + val h = f fallbackTo g + + h onSuccess { + case _ => + done() + assert(false) + } + h onFailure { + case e: Exception => + done() + assert(e == cause) + } + } + testMapSuccess() testMapFailure() testFlatMapSuccess() @@ -249,7 +501,13 @@ trait FutureCombinators extends TestBase { testForeachFailure() testRecoverSuccess() testRecoverFailure() - + testRecoverWithSuccess() + testRecoverWithFailure() + testZipSuccess() + testZipFailureLeft() + testZipFailureRight() + testFallbackTo() + testFallbackToFailure() } |