Skip to content

Commit

Permalink
Merge pull request akka#1423 from akka/wip-future-docs-∂π
Browse files Browse the repository at this point in the history
add Futures.promise to the docs
  • Loading branch information
rkuhn committed May 10, 2013
2 parents 96aac98 + e6655ec commit a5c9147
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
10 changes: 9 additions & 1 deletion akka-docs/rst/java/code/docs/future/FutureDocTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import scala.concurrent.ExecutionContext;
import scala.concurrent.Future;
import scala.concurrent.Await;
import scala.concurrent.Promise;
import akka.util.Timeout;
//#imports1

Expand Down Expand Up @@ -340,7 +341,7 @@ public Object apply(Object r, String t) {
}

@Test
public void useSuccessfulAndFailed() throws Exception {
public void useSuccessfulAndFailedAndPromise() throws Exception {
final ExecutionContext ec = system.dispatcher();
//#successful
Future<String> future = Futures.successful("Yay!");
Expand All @@ -349,11 +350,18 @@ public void useSuccessfulAndFailed() throws Exception {
Future<String> otherFuture = Futures.failed(
new IllegalArgumentException("Bang!"));
//#failed
//#promise
Promise<String> promise = Futures.promise();
Future<String> theFuture = promise.future();
promise.success("hello");
//#promise
Object result = Await.result(future, Duration.create(5, SECONDS));
assertEquals("Yay!", result);
Throwable result2 = Await.result(otherFuture.failed(),
Duration.create(5, SECONDS));
assertEquals("Bang!", result2.getMessage());
String out = Await.result(theFuture, Duration.create(5, SECONDS));
assertEquals("hello", out);
}

@Test
Expand Down
4 changes: 4 additions & 0 deletions akka-docs/rst/java/futures.rst
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ Or failures:
.. includecode:: code/docs/future/FutureDocTest.java
:include: failed

It is also possible to create an empty ``Promise``, to be filled later, and obtain the corresponding ``Future``:

.. includecode:: code/docs/future/FutureDocTestBase.java#promise

For these examples ``PrintResult`` is defined as follows:

.. includecode:: code/docs/future/FutureDocTest.java
Expand Down
8 changes: 7 additions & 1 deletion akka-docs/rst/scala/code/docs/future/FutureDocSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -399,15 +399,21 @@ class FutureDocSpec extends AkkaSpec {
}
}

"demonstrate usage of Future.successful & Future.failed" in {
"demonstrate usage of Future.successful & Future.failed & Future.promise" in {
//#successful
val future = Future.successful("Yay!")
//#successful
//#failed
val otherFuture = Future.failed[String](new IllegalArgumentException("Bang!"))
//#failed
//#promise
val promise = Promise[String]()
val theFuture = promise.future
promise.success("hello")
//#promise
Await.result(future, 3 seconds) must be("Yay!")
intercept[IllegalArgumentException] { Await.result(otherFuture, 3 seconds) }
Await.result(theFuture, 3 seconds) must be("hello")
}

"demonstrate usage of pattern.after" in {
Expand Down
5 changes: 5 additions & 0 deletions akka-docs/rst/scala/futures.rst
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ Or failures:
.. includecode:: code/docs/future/FutureDocSpec.scala
:include: failed

It is also possible to create an empty ``Promise``, to be filled later, and obtain the corresponding ``Future``:

.. includecode:: code/docs/future/FutureDocSpec.scala
:include: promise

Functional Futures
------------------

Expand Down

0 comments on commit a5c9147

Please sign in to comment.