pekko/akka-actor-tests/src/test/java/akka/dispatch/JavaFutureTests.java

330 lines
10 KiB
Java
Raw Normal View History

/*
* Copyright (C) 2018-2019 Lightbend Inc. <https://www.lightbend.com>
*/
2011-03-01 15:23:29 -07:00
package akka.dispatch;
import akka.testkit.AkkaJUnitActorSystemResource;
import akka.actor.ActorSystem;
import akka.japi.*;
import org.junit.ClassRule;
import org.scalatestplus.junit.JUnitSuite;
Various scala-2.13.0-M5 fixes fix akka-actor-tests compile errors some tests still fail though Fix test failures in akka-actor-test Manually work arround missing implicit Factory[Nothing, Seq[Nothing]] see https://github.com/scala/scala-collection-compat/issues/137 akka-remote scalafix changes Fix shutdownAll compile error test:akka-remote scalafix changes akka-multi-node-testkit scalafix Fix akka-remote-tests multi-jvm compile errors akka-stream-tests/test:scalafix Fix test:akka-stream-tests Crude implementation of ByteString.map scalafix akka-actor-typed, akka-actor-typed-tests akka-actor-typed-tests compile and succeed scalafix akka-camel scalafix akka-cluster akka-cluster compile & test scalafix akka-cluster-metrics Fix akka-cluster-metrics scalafix akka-cluster-tools akka-cluster-tools compile and test scalafix akka-distributed-data akka-distributed-data fixes scalafix akka-persistence scalafix akka-cluster-sharding fix akka-cluster-sharding scalafix akka-contrib Fix akka-cluster-sharding-typed test scalafix akka-docs Use scala-stm 0.9 (released for M5) akka-docs Remove dependency on collections-compat Cherry-pick the relevant constructs to our own private utils Shorten 'scala.collections.immutable' by importing it Duplicate 'immutable' imports Use 'foreach' on futures Replace MapLike with regular Map Internal API markers Simplify ccompat by moving PackageShared into object Since we don't currently need to differentiate between 2.11 and Avoid relying on 'union' (and ++) being left-biased Fix akka-actor/doc by removing -Ywarn-unused Make more things more private Copyright headers Use 'unsorted' to go from SortedSet to Set Duplicate import Use onComplete rather than failed.foreach Clarify why we partly duplicate scala-collection-compat
2018-11-22 16:18:10 +01:00
import scala.Function1;
import scala.concurrent.Await;
2012-07-04 15:25:30 +02:00
import scala.concurrent.Future;
import scala.concurrent.Promise;
import scala.concurrent.duration.Duration;
2011-03-01 15:23:29 -07:00
import org.junit.Test;
import static org.junit.Assert.*;
import java.io.IOException;
2011-03-01 15:23:29 -07:00
import java.util.concurrent.Callable;
import java.util.LinkedList;
import java.lang.Iterable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import static akka.japi.Util.classTag;
import akka.testkit.AkkaSpec;
Various scala-2.13.0-M5 fixes fix akka-actor-tests compile errors some tests still fail though Fix test failures in akka-actor-test Manually work arround missing implicit Factory[Nothing, Seq[Nothing]] see https://github.com/scala/scala-collection-compat/issues/137 akka-remote scalafix changes Fix shutdownAll compile error test:akka-remote scalafix changes akka-multi-node-testkit scalafix Fix akka-remote-tests multi-jvm compile errors akka-stream-tests/test:scalafix Fix test:akka-stream-tests Crude implementation of ByteString.map scalafix akka-actor-typed, akka-actor-typed-tests akka-actor-typed-tests compile and succeed scalafix akka-camel scalafix akka-cluster akka-cluster compile & test scalafix akka-cluster-metrics Fix akka-cluster-metrics scalafix akka-cluster-tools akka-cluster-tools compile and test scalafix akka-distributed-data akka-distributed-data fixes scalafix akka-persistence scalafix akka-cluster-sharding fix akka-cluster-sharding scalafix akka-contrib Fix akka-cluster-sharding-typed test scalafix akka-docs Use scala-stm 0.9 (released for M5) akka-docs Remove dependency on collections-compat Cherry-pick the relevant constructs to our own private utils Shorten 'scala.collections.immutable' by importing it Duplicate 'immutable' imports Use 'foreach' on futures Replace MapLike with regular Map Internal API markers Simplify ccompat by moving PackageShared into object Since we don't currently need to differentiate between 2.11 and Avoid relying on 'union' (and ++) being left-biased Fix akka-actor/doc by removing -Ywarn-unused Make more things more private Copyright headers Use 'unsorted' to go from SortedSet to Set Duplicate import Use onComplete rather than failed.foreach Clarify why we partly duplicate scala-collection-compat
2018-11-22 16:18:10 +01:00
import scala.util.Try;
2011-03-01 15:23:29 -07:00
public class JavaFutureTests extends JUnitSuite {
2011-10-28 15:55:15 +02:00
@ClassRule
public static AkkaJUnitActorSystemResource actorSystemResource =
new AkkaJUnitActorSystemResource("JavaFutureTests", AkkaSpec.testConf());
private final ActorSystem system = actorSystemResource.getSystem();
2011-12-12 22:24:17 +01:00
private final Duration timeout = Duration.create(5, TimeUnit.SECONDS);
@Test
public void mustBeAbleToMapAFuture() throws Exception {
Future<String> f1 = Futures.future(new Callable<String>() {
public String call() {
return "Hello";
}
}, system.dispatcher());
2012-01-26 14:15:25 +01:00
Future<String> f2 = f1.map(new Mapper<String, String>() {
public String apply(String s) {
return s + " World";
}
2012-07-04 15:25:30 +02:00
}, system.dispatcher());
assertEquals("Hello World", Await.result(f2, timeout));
}
@Test
public void mustBeAbleToExecuteAnOnResultCallback() throws Throwable {
final CountDownLatch latch = new CountDownLatch(1);
Promise<String> cf = Futures.promise();
2012-07-04 15:25:30 +02:00
Future<String> f = cf.future();
Various scala-2.13.0-M5 fixes fix akka-actor-tests compile errors some tests still fail though Fix test failures in akka-actor-test Manually work arround missing implicit Factory[Nothing, Seq[Nothing]] see https://github.com/scala/scala-collection-compat/issues/137 akka-remote scalafix changes Fix shutdownAll compile error test:akka-remote scalafix changes akka-multi-node-testkit scalafix Fix akka-remote-tests multi-jvm compile errors akka-stream-tests/test:scalafix Fix test:akka-stream-tests Crude implementation of ByteString.map scalafix akka-actor-typed, akka-actor-typed-tests akka-actor-typed-tests compile and succeed scalafix akka-camel scalafix akka-cluster akka-cluster compile & test scalafix akka-cluster-metrics Fix akka-cluster-metrics scalafix akka-cluster-tools akka-cluster-tools compile and test scalafix akka-distributed-data akka-distributed-data fixes scalafix akka-persistence scalafix akka-cluster-sharding fix akka-cluster-sharding scalafix akka-contrib Fix akka-cluster-sharding-typed test scalafix akka-docs Use scala-stm 0.9 (released for M5) akka-docs Remove dependency on collections-compat Cherry-pick the relevant constructs to our own private utils Shorten 'scala.collections.immutable' by importing it Duplicate 'immutable' imports Use 'foreach' on futures Replace MapLike with regular Map Internal API markers Simplify ccompat by moving PackageShared into object Since we don't currently need to differentiate between 2.11 and Avoid relying on 'union' (and ++) being left-biased Fix akka-actor/doc by removing -Ywarn-unused Make more things more private Copyright headers Use 'unsorted' to go from SortedSet to Set Duplicate import Use onComplete rather than failed.foreach Clarify why we partly duplicate scala-collection-compat
2018-11-22 16:18:10 +01:00
f.onComplete(new OnComplete<String>() {
public void onComplete(Throwable t, String r) {
if ("foo".equals(r))
latch.countDown();
}
2012-07-04 15:25:30 +02:00
}, system.dispatcher());
cf.success("foo");
assertTrue(latch.await(5, TimeUnit.SECONDS));
assertEquals(Await.result(f, timeout), "foo");
}
@Test
public void mustBeAbleToExecuteAnOnExceptionCallback() throws Throwable {
final CountDownLatch latch = new CountDownLatch(1);
Promise<String> cf = Futures.promise();
2012-07-04 15:25:30 +02:00
Future<String> f = cf.future();
Various scala-2.13.0-M5 fixes fix akka-actor-tests compile errors some tests still fail though Fix test failures in akka-actor-test Manually work arround missing implicit Factory[Nothing, Seq[Nothing]] see https://github.com/scala/scala-collection-compat/issues/137 akka-remote scalafix changes Fix shutdownAll compile error test:akka-remote scalafix changes akka-multi-node-testkit scalafix Fix akka-remote-tests multi-jvm compile errors akka-stream-tests/test:scalafix Fix test:akka-stream-tests Crude implementation of ByteString.map scalafix akka-actor-typed, akka-actor-typed-tests akka-actor-typed-tests compile and succeed scalafix akka-camel scalafix akka-cluster akka-cluster compile & test scalafix akka-cluster-metrics Fix akka-cluster-metrics scalafix akka-cluster-tools akka-cluster-tools compile and test scalafix akka-distributed-data akka-distributed-data fixes scalafix akka-persistence scalafix akka-cluster-sharding fix akka-cluster-sharding scalafix akka-contrib Fix akka-cluster-sharding-typed test scalafix akka-docs Use scala-stm 0.9 (released for M5) akka-docs Remove dependency on collections-compat Cherry-pick the relevant constructs to our own private utils Shorten 'scala.collections.immutable' by importing it Duplicate 'immutable' imports Use 'foreach' on futures Replace MapLike with regular Map Internal API markers Simplify ccompat by moving PackageShared into object Since we don't currently need to differentiate between 2.11 and Avoid relying on 'union' (and ++) being left-biased Fix akka-actor/doc by removing -Ywarn-unused Make more things more private Copyright headers Use 'unsorted' to go from SortedSet to Set Duplicate import Use onComplete rather than failed.foreach Clarify why we partly duplicate scala-collection-compat
2018-11-22 16:18:10 +01:00
f.onComplete(new OnComplete<String>() {
public void onComplete(Throwable t, String r) {
// 'null instanceof ...' is always false
if (t instanceof NullPointerException)
latch.countDown();
}
2012-07-04 15:25:30 +02:00
}, system.dispatcher());
Throwable exception = new NullPointerException();
cf.failure(exception);
assertTrue(latch.await(5, TimeUnit.SECONDS));
2012-08-20 15:21:44 +02:00
assertEquals(f.value().get().failed().get(), exception);
}
@Test
public void mustBeAbleToExecuteAnOnCompleteCallback() throws Throwable {
final CountDownLatch latch = new CountDownLatch(1);
Promise<String> cf = Futures.promise();
2012-07-04 15:25:30 +02:00
Future<String> f = cf.future();
2012-01-26 14:15:25 +01:00
f.onComplete(new OnComplete<String>() {
public void onComplete(Throwable t, String r) {
latch.countDown();
}
2012-07-04 15:25:30 +02:00
}, system.dispatcher());
cf.success("foo");
assertTrue(latch.await(5, TimeUnit.SECONDS));
assertEquals(Await.result(f, timeout), "foo");
}
@Test
public void mustBeAbleToForeachAFuture() throws Throwable {
final CountDownLatch latch = new CountDownLatch(1);
Promise<String> cf = Futures.promise();
2012-07-04 15:25:30 +02:00
Future<String> f = cf.future();
2012-01-26 14:15:25 +01:00
f.foreach(new Foreach<String>() {
public void each(String future) {
latch.countDown();
}
2012-07-04 15:25:30 +02:00
},system.dispatcher());
cf.success("foo");
assertTrue(latch.await(5, TimeUnit.SECONDS));
assertEquals(Await.result(f, timeout), "foo");
}
@Test
public void mustBeAbleToFlatMapAFuture() throws Throwable {
final CountDownLatch latch = new CountDownLatch(1);
Promise<String> cf = Futures.promise();
cf.success("1000");
2012-07-04 15:25:30 +02:00
Future<String> f = cf.future();
2012-01-26 14:15:25 +01:00
Future<Integer> r = f.flatMap(new Mapper<String, Future<Integer>>() {
public Future<Integer> checkedApply(String r) throws Throwable {
if (false) throw new IOException("Just here to make sure this compiles.");
latch.countDown();
Promise<Integer> cf = Futures.promise();
cf.success(Integer.parseInt(r));
2012-07-04 15:25:30 +02:00
return cf.future();
}
2012-07-04 15:25:30 +02:00
}, system.dispatcher());
assertEquals(Await.result(f, timeout), "1000");
assertEquals(Await.result(r, timeout).intValue(), 1000);
assertTrue(latch.await(5, TimeUnit.SECONDS));
}
@Test
public void mustBeAbleToFilterAFuture() throws Throwable {
final CountDownLatch latch = new CountDownLatch(1);
Promise<String> cf = Futures.promise();
2012-07-04 15:25:30 +02:00
Future<String> f = cf.future();
Future<String> r = f.filter(Filter.filterOf(new Function<String, Boolean>() {
public Boolean apply(String r) {
latch.countDown();
return r.equals("foo");
}
2012-07-04 15:25:30 +02:00
}), system.dispatcher());
cf.success("foo");
assertTrue(latch.await(5, TimeUnit.SECONDS));
assertEquals(Await.result(f, timeout), "foo");
assertEquals(Await.result(r, timeout), "foo");
}
// TODO: Improve this test, perhaps with an Actor
@Test
public void mustSequenceAFutureList() throws Exception{
LinkedList<Future<String>> listFutures = new LinkedList<Future<String>>();
LinkedList<String> listExpected = new LinkedList<String>();
for (int i = 0; i < 10; i++) {
listExpected.add("test");
listFutures.add(Futures.future(new Callable<String>() {
public String call() {
return "test";
}
}, system.dispatcher()));
}
Future<Iterable<String>> futureList = Futures.sequence(listFutures, system.dispatcher());
assertEquals(Await.result(futureList, timeout), listExpected);
}
// TODO: Improve this test, perhaps with an Actor
@Test
public void foldForJavaApiMustWork() throws Exception{
LinkedList<Future<String>> listFutures = new LinkedList<Future<String>>();
StringBuilder expected = new StringBuilder();
for (int i = 0; i < 10; i++) {
expected.append("test");
listFutures.add(Futures.future(new Callable<String>() {
public String call() {
return "test";
}
}, system.dispatcher()));
2011-03-01 15:23:29 -07:00
}
Future<String> result = Futures.fold("", listFutures, new Function2<String, String, String>() {
public String apply(String r, String t) {
return r + t;
}
}, system.dispatcher());
assertEquals(Await.result(result, timeout), expected.toString());
}
@Test
public void reduceForJavaApiMustWork() throws Exception{
LinkedList<Future<String>> listFutures = new LinkedList<Future<String>>();
StringBuilder expected = new StringBuilder();
for (int i = 0; i < 10; i++) {
expected.append("test");
listFutures.add(Futures.future(new Callable<String>() {
public String call() {
return "test";
}
}, system.dispatcher()));
}
Future<String> result = Futures.reduce(listFutures, new Function2<String, String, String>() {
public String apply(String r, String t) {
return r + t;
}
}, system.dispatcher());
assertEquals(Await.result(result, timeout), expected.toString());
}
@Test
public void traverseForJavaApiMustWork() throws Exception{
LinkedList<String> listStrings = new LinkedList<String>();
LinkedList<String> expectedStrings = new LinkedList<String>();
for (int i = 0; i < 10; i++) {
expectedStrings.add("TEST");
listStrings.add("test");
}
Future<Iterable<String>> result = Futures.traverse(listStrings, new Function<String, Future<String>>() {
public Future<String> apply(final String r) {
return Futures.future(new Callable<String>() {
public String call() {
return r.toUpperCase();
}
}, system.dispatcher());
}
}, system.dispatcher());
assertEquals(Await.result(result, timeout), expectedStrings);
}
@Test
public void findForJavaApiMustWork() throws Exception{
LinkedList<Future<Integer>> listFutures = new LinkedList<Future<Integer>>();
for (int i = 0; i < 10; i++) {
final Integer fi = i;
listFutures.add(Futures.future(new Callable<Integer>() {
public Integer call() {
return fi;
2011-08-26 17:25:18 +02:00
}
}, system.dispatcher()));
2011-08-26 17:25:18 +02:00
}
final Integer expect = 5;
Future<Option<Integer>> f = Futures.find(listFutures, new Function<Integer, Boolean>() {
public Boolean apply(Integer i) {
return i == 5;
}
}, system.dispatcher());
assertEquals(expect, Await.result(f, timeout).get());
2011-12-11 14:06:30 +01:00
}
@Test
public void blockMustBeCallable() throws Exception {
Promise<String> p = Futures.promise();
2011-12-11 14:06:30 +01:00
Duration d = Duration.create(1, TimeUnit.SECONDS);
p.success("foo");
2012-07-04 15:25:30 +02:00
Await.ready(p.future(), d);
assertEquals(Await.result(p.future(), d), "foo");
}
@Test
public void mapToMustBeCallable() throws Exception {
Promise<Object> p = Futures.promise();
Future<String> f = p.future().mapTo(classTag(String.class));
Duration d = Duration.create(1, TimeUnit.SECONDS);
p.success("foo");
2012-07-04 15:25:30 +02:00
Await.ready(p.future(), d);
assertEquals(Await.result(p.future(), d), "foo");
}
@Test
public void recoverToMustBeCallable() throws Exception {
final IllegalStateException fail = new IllegalStateException("OHNOES");
Promise<Object> p = Futures.promise();
Future<Object> f = p.future().recover(new Recover<Object>() {
public Object recover(Throwable t) throws Throwable {
if (t == fail)
return "foo";
else
throw t;
}
2012-07-04 15:25:30 +02:00
}, system.dispatcher());
Duration d = Duration.create(1, TimeUnit.SECONDS);
p.failure(fail);
assertEquals(Await.result(f, d), "foo");
}
@Test
public void recoverWithToMustBeCallable() throws Exception{
final IllegalStateException fail = new IllegalStateException("OHNOES");
Promise<Object> p = Futures.promise();
Future<Object> f = p.future().recoverWith(new Recover<Future<Object>>() {
public Future<Object> recover(Throwable t) throws Throwable {
if (t == fail)
return Futures.<Object>successful("foo");
else
throw t;
}
2012-07-04 15:25:30 +02:00
}, system.dispatcher());
Duration d = Duration.create(1, TimeUnit.SECONDS);
p.failure(fail);
assertEquals(Await.result(f, d), "foo");
}
2011-03-01 15:23:29 -07:00
}