From 1efed78de89d38af04d57d6397402e65bea8c204 Mon Sep 17 00:00:00 2001 From: Viktor Klang Date: Sun, 11 Dec 2011 14:06:30 +0100 Subject: [PATCH] Removing resultOrException --- .../java/akka/dispatch/JavaFutureTests.java | 13 ++++++++++-- .../test/scala/akka/dispatch/FutureSpec.scala | 21 ++++++++----------- .../src/main/scala/akka/dispatch/Future.scala | 20 ++++++++---------- .../cluster/sample/ComputeGridSample.scala | 6 +++--- 4 files changed, 32 insertions(+), 28 deletions(-) diff --git a/akka-actor-tests/src/test/java/akka/dispatch/JavaFutureTests.java b/akka-actor-tests/src/test/java/akka/dispatch/JavaFutureTests.java index a7f1d09fbc..7a684460ff 100644 --- a/akka-actor-tests/src/test/java/akka/dispatch/JavaFutureTests.java +++ b/akka-actor-tests/src/test/java/akka/dispatch/JavaFutureTests.java @@ -3,6 +3,7 @@ package akka.dispatch; import akka.actor.Timeout; import akka.actor.ActorSystem; +import akka.util.Duration; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; @@ -269,7 +270,15 @@ public class JavaFutureTests { } }); - final Integer got = f.get().get(); - assertEquals(expect, got); + assertEquals(expect, Block.sync(f, Duration.create(5, TimeUnit.SECONDS))); + } + + @Test + public void BlockMustBeCallable() { + Promise p = ff.promise(); + Duration d = Duration.create(1, TimeUnit.SECONDS); + p.completeWithResult("foo"); + Block.on(p, d); + assertEquals(Block.sync(p, d), "foo"); } } diff --git a/akka-actor-tests/src/test/scala/akka/dispatch/FutureSpec.scala b/akka-actor-tests/src/test/scala/akka/dispatch/FutureSpec.scala index d9e41c2941..5193d982e4 100644 --- a/akka-actor-tests/src/test/scala/akka/dispatch/FutureSpec.scala +++ b/akka-actor-tests/src/test/scala/akka/dispatch/FutureSpec.scala @@ -37,6 +37,7 @@ object FutureSpec { } } +@org.junit.runner.RunWith(classOf[org.scalatest.junit.JUnitRunner]) class JavaFutureSpec extends JavaFutureTests with JUnitSuite @org.junit.runner.RunWith(classOf[org.scalatest.junit.JUnitRunner]) @@ -439,19 +440,15 @@ class FutureSpec extends AkkaSpec with Checkers with BeforeAndAfterAll with Defa "shouldBlockUntilResult" in { val latch = new StandardLatch - val f = Future({ latch.await; 5 }) - val f2 = Future({ f.get + 5 }) + val f = Future { latch.await; 5 } + val f2 = Future { Block.sync(f, timeout.duration) + 5 } - assert(f2.resultOrException === None) + intercept[TimeoutException](Block.on(f2, 100 millis)) latch.open - assert(f2.get === 10) + assert(Block.sync(f2, timeout.duration) === 10) - val f3 = Future({ Thread.sleep(100); 5 }) - filterException[TimeoutException] { - intercept[TimeoutException] { - Block.on(f3, 0 millis) - } - } + val f3 = Future { Thread.sleep(100); 5 } + filterException[TimeoutException] { intercept[TimeoutException] { Block.on(f3, 0 millis) } } } "futureComposingWithContinuations" in { @@ -826,7 +823,7 @@ class FutureSpec extends AkkaSpec with Checkers with BeforeAndAfterAll with Defa "contain a result" in { f((future, result) ⇒ future.result must be(Some(result))) } "not contain an exception" in { f((future, _) ⇒ future.exception must be(None)) } "return result with 'get'" in { f((future, result) ⇒ future.get must be(result)) } - "return result with 'resultOrException'" in { f((future, result) ⇒ future.resultOrException must be(Some(result))) } + "return result with 'Block.sync'" in { f((future, result) ⇒ Block.sync(future, timeout.duration) must be(result)) } "not timeout" in { f((future, _) ⇒ Block.on(future, 0 millis)) } "filter result" in { f { (future, result) ⇒ @@ -850,7 +847,7 @@ class FutureSpec extends AkkaSpec with Checkers with BeforeAndAfterAll with Defa "not contain a result" in { f((future, _) ⇒ future.result must be(None)) } "contain an exception" in { f((future, message) ⇒ future.exception.get.getMessage must be(message)) } "throw exception with 'get'" in { f((future, message) ⇒ (evaluating { future.get } must produce[E]).getMessage must be(message)) } - "throw exception with 'resultOrException'" in { f((future, message) ⇒ (evaluating { future.resultOrException } must produce[E]).getMessage must be(message)) } + "throw exception with 'Block.sync'" in { f((future, message) ⇒ (evaluating { Block.sync(future, timeout.duration) } must produce[E]).getMessage must be(message)) } "retain exception with filter" in { f { (future, message) ⇒ (evaluating { (future filter (_ ⇒ true)).get } must produce[E]).getMessage must be(message) diff --git a/akka-actor/src/main/scala/akka/dispatch/Future.scala b/akka-actor/src/main/scala/akka/dispatch/Future.scala index e15547ad3f..f1e21641b5 100644 --- a/akka-actor/src/main/scala/akka/dispatch/Future.scala +++ b/akka-actor/src/main/scala/akka/dispatch/Future.scala @@ -617,15 +617,6 @@ sealed trait Future[+T] extends japi.Future[T] with Block.Blockable[T] { } future } - - /** - * Returns the current result, throws the exception if one has been raised, else returns None - */ - final def resultOrException: Option[T] = value match { - case Some(Left(e)) ⇒ throw e - case Some(Right(r)) ⇒ Some(r) - case _ ⇒ None - } } object Promise { @@ -749,7 +740,11 @@ class DefaultPromise[T](implicit val dispatcher: MessageDispatcher) extends Abst if (value.isDefined || tryAwait(atMost)) this else throw new TimeoutException("Futures timed out after [" + atMost.toMillis + "] milliseconds") - def sync(atMost: Duration)(implicit permit: CanBlock): T = block(atMost).resultOrException.get + def sync(atMost: Duration)(implicit permit: CanBlock): T = + block(atMost).value.get match { + case Left(e) ⇒ throw e + case Right(r) ⇒ r + } def value: Option[Either[Throwable, T]] = getState.value @@ -824,5 +819,8 @@ final class KeptPromise[T](suppliedValue: Either[Throwable, T])(implicit val dis } def block(atMost: Duration)(implicit permit: CanBlock): this.type = this - def sync(atMost: Duration)(implicit permit: CanBlock): T = resultOrException.get + def sync(atMost: Duration)(implicit permit: CanBlock): T = value.get match { + case Left(e) ⇒ throw e + case Right(r) ⇒ r + } } diff --git a/akka-cluster/src/test/scala/akka/cluster/sample/ComputeGridSample.scala b/akka-cluster/src/test/scala/akka/cluster/sample/ComputeGridSample.scala index 4cf7a7010f..4811b8c9d4 100644 --- a/akka-cluster/src/test/scala/akka/cluster/sample/ComputeGridSample.scala +++ b/akka-cluster/src/test/scala/akka/cluster/sample/ComputeGridSample.scala @@ -49,7 +49,7 @@ object ComputeGridSample { val fun = () ⇒ "AKKA ROCKS" val futures = local send (fun, 2) // send and invoke function on to two cluster nodes and get result - val result = Futures.fold("")(futures)(_ + " - " + _).await.resultOrException + val result = Block.sync(Futures.fold("")(futures)(_ + " - " + _), timeout) println("===================>>> Cluster says [" + result + "]") local.stop @@ -83,8 +83,8 @@ object ComputeGridSample { val future2 = local send (fun, 2, 1) head // send and invoke function on one cluster node and get result // grab the result from the first one that returns - val result = Futures.firstCompletedOf(List(future1, future2)).await.resultOrException - println("===================>>> Cluster says [" + result.get + "]") + val result = Block.sync(Futures.firstCompletedOf(List(future1, future2)), timeout) + println("===================>>> Cluster says [" + result + "]") local.stop remote1.stop