#19440 replace Scala Future usage with CompletionStage in javadsl
This entails:
* adding akka.pattern.PatternCS.* to enable ask etc. with
CompletionStage
* changing RequestContext to offer an ExecutionContextExecutor for the
CompletionStage.*Async combinators
* splitting up akka.stream.Queue for JavaDSL consistency
This commit is contained in:
parent
396f4370e9
commit
4c72495581
118 changed files with 1646 additions and 1379 deletions
|
|
@ -28,6 +28,9 @@ import scala.concurrent.duration.Duration;
|
|||
import scala.concurrent.duration.FiniteDuration;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.CompletionStage;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
import java.util.stream.Stream;
|
||||
|
|
@ -50,8 +53,8 @@ public class FlowTest extends StreamTest {
|
|||
final JavaTestKit probe = new JavaTestKit(system);
|
||||
final String[] lookup = { "a", "b", "c", "d", "e", "f" };
|
||||
final java.lang.Iterable<Integer> input = Arrays.asList(0, 1, 2, 3, 4, 5);
|
||||
final Source<Integer, ?> ints = Source.from(input);
|
||||
final Flow<Integer, String, ?> flow1 = Flow.of(Integer.class).drop(2).take(3
|
||||
final Source<Integer, NotUsed> ints = Source.from(input);
|
||||
final Flow<Integer, String, NotUsed> flow1 = Flow.of(Integer.class).drop(2).take(3
|
||||
).takeWithin(FiniteDuration.create(10, TimeUnit.SECONDS
|
||||
)).map(new Function<Integer, String>() {
|
||||
public String apply(Integer elem) {
|
||||
|
|
@ -62,7 +65,7 @@ public class FlowTest extends StreamTest {
|
|||
return !elem.equals("c");
|
||||
}
|
||||
});
|
||||
final Flow<String, String, ?> flow2 = Flow.of(String.class).grouped(2
|
||||
final Flow<String, String, NotUsed> flow2 = Flow.of(String.class).grouped(2
|
||||
).mapConcat(new Function<java.util.List<String>, java.lang.Iterable<String>>() {
|
||||
public java.util.List<String> apply(java.util.List<String> elem) {
|
||||
return elem;
|
||||
|
|
@ -74,16 +77,9 @@ public class FlowTest extends StreamTest {
|
|||
}
|
||||
});
|
||||
|
||||
ints.via(flow1.via(flow2)).runFold("", new Function2<String, String, String>() {
|
||||
public String apply(String acc, String elem) {
|
||||
return acc + elem;
|
||||
}
|
||||
}, materializer
|
||||
).foreach(new Foreach<String>() { // Scala Future
|
||||
public void each(String elem) {
|
||||
probe.getRef().tell(elem, ActorRef.noSender());
|
||||
}
|
||||
}, system.dispatcher());
|
||||
ints.via(flow1.via(flow2))
|
||||
.runFold("", (acc, elem) -> acc + elem, materializer)
|
||||
.thenAccept(elem -> probe.getRef().tell(elem, ActorRef.noSender()));
|
||||
|
||||
probe.expectMsgEquals("de");
|
||||
}
|
||||
|
|
@ -91,36 +87,26 @@ public class FlowTest extends StreamTest {
|
|||
@Test
|
||||
public void mustBeAbleToUseDropWhile() throws Exception {
|
||||
final JavaTestKit probe = new JavaTestKit(system);
|
||||
final Source<Integer, ?> source = Source.from(Arrays.asList(0, 1, 2, 3));
|
||||
final Flow<Integer, Integer, ?> flow = Flow.of(Integer.class).dropWhile
|
||||
(new Predicate<Integer>() {
|
||||
public boolean test(Integer elem) {
|
||||
return elem < 2;
|
||||
}
|
||||
});
|
||||
final Source<Integer, NotUsed> source = Source.from(Arrays.asList(0, 1, 2, 3));
|
||||
final Flow<Integer, Integer, NotUsed> flow =
|
||||
Flow.of(Integer.class).dropWhile(elem -> elem < 2);
|
||||
|
||||
final Future<Done> future = source.via(flow).runWith(Sink.foreach(new Procedure<Integer>() { // Scala Future
|
||||
public void apply(Integer elem) {
|
||||
probe.getRef().tell(elem, ActorRef.noSender());
|
||||
}
|
||||
}), materializer);
|
||||
final CompletionStage<Done> future =
|
||||
source.via(flow).runWith(Sink.foreach(elem -> probe.getRef().tell(elem, ActorRef.noSender())), materializer);
|
||||
|
||||
probe.expectMsgEquals(2);
|
||||
probe.expectMsgEquals(3);
|
||||
Await.ready(future, Duration.apply(200, TimeUnit.MILLISECONDS));
|
||||
future.toCompletableFuture().get(200, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mustBeAbleToUseIntersperse() throws Exception {
|
||||
final JavaTestKit probe = new JavaTestKit(system);
|
||||
final Source<String, ?> source = Source.from(Arrays.asList("0", "1", "2", "3"));
|
||||
final Flow<String, String, ?> flow = Flow.of(String.class).intersperse("[", ",", "]");
|
||||
final Source<String, NotUsed> source = Source.from(Arrays.asList("0", "1", "2", "3"));
|
||||
final Flow<String, String, NotUsed> flow = Flow.of(String.class).intersperse("[", ",", "]");
|
||||
|
||||
final Future<Done> future = source.via(flow).runWith(Sink.foreach(new Procedure<String>() { // Scala Future
|
||||
public void apply(String elem) {
|
||||
probe.getRef().tell(elem, ActorRef.noSender());
|
||||
}
|
||||
}), materializer);
|
||||
final CompletionStage<Done> future =
|
||||
source.via(flow).runWith(Sink.foreach(elem -> probe.getRef().tell(elem, ActorRef.noSender())), materializer);
|
||||
|
||||
probe.expectMsgEquals("[");
|
||||
probe.expectMsgEquals("0");
|
||||
|
|
@ -131,20 +117,17 @@ public class FlowTest extends StreamTest {
|
|||
probe.expectMsgEquals(",");
|
||||
probe.expectMsgEquals("3");
|
||||
probe.expectMsgEquals("]");
|
||||
Await.ready(future, Duration.apply(200, TimeUnit.MILLISECONDS));
|
||||
future.toCompletableFuture().get(200, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mustBeAbleToUseIntersperseAndConcat() throws Exception {
|
||||
final JavaTestKit probe = new JavaTestKit(system);
|
||||
final Source<String, ?> source = Source.from(Arrays.asList("0", "1", "2", "3"));
|
||||
final Flow<String, String, ?> flow = Flow.of(String.class).intersperse(",");
|
||||
final Source<String, NotUsed> source = Source.from(Arrays.asList("0", "1", "2", "3"));
|
||||
final Flow<String, String, NotUsed> flow = Flow.of(String.class).intersperse(",");
|
||||
|
||||
final Future<Done> future = Source.single(">> ").concat(source.via(flow)).runWith(Sink.foreach(new Procedure<String>() { // Scala Future
|
||||
public void apply(String elem) {
|
||||
probe.getRef().tell(elem, ActorRef.noSender());
|
||||
}
|
||||
}), materializer);
|
||||
final CompletionStage<Done> future =
|
||||
Source.single(">> ").concat(source.via(flow)).runWith(Sink.foreach(elem -> probe.getRef().tell(elem, ActorRef.noSender())), materializer);
|
||||
|
||||
probe.expectMsgEquals(">> ");
|
||||
probe.expectMsgEquals("0");
|
||||
|
|
@ -154,25 +137,22 @@ public class FlowTest extends StreamTest {
|
|||
probe.expectMsgEquals("2");
|
||||
probe.expectMsgEquals(",");
|
||||
probe.expectMsgEquals("3");
|
||||
Await.ready(future, Duration.apply(200, TimeUnit.MILLISECONDS));
|
||||
future.toCompletableFuture().get(200, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mustBeAbleToUseTakeWhile() throws Exception {
|
||||
final JavaTestKit probe = new JavaTestKit(system);
|
||||
final Source<Integer, ?> source = Source.from(Arrays.asList(0, 1, 2, 3));
|
||||
final Flow<Integer, Integer, ?> flow = Flow.of(Integer.class).takeWhile
|
||||
final Source<Integer, NotUsed> source = Source.from(Arrays.asList(0, 1, 2, 3));
|
||||
final Flow<Integer, Integer, NotUsed> flow = Flow.of(Integer.class).takeWhile
|
||||
(new Predicate<Integer>() {
|
||||
public boolean test(Integer elem) {
|
||||
return elem < 2;
|
||||
}
|
||||
});
|
||||
|
||||
final Future<Done> future = source.via(flow).runWith(Sink.foreach(new Procedure<Integer>() { // Scala Future
|
||||
public void apply(Integer elem) {
|
||||
probe.getRef().tell(elem, ActorRef.noSender());
|
||||
}
|
||||
}), materializer);
|
||||
final CompletionStage<Done> future =
|
||||
source.via(flow).runWith(Sink.foreach(elem -> probe.getRef().tell(elem, ActorRef.noSender())), materializer);
|
||||
|
||||
probe.expectMsgEquals(0);
|
||||
probe.expectMsgEquals(1);
|
||||
|
|
@ -180,7 +160,7 @@ public class FlowTest extends StreamTest {
|
|||
FiniteDuration duration = Duration.apply(200, TimeUnit.MILLISECONDS);
|
||||
|
||||
probe.expectNoMsg(duration);
|
||||
Await.ready(future, duration);
|
||||
future.toCompletableFuture().get(200, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -189,7 +169,7 @@ public class FlowTest extends StreamTest {
|
|||
final JavaTestKit probe = new JavaTestKit(system);
|
||||
final Iterable<Integer> input = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7);
|
||||
// duplicate each element, stop after 4 elements, and emit sum to the end
|
||||
final Flow<Integer, Integer, ?> flow = Flow.of(Integer.class).transform(new Creator<Stage<Integer, Integer>>() {
|
||||
final Flow<Integer, Integer, NotUsed> flow = Flow.of(Integer.class).transform(new Creator<Stage<Integer, Integer>>() {
|
||||
@Override
|
||||
public PushPullStage<Integer, Integer> create() throws Exception {
|
||||
return new StatefulStage<Integer, Integer>() {
|
||||
|
|
@ -252,9 +232,9 @@ public class FlowTest extends StreamTest {
|
|||
.grouped(10)
|
||||
.mergeSubstreams();
|
||||
|
||||
final Future<List<List<String>>> future =
|
||||
final CompletionStage<List<List<String>>> future =
|
||||
Source.from(input).via(flow).grouped(10).runWith(Sink.<List<List<String>>> head(), materializer);
|
||||
final Object[] result = Await.result(future, Duration.create(1, TimeUnit.SECONDS)).toArray();
|
||||
final Object[] result = future.toCompletableFuture().get(1, TimeUnit.SECONDS).toArray();
|
||||
Arrays.sort(result, (Comparator<Object>)(Object) new Comparator<List<String>>() {
|
||||
@Override
|
||||
public int compare(List<String> o1, List<String> o2) {
|
||||
|
|
@ -278,9 +258,9 @@ public class FlowTest extends StreamTest {
|
|||
.grouped(10)
|
||||
.concatSubstreams();
|
||||
|
||||
final Future<List<List<String>>> future =
|
||||
final CompletionStage<List<List<String>>> future =
|
||||
Source.from(input).via(flow).grouped(10).runWith(Sink.<List<List<String>>> head(), materializer);
|
||||
final List<List<String>> result = Await.result(future, Duration.create(1, TimeUnit.SECONDS));
|
||||
final List<List<String>> result = future.toCompletableFuture().get(1, TimeUnit.SECONDS);
|
||||
|
||||
assertEquals(Arrays.asList(Arrays.asList("A", "B", "C"), Arrays.asList(".", "D"), Arrays.asList(".", "E", "F")), result);
|
||||
}
|
||||
|
|
@ -298,9 +278,9 @@ public class FlowTest extends StreamTest {
|
|||
.grouped(10)
|
||||
.concatSubstreams();
|
||||
|
||||
final Future<List<List<String>>> future =
|
||||
final CompletionStage<List<List<String>>> future =
|
||||
Source.from(input).via(flow).grouped(10).runWith(Sink.<List<List<String>>> head(), materializer);
|
||||
final List<List<String>> result = Await.result(future, Duration.create(1, TimeUnit.SECONDS));
|
||||
final List<List<String>> result = future.toCompletableFuture().get(1, TimeUnit.SECONDS);
|
||||
|
||||
assertEquals(Arrays.asList(Arrays.asList("A", "B", "C", "."), Arrays.asList("D", "."), Arrays.asList("E", "F")), result);
|
||||
}
|
||||
|
|
@ -352,9 +332,9 @@ public class FlowTest extends StreamTest {
|
|||
|
||||
// collecting
|
||||
final Publisher<String> pub = source.runWith(publisher, materializer);
|
||||
final Future<List<String>> all = Source.fromPublisher(pub).grouped(100).runWith(Sink.<List<String>>head(), materializer);
|
||||
final CompletionStage<List<String>> all = Source.fromPublisher(pub).grouped(100).runWith(Sink.<List<String>>head(), materializer);
|
||||
|
||||
final List<String> result = Await.result(all, Duration.apply(200, TimeUnit.MILLISECONDS));
|
||||
final List<String> result = all.toCompletableFuture().get(200, TimeUnit.MILLISECONDS);
|
||||
assertEquals(new HashSet<Object>(Arrays.asList("a", "b", "c", "d", "e", "f")), new HashSet<String>(result));
|
||||
}
|
||||
|
||||
|
|
@ -396,9 +376,9 @@ public class FlowTest extends StreamTest {
|
|||
final Iterable<String> input1 = Arrays.asList("A", "B", "C");
|
||||
final Iterable<String> input2 = Arrays.asList("D", "E", "F");
|
||||
|
||||
final Source<String, ?> in1 = Source.from(input1);
|
||||
final Source<String, ?> in2 = Source.from(input2);
|
||||
final Flow<String, String, ?> flow = Flow.of(String.class);
|
||||
final Source<String, NotUsed> in1 = Source.from(input1);
|
||||
final Source<String, NotUsed> in2 = Source.from(input2);
|
||||
final Flow<String, String, NotUsed> flow = Flow.of(String.class);
|
||||
in1.via(flow.concat(in2)).runForeach(new Procedure<String>() {
|
||||
public void apply(String elem) {
|
||||
probe.getRef().tell(elem, ActorRef.noSender());
|
||||
|
|
@ -415,9 +395,9 @@ public class FlowTest extends StreamTest {
|
|||
final Iterable<String> input1 = Arrays.asList("A", "B", "C");
|
||||
final Iterable<String> input2 = Arrays.asList("D", "E", "F");
|
||||
|
||||
final Source<String, ?> in1 = Source.from(input1);
|
||||
final Source<String, ?> in2 = Source.from(input2);
|
||||
final Flow<String, String, ?> flow = Flow.of(String.class);
|
||||
final Source<String, NotUsed> in1 = Source.from(input1);
|
||||
final Source<String, NotUsed> in2 = Source.from(input2);
|
||||
final Flow<String, String, NotUsed> flow = Flow.of(String.class);
|
||||
in2.via(flow.prepend(in1)).runForeach(new Procedure<String>() {
|
||||
public void apply(String elem) {
|
||||
probe.getRef().tell(elem, ActorRef.noSender());
|
||||
|
|
@ -432,15 +412,14 @@ public class FlowTest extends StreamTest {
|
|||
public void mustBeAbleToUsePrefixAndTail() throws Exception {
|
||||
final JavaTestKit probe = new JavaTestKit(system);
|
||||
final Iterable<Integer> input = Arrays.asList(1, 2, 3, 4, 5, 6);
|
||||
final Flow<Integer, Pair<List<Integer>, Source<Integer, NotUsed>>, ?> flow = Flow.of(Integer.class).prefixAndTail(3);
|
||||
Future<Pair<List<Integer>, Source<Integer, NotUsed>>> future =
|
||||
final Flow<Integer, Pair<List<Integer>, Source<Integer, NotUsed>>, NotUsed> flow = Flow.of(Integer.class).prefixAndTail(3);
|
||||
CompletionStage<Pair<List<Integer>, Source<Integer, NotUsed>>> future =
|
||||
Source.from(input).via(flow).runWith(Sink.<Pair<List<Integer>, Source<Integer, NotUsed>>>head(), materializer);
|
||||
Pair<List<Integer>, Source<Integer, NotUsed>> result = Await.result(future,
|
||||
probe.dilated(FiniteDuration.create(3, TimeUnit.SECONDS)));
|
||||
Pair<List<Integer>, Source<Integer, NotUsed>> result = future.toCompletableFuture().get(3, TimeUnit.SECONDS);
|
||||
assertEquals(Arrays.asList(1, 2, 3), result.first());
|
||||
|
||||
Future<List<Integer>> tailFuture = result.second().grouped(4).runWith(Sink.<List<Integer>>head(), materializer);
|
||||
List<Integer> tailResult = Await.result(tailFuture, probe.dilated(FiniteDuration.create(3, TimeUnit.SECONDS)));
|
||||
CompletionStage<List<Integer>> tailFuture = result.second().grouped(4).runWith(Sink.<List<Integer>>head(), materializer);
|
||||
List<Integer> tailResult = tailFuture.toCompletableFuture().get(3, TimeUnit.SECONDS);
|
||||
assertEquals(Arrays.asList(4, 5, 6), tailResult);
|
||||
}
|
||||
|
||||
|
|
@ -454,12 +433,12 @@ public class FlowTest extends StreamTest {
|
|||
mainInputs.add(Source.from(input1));
|
||||
mainInputs.add(Source.from(input2));
|
||||
|
||||
final Flow<Source<Integer, NotUsed>, List<Integer>, ?> flow = Flow.<Source<Integer, NotUsed>>create().
|
||||
final Flow<Source<Integer, NotUsed>, List<Integer>, NotUsed> flow = Flow.<Source<Integer, NotUsed>>create().
|
||||
flatMapConcat(ConstantFun.<Source<Integer, NotUsed>>javaIdentityFunction()).grouped(6);
|
||||
Future<List<Integer>> future = Source.from(mainInputs).via(flow)
|
||||
CompletionStage<List<Integer>> future = Source.from(mainInputs).via(flow)
|
||||
.runWith(Sink.<List<Integer>>head(), materializer);
|
||||
|
||||
List<Integer> result = Await.result(future, probe.dilated(FiniteDuration.create(3, TimeUnit.SECONDS)));
|
||||
List<Integer> result = future.toCompletableFuture().get(3, TimeUnit.SECONDS);
|
||||
|
||||
assertEquals(Arrays.asList(1, 2, 3, 4, 5), result);
|
||||
}
|
||||
|
|
@ -478,12 +457,12 @@ public class FlowTest extends StreamTest {
|
|||
mainInputs.add(Source.from(input3));
|
||||
mainInputs.add(Source.from(input4));
|
||||
|
||||
final Flow<Source<Integer, NotUsed>, List<Integer>, ?> flow = Flow.<Source<Integer, NotUsed>>create().
|
||||
final Flow<Source<Integer, NotUsed>, List<Integer>, NotUsed> flow = Flow.<Source<Integer, NotUsed>>create().
|
||||
flatMapMerge(3, ConstantFun.<Source<Integer, NotUsed>>javaIdentityFunction()).grouped(60);
|
||||
Future<List<Integer>> future = Source.from(mainInputs).via(flow)
|
||||
CompletionStage<List<Integer>> future = Source.from(mainInputs).via(flow)
|
||||
.runWith(Sink.<List<Integer>>head(), materializer);
|
||||
|
||||
List<Integer> result = Await.result(future, probe.dilated(FiniteDuration.create(3, TimeUnit.SECONDS)));
|
||||
List<Integer> result = future.toCompletableFuture().get(3, TimeUnit.SECONDS);
|
||||
final Set<Integer> set = new HashSet<Integer>();
|
||||
for (Integer i: result) {
|
||||
set.add(i);
|
||||
|
|
@ -501,10 +480,10 @@ public class FlowTest extends StreamTest {
|
|||
final JavaTestKit probe = new JavaTestKit(system);
|
||||
final List<String> input = Arrays.asList("A", "B", "C");
|
||||
final Flow<String, List<String>, NotUsed> flow = Flow.of(String.class).buffer(2, OverflowStrategy.backpressure()).grouped(4);
|
||||
Future<List<String>> future = Source.from(input).via(flow)
|
||||
final CompletionStage<List<String>> future = Source.from(input).via(flow)
|
||||
.runWith(Sink.<List<String>>head(), materializer);
|
||||
|
||||
List<String> result = Await.result(future, probe.dilated(FiniteDuration.create(3, TimeUnit.SECONDS)));
|
||||
List<String> result = future.toCompletableFuture().get(3, TimeUnit.SECONDS);
|
||||
assertEquals(input, result);
|
||||
}
|
||||
|
||||
|
|
@ -523,13 +502,8 @@ public class FlowTest extends StreamTest {
|
|||
return aggr + in;
|
||||
}
|
||||
});
|
||||
Future <String> future = Source.from(input).via(flow).runFold("", new Function2<String, String, String>() {
|
||||
@Override
|
||||
public String apply(String aggr, String in) throws Exception {
|
||||
return aggr + in;
|
||||
}
|
||||
}, materializer);
|
||||
String result = Await.result(future, probe.dilated(FiniteDuration.create(3, TimeUnit.SECONDS)));
|
||||
CompletionStage<String> future = Source.from(input).via(flow).runFold("", (aggr, in) -> aggr + in, materializer);
|
||||
String result = future.toCompletableFuture().get(3, TimeUnit.SECONDS);
|
||||
assertEquals("ABC", result);
|
||||
}
|
||||
|
||||
|
|
@ -548,13 +522,8 @@ public class FlowTest extends StreamTest {
|
|||
return aggr + in;
|
||||
}
|
||||
});
|
||||
Future <String> future = Source.from(input).via(flow).runFold("", new Function2<String, String, String>() {
|
||||
@Override
|
||||
public String apply(String aggr, String in) throws Exception {
|
||||
return aggr + in;
|
||||
}
|
||||
}, materializer);
|
||||
String result = Await.result(future, probe.dilated(FiniteDuration.create(3, TimeUnit.SECONDS)));
|
||||
CompletionStage<String> future = Source.from(input).via(flow).runFold("", (aggr, in) -> aggr + in, materializer);
|
||||
String result = future.toCompletableFuture().get(3, TimeUnit.SECONDS);
|
||||
assertEquals("ABC", result);
|
||||
}
|
||||
|
||||
|
|
@ -578,13 +547,8 @@ public class FlowTest extends StreamTest {
|
|||
return aggr + in;
|
||||
}
|
||||
});
|
||||
Future <String> future = Source.from(input).via(flow).runFold("", new Function2<String, String, String>() {
|
||||
@Override
|
||||
public String apply(String aggr, String in) throws Exception {
|
||||
return aggr + in;
|
||||
}
|
||||
}, materializer);
|
||||
String result = Await.result(future, probe.dilated(FiniteDuration.create(3, TimeUnit.SECONDS)));
|
||||
CompletionStage<String> future = Source.from(input).via(flow).runFold("", (aggr, in) -> aggr + in, materializer);
|
||||
String result = future.toCompletableFuture().get(3, TimeUnit.SECONDS);
|
||||
assertEquals("ABC", result);
|
||||
}
|
||||
|
||||
|
|
@ -593,9 +557,9 @@ public class FlowTest extends StreamTest {
|
|||
final JavaTestKit probe = new JavaTestKit(system);
|
||||
final List<String> input = Arrays.asList("A", "B", "C");
|
||||
final Flow<String, String, NotUsed> flow = Flow.of(String.class).expand(in -> Stream.iterate(in, i -> i).iterator());
|
||||
final Sink<String, Future<String>> sink = Sink.<String>head();
|
||||
Future<String> future = Source.from(input).via(flow).runWith(sink, materializer);
|
||||
String result = Await.result(future, probe.dilated(FiniteDuration.create(3, TimeUnit.SECONDS)));
|
||||
final Sink<String, CompletionStage<String>> sink = Sink.<String>head();
|
||||
CompletionStage<String> future = Source.from(input).via(flow).runWith(sink, materializer);
|
||||
String result = future.toCompletableFuture().get(3, TimeUnit.SECONDS);
|
||||
assertEquals("A", result);
|
||||
}
|
||||
|
||||
|
|
@ -603,11 +567,7 @@ public class FlowTest extends StreamTest {
|
|||
public void mustBeAbleToUseMapAsync() throws Exception {
|
||||
final JavaTestKit probe = new JavaTestKit(system);
|
||||
final Iterable<String> input = Arrays.asList("a", "b", "c");
|
||||
final Flow<String, String, NotUsed> flow = Flow.of(String.class).mapAsync(4, new Function<String, Future<String>>() {
|
||||
public Future<String> apply(String elem) {
|
||||
return Futures.successful(elem.toUpperCase());
|
||||
}
|
||||
});
|
||||
final Flow<String, String, NotUsed> flow = Flow.of(String.class).mapAsync(4, elem -> CompletableFuture.completedFuture(elem.toUpperCase()));
|
||||
Source.from(input).via(flow).runForeach(new Procedure<String>() {
|
||||
public void apply(String elem) {
|
||||
probe.getRef().tell(elem, ActorRef.noSender());
|
||||
|
|
@ -623,8 +583,8 @@ public class FlowTest extends StreamTest {
|
|||
final TestPublisher.ManualProbe<Integer> publisherProbe = TestPublisher.manualProbe(true,system);
|
||||
final JavaTestKit probe = new JavaTestKit(system);
|
||||
|
||||
final Source<Integer, ?> source = Source.fromPublisher(publisherProbe);
|
||||
final Flow<Integer, Integer, ?> flow = Flow.of(Integer.class).map(
|
||||
final Source<Integer, NotUsed> source = Source.fromPublisher(publisherProbe);
|
||||
final Flow<Integer, Integer, NotUsed> flow = Flow.of(Integer.class).map(
|
||||
new Function<Integer, Integer>() {
|
||||
public Integer apply(Integer elem) {
|
||||
if (elem == 2) throw new RuntimeException("ex");
|
||||
|
|
@ -638,11 +598,8 @@ public class FlowTest extends StreamTest {
|
|||
}
|
||||
});
|
||||
|
||||
final Future<Done> future = source.via(flow).runWith(Sink.foreach(new Procedure<Integer>() {
|
||||
public void apply(Integer elem) {
|
||||
probe.getRef().tell(elem, ActorRef.noSender());
|
||||
}
|
||||
}), materializer);
|
||||
final CompletionStage<Done> future =
|
||||
source.via(flow).runWith(Sink.foreach(elem -> probe.getRef().tell(elem, ActorRef.noSender())), materializer);
|
||||
|
||||
final PublisherProbeSubscription<Integer> s = publisherProbe.expectSubscription();
|
||||
|
||||
|
|
@ -652,7 +609,7 @@ public class FlowTest extends StreamTest {
|
|||
probe.expectMsgEquals(1);
|
||||
s.sendNext(2);
|
||||
probe.expectMsgEquals(0);
|
||||
Await.ready(future, Duration.apply(200, TimeUnit.MILLISECONDS));
|
||||
future.toCompletableFuture().get(200, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -660,9 +617,9 @@ public class FlowTest extends StreamTest {
|
|||
final JavaTestKit probe = new JavaTestKit(system);
|
||||
final List<String> input = Arrays.asList("A", "B", "C");
|
||||
|
||||
Flow<String,String,?> otherFlow = Flow.of(String.class);
|
||||
Flow<String,String,NotUsed> otherFlow = Flow.of(String.class);
|
||||
|
||||
Flow<String,String,?> myFlow = Flow.of(String.class).via(otherFlow);
|
||||
Flow<String,String,NotUsed> myFlow = Flow.of(String.class).via(otherFlow);
|
||||
Source.from(input).via(myFlow).runWith(Sink.foreach(new Procedure<String>() { // Scala Future
|
||||
public void apply(String elem) {
|
||||
probe.getRef().tell(elem, ActorRef.noSender());
|
||||
|
|
@ -676,7 +633,7 @@ public class FlowTest extends StreamTest {
|
|||
public void mustBeAbleToMaterializeIdentityToJavaSink() throws Exception {
|
||||
final JavaTestKit probe = new JavaTestKit(system);
|
||||
final List<String> input = Arrays.asList("A", "B", "C");
|
||||
Flow<String,String,?> otherFlow = Flow.of(String.class);
|
||||
Flow<String,String,NotUsed> otherFlow = Flow.of(String.class);
|
||||
|
||||
Sink<String,NotUsed> sink = Flow.of(String.class).to(otherFlow.to(Sink.foreach(new Procedure<String>() { // Scala Future
|
||||
public void apply(String elem) {
|
||||
|
|
@ -767,54 +724,54 @@ public class FlowTest extends StreamTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void mustBeAbleToUseInitialTimeout() throws Exception {
|
||||
public void mustBeAbleToUseInitialTimeout() throws Throwable {
|
||||
try {
|
||||
Await.result(
|
||||
Source.<Integer>maybe()
|
||||
.via(Flow.of(Integer.class).initialTimeout(Duration.create(1, "second")))
|
||||
.runWith(Sink.<Integer>head(), materializer),
|
||||
Duration.create(3, "second")
|
||||
);
|
||||
fail("A TimeoutException was expected");
|
||||
} catch(TimeoutException e) {
|
||||
try {
|
||||
Source.<Integer> maybe().via(Flow.of(Integer.class).initialTimeout(Duration.create(1, "second")))
|
||||
.runWith(Sink.<Integer> head(), materializer).toCompletableFuture().get(3, TimeUnit.SECONDS);
|
||||
fail("A TimeoutException was expected");
|
||||
} catch (ExecutionException e) {
|
||||
throw e.getCause();
|
||||
}
|
||||
} catch (TimeoutException e) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void mustBeAbleToUseCompletionTimeout() throws Exception {
|
||||
public void mustBeAbleToUseCompletionTimeout() throws Throwable {
|
||||
try {
|
||||
Await.result(
|
||||
Source.<Integer>maybe()
|
||||
.via(Flow.of(Integer.class).completionTimeout(Duration.create(1, "second")))
|
||||
.runWith(Sink.<Integer>head(), materializer),
|
||||
Duration.create(3, "second")
|
||||
);
|
||||
fail("A TimeoutException was expected");
|
||||
} catch(TimeoutException e) {
|
||||
try {
|
||||
Source.<Integer> maybe().via(Flow.of(Integer.class).completionTimeout(Duration.create(1, "second")))
|
||||
.runWith(Sink.<Integer> head(), materializer).toCompletableFuture().get(3, TimeUnit.SECONDS);
|
||||
fail("A TimeoutException was expected");
|
||||
} catch (ExecutionException e) {
|
||||
throw e.getCause();
|
||||
}
|
||||
} catch (TimeoutException e) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mustBeAbleToUseIdleTimeout() throws Exception {
|
||||
public void mustBeAbleToUseIdleTimeout() throws Throwable {
|
||||
try {
|
||||
Await.result(
|
||||
Source.<Integer>maybe()
|
||||
.via(Flow.of(Integer.class).idleTimeout(Duration.create(1, "second")))
|
||||
.runWith(Sink.<Integer>head(), materializer),
|
||||
Duration.create(3, "second")
|
||||
);
|
||||
fail("A TimeoutException was expected");
|
||||
} catch(TimeoutException e) {
|
||||
try {
|
||||
Source.<Integer> maybe().via(Flow.of(Integer.class).idleTimeout(Duration.create(1, "second")))
|
||||
.runWith(Sink.<Integer> head(), materializer).toCompletableFuture().get(3, TimeUnit.SECONDS);
|
||||
fail("A TimeoutException was expected");
|
||||
} catch (ExecutionException e) {
|
||||
throw e.getCause();
|
||||
}
|
||||
} catch (TimeoutException e) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mustBeAbleToUseKeepAlive() throws Exception {
|
||||
Integer result = Await.result(
|
||||
Integer result =
|
||||
Source.<Integer>maybe()
|
||||
.via(Flow.of(Integer.class)
|
||||
.keepAlive(Duration.create(1, "second"), new Creator<Integer>() {
|
||||
|
|
@ -824,9 +781,8 @@ public class FlowTest extends StreamTest {
|
|||
})
|
||||
)
|
||||
.takeWithin(Duration.create(1500, "milliseconds"))
|
||||
.runWith(Sink.<Integer>head(), materializer),
|
||||
Duration.create(3, "second")
|
||||
);
|
||||
.runWith(Sink.<Integer>head(), materializer)
|
||||
.toCompletableFuture().get(3, TimeUnit.SECONDS);
|
||||
|
||||
assertEquals((Object) 0, result);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue