2018-10-29 17:19:37 +08:00
|
|
|
/*
|
2022-02-04 12:36:44 +01:00
|
|
|
* Copyright (C) 2009-2022 Lightbend Inc. <https://www.lightbend.com>
|
2011-12-19 11:07:59 +01:00
|
|
|
*/
|
2018-03-13 23:45:55 +09:00
|
|
|
|
2017-03-16 09:30:00 +01:00
|
|
|
package jdocs.future;
|
2011-12-15 22:31:12 +01:00
|
|
|
|
2022-11-12 10:21:24 +01:00
|
|
|
import org.apache.pekko.actor.typed.ActorSystem;
|
|
|
|
|
import org.apache.pekko.dispatch.Futures;
|
|
|
|
|
import org.apache.pekko.pattern.Patterns;
|
|
|
|
|
import org.apache.pekko.testkit.AkkaJUnitActorSystemResource;
|
|
|
|
|
import org.apache.pekko.testkit.AkkaSpec;
|
|
|
|
|
import org.apache.pekko.util.Timeout;
|
2017-03-16 09:30:00 +01:00
|
|
|
import jdocs.AbstractJavaTest;
|
2020-07-02 10:19:11 +02:00
|
|
|
import org.junit.ClassRule;
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
import scala.compat.java8.FutureConverters;
|
|
|
|
|
import scala.concurrent.Await;
|
2012-07-06 17:04:04 +02:00
|
|
|
import scala.concurrent.ExecutionContext;
|
|
|
|
|
import scala.concurrent.Future;
|
2017-07-26 04:09:45 +09:00
|
|
|
|
2018-08-23 22:43:17 +09:00
|
|
|
import java.time.Duration;
|
2012-11-21 20:33:54 +01:00
|
|
|
import java.util.Arrays;
|
2020-07-02 10:19:11 +02:00
|
|
|
import java.util.concurrent.Callable;
|
|
|
|
|
import java.util.concurrent.CompletableFuture;
|
|
|
|
|
import java.util.concurrent.CompletionStage;
|
2018-03-26 14:56:20 +03:00
|
|
|
|
2022-11-12 10:21:24 +01:00
|
|
|
import static org.apache.pekko.actor.typed.javadsl.Adapter.toTyped;
|
|
|
|
|
import static org.apache.pekko.dispatch.Futures.future;
|
2020-07-02 10:19:11 +02:00
|
|
|
// #imports
|
2022-11-12 10:21:24 +01:00
|
|
|
import org.apache.pekko.pattern.Patterns;
|
2011-12-15 22:31:12 +01:00
|
|
|
|
2020-07-02 10:19:11 +02:00
|
|
|
// #imports
|
|
|
|
|
import static java.util.concurrent.TimeUnit.SECONDS;
|
2011-12-15 22:31:12 +01:00
|
|
|
|
2016-02-11 16:39:25 +01:00
|
|
|
public class FutureDocTest extends AbstractJavaTest {
|
2011-12-15 22:31:12 +01:00
|
|
|
|
2013-05-02 17:12:36 +02:00
|
|
|
@ClassRule
|
|
|
|
|
public static AkkaJUnitActorSystemResource actorSystemResource =
|
2019-01-12 04:00:53 +08:00
|
|
|
new AkkaJUnitActorSystemResource("FutureDocTest", AkkaSpec.testConf());
|
2011-12-15 22:31:12 +01:00
|
|
|
|
2020-07-02 10:19:11 +02:00
|
|
|
private final ActorSystem<Void> system = toTyped(actorSystemResource.getSystem());
|
2012-01-24 16:31:20 +01:00
|
|
|
|
2020-07-02 10:19:11 +02:00
|
|
|
@Test(expected = java.util.concurrent.CompletionException.class)
|
2012-08-20 19:49:01 +02:00
|
|
|
public void useAfter() throws Exception {
|
2020-07-02 10:19:11 +02:00
|
|
|
final ExecutionContext ec = system.executionContext();
|
|
|
|
|
// #after
|
|
|
|
|
CompletionStage<String> failWithException =
|
|
|
|
|
CompletableFuture.supplyAsync(
|
|
|
|
|
() -> {
|
|
|
|
|
throw new IllegalStateException("OHNOES1");
|
|
|
|
|
});
|
|
|
|
|
CompletionStage<String> delayed =
|
|
|
|
|
Patterns.after(Duration.ofMillis(200), system, () -> failWithException);
|
2019-01-12 04:00:53 +08:00
|
|
|
// #after
|
|
|
|
|
Future<String> future =
|
|
|
|
|
future(
|
2020-07-02 10:19:11 +02:00
|
|
|
() -> {
|
|
|
|
|
Thread.sleep(1000);
|
|
|
|
|
return "foo";
|
2019-01-12 04:00:53 +08:00
|
|
|
},
|
|
|
|
|
ec);
|
|
|
|
|
Future<String> result =
|
2020-07-02 10:19:11 +02:00
|
|
|
Futures.firstCompletedOf(
|
|
|
|
|
Arrays.<Future<String>>asList(future, FutureConverters.toScala(delayed)), ec);
|
2018-08-23 22:43:17 +09:00
|
|
|
Timeout timeout = Timeout.create(Duration.ofSeconds(2));
|
|
|
|
|
Await.result(result, timeout.duration());
|
2012-08-20 19:49:01 +02:00
|
|
|
}
|
|
|
|
|
|
2018-03-26 14:56:20 +03:00
|
|
|
@Test
|
|
|
|
|
public void useRetry() throws Exception {
|
2019-01-12 04:00:53 +08:00
|
|
|
// #retry
|
2018-03-26 14:56:20 +03:00
|
|
|
Callable<CompletionStage<String>> attempt = () -> CompletableFuture.completedFuture("test");
|
2020-07-02 10:19:11 +02:00
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
CompletionStage<String> retriedFuture =
|
2020-07-02 10:19:11 +02:00
|
|
|
Patterns.retry(attempt, 3, java.time.Duration.ofMillis(200), system);
|
2019-01-12 04:00:53 +08:00
|
|
|
// #retry
|
2018-03-26 14:56:20 +03:00
|
|
|
|
|
|
|
|
retriedFuture.toCompletableFuture().get(2, SECONDS);
|
|
|
|
|
}
|
2011-12-15 22:31:12 +01:00
|
|
|
}
|