2014-12-16 11:48:42 +01:00
|
|
|
/**
|
2016-02-23 12:58:39 +01:00
|
|
|
* Copyright (C) 2014-2016 Lightbend Inc. <http://www.lightbend.com>
|
2014-12-16 11:48:42 +01:00
|
|
|
*/
|
2014-04-23 10:05:09 +02:00
|
|
|
package akka.stream.javadsl;
|
|
|
|
|
|
2016-01-20 10:00:37 +02:00
|
|
|
import akka.Done;
|
|
|
|
|
import akka.NotUsed;
|
2014-08-22 11:42:05 +02:00
|
|
|
import akka.actor.ActorRef;
|
2014-10-03 17:33:14 +02:00
|
|
|
import akka.dispatch.Foreach;
|
2014-08-22 11:42:05 +02:00
|
|
|
import akka.dispatch.Futures;
|
2015-06-13 14:02:37 -04:00
|
|
|
import akka.japi.JavaPartialFunction;
|
2014-10-03 17:33:14 +02:00
|
|
|
import akka.japi.Pair;
|
2015-04-23 20:59:55 +02:00
|
|
|
import akka.japi.function.*;
|
2015-01-28 14:19:50 +01:00
|
|
|
import akka.stream.*;
|
2015-11-03 14:46:17 +01:00
|
|
|
import akka.stream.impl.ConstantFun;
|
2015-11-30 15:45:37 +01:00
|
|
|
import akka.stream.javadsl.GraphDSL.Builder;
|
2015-08-16 00:04:04 -04:00
|
|
|
import akka.stream.stage.*;
|
2016-02-25 14:27:45 +01:00
|
|
|
import akka.testkit.AkkaSpec;
|
2015-08-16 00:04:04 -04:00
|
|
|
import akka.stream.testkit.TestPublisher;
|
2014-08-22 11:42:05 +02:00
|
|
|
import akka.testkit.JavaTestKit;
|
2014-04-23 10:05:09 +02:00
|
|
|
import org.junit.ClassRule;
|
2015-10-31 14:46:10 +01:00
|
|
|
import org.junit.Ignore;
|
2014-04-23 10:05:09 +02:00
|
|
|
import org.junit.Test;
|
2015-08-16 00:04:04 -04:00
|
|
|
import org.reactivestreams.Publisher;
|
2014-04-23 10:05:09 +02:00
|
|
|
import scala.concurrent.Await;
|
|
|
|
|
import scala.concurrent.Future;
|
2014-11-20 21:59:33 +01:00
|
|
|
import scala.concurrent.duration.Duration;
|
2015-08-16 00:04:04 -04:00
|
|
|
import scala.concurrent.duration.FiniteDuration;
|
|
|
|
|
|
2014-08-22 11:42:05 +02:00
|
|
|
import java.util.*;
|
2016-01-21 16:37:26 +01:00
|
|
|
import java.util.concurrent.CompletableFuture;
|
|
|
|
|
import java.util.concurrent.CompletionStage;
|
|
|
|
|
import java.util.concurrent.ExecutionException;
|
2014-08-22 11:42:05 +02:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2015-11-02 15:30:10 +01:00
|
|
|
import java.util.concurrent.TimeoutException;
|
2016-01-18 11:29:14 +01:00
|
|
|
import java.util.stream.Stream;
|
2015-08-16 00:04:04 -04:00
|
|
|
|
|
|
|
|
import static akka.stream.testkit.StreamTestKit.PublisherProbeSubscription;
|
2015-11-25 19:58:48 +01:00
|
|
|
import static org.junit.Assert.*;
|
2014-04-23 10:05:09 +02:00
|
|
|
|
2015-04-09 15:16:59 +02:00
|
|
|
@SuppressWarnings("serial")
|
2014-11-07 15:00:50 +01:00
|
|
|
public class FlowTest extends StreamTest {
|
|
|
|
|
public FlowTest() {
|
|
|
|
|
super(actorSystemResource);
|
|
|
|
|
}
|
2014-04-23 10:05:09 +02:00
|
|
|
|
2014-11-07 15:00:50 +01:00
|
|
|
@ClassRule
|
2014-10-27 14:35:41 +01:00
|
|
|
public static AkkaJUnitActorSystemResource actorSystemResource = new AkkaJUnitActorSystemResource("FlowTest",
|
|
|
|
|
AkkaSpec.testConf());
|
2014-04-23 10:05:09 +02:00
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseSimpleOperators() {
|
|
|
|
|
final JavaTestKit probe = new JavaTestKit(system);
|
2014-10-27 14:35:41 +01:00
|
|
|
final String[] lookup = { "a", "b", "c", "d", "e", "f" };
|
2014-11-09 21:09:50 +01:00
|
|
|
final java.lang.Iterable<Integer> input = Arrays.asList(0, 1, 2, 3, 4, 5);
|
2016-01-21 16:37:26 +01:00
|
|
|
final Source<Integer, NotUsed> ints = Source.from(input);
|
|
|
|
|
final Flow<Integer, String, NotUsed> flow1 = Flow.of(Integer.class).drop(2).take(3
|
2014-12-03 10:33:21 +01:00
|
|
|
).takeWithin(FiniteDuration.create(10, TimeUnit.SECONDS
|
|
|
|
|
)).map(new Function<Integer, String>() {
|
2014-10-27 14:35:41 +01:00
|
|
|
public String apply(Integer elem) {
|
|
|
|
|
return lookup[elem];
|
|
|
|
|
}
|
|
|
|
|
}).filter(new Predicate<String>() {
|
2014-10-03 17:33:14 +02:00
|
|
|
public boolean test(String elem) {
|
|
|
|
|
return !elem.equals("c");
|
|
|
|
|
}
|
2014-12-03 10:33:21 +01:00
|
|
|
});
|
2016-01-21 16:37:26 +01:00
|
|
|
final Flow<String, String, NotUsed> flow2 = Flow.of(String.class).grouped(2
|
2015-05-12 15:54:26 +02:00
|
|
|
).mapConcat(new Function<java.util.List<String>, java.lang.Iterable<String>>() {
|
2014-10-03 17:33:14 +02:00
|
|
|
public java.util.List<String> apply(java.util.List<String> elem) {
|
|
|
|
|
return elem;
|
|
|
|
|
}
|
2014-12-03 10:33:21 +01:00
|
|
|
}).groupedWithin(100, FiniteDuration.create(50, TimeUnit.MILLISECONDS)
|
2015-05-12 15:54:26 +02:00
|
|
|
).mapConcat(new Function<java.util.List<String>, java.lang.Iterable<String>>() {
|
2014-10-27 14:35:41 +01:00
|
|
|
public java.util.List<String> apply(java.util.List<String> elem) {
|
|
|
|
|
return elem;
|
|
|
|
|
}
|
2014-12-03 10:33:21 +01:00
|
|
|
});
|
|
|
|
|
|
2016-01-21 16:37:26 +01:00
|
|
|
ints.via(flow1.via(flow2))
|
|
|
|
|
.runFold("", (acc, elem) -> acc + elem, materializer)
|
|
|
|
|
.thenAccept(elem -> probe.getRef().tell(elem, ActorRef.noSender()));
|
2014-04-23 10:05:09 +02:00
|
|
|
|
|
|
|
|
probe.expectMsgEquals("de");
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-12 23:22:36 -04:00
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseDropWhile() throws Exception {
|
|
|
|
|
final JavaTestKit probe = new JavaTestKit(system);
|
2016-01-21 16:37:26 +01:00
|
|
|
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);
|
2015-06-12 23:22:36 -04:00
|
|
|
|
2016-01-21 16:37:26 +01:00
|
|
|
final CompletionStage<Done> future =
|
|
|
|
|
source.via(flow).runWith(Sink.foreach(elem -> probe.getRef().tell(elem, ActorRef.noSender())), materializer);
|
2015-06-12 23:22:36 -04:00
|
|
|
|
|
|
|
|
probe.expectMsgEquals(2);
|
|
|
|
|
probe.expectMsgEquals(3);
|
2016-01-21 16:37:26 +01:00
|
|
|
future.toCompletableFuture().get(200, TimeUnit.MILLISECONDS);
|
2015-06-12 23:22:36 -04:00
|
|
|
}
|
|
|
|
|
|
2016-01-27 00:00:39 -05:00
|
|
|
@Test
|
2016-02-15 16:53:57 +01:00
|
|
|
public void mustBeAbleToUseStatefulMaponcat() throws Exception {
|
2016-01-27 00:00:39 -05:00
|
|
|
final JavaTestKit probe = new JavaTestKit(system);
|
|
|
|
|
final java.lang.Iterable<Integer> input = Arrays.asList(1, 2, 3, 4, 5);
|
|
|
|
|
final Source<Integer, NotUsed> ints = Source.from(input);
|
|
|
|
|
final Flow<Integer, Integer, NotUsed> flow = Flow.of(Integer.class).statefulMapConcat(
|
2016-02-15 16:53:57 +01:00
|
|
|
() -> {
|
|
|
|
|
int[] state = new int[] {0};
|
|
|
|
|
return (elem) -> {
|
|
|
|
|
List<Integer> list = new ArrayList<>(Collections.nCopies(state[0], elem));
|
|
|
|
|
state[0] = elem;
|
|
|
|
|
return list;
|
|
|
|
|
};
|
2016-01-27 00:00:39 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
ints.via(flow)
|
|
|
|
|
.runFold("", (acc, elem) -> acc + elem, materializer)
|
|
|
|
|
.thenAccept(elem -> probe.getRef().tell(elem, ActorRef.noSender()));
|
|
|
|
|
|
|
|
|
|
probe.expectMsgEquals("2334445555");
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-16 01:55:20 +02:00
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseIntersperse() throws Exception {
|
|
|
|
|
final JavaTestKit probe = new JavaTestKit(system);
|
2016-01-21 16:37:26 +01:00
|
|
|
final Source<String, NotUsed> source = Source.from(Arrays.asList("0", "1", "2", "3"));
|
|
|
|
|
final Flow<String, String, NotUsed> flow = Flow.of(String.class).intersperse("[", ",", "]");
|
2015-10-16 01:55:20 +02:00
|
|
|
|
2016-01-21 16:37:26 +01:00
|
|
|
final CompletionStage<Done> future =
|
|
|
|
|
source.via(flow).runWith(Sink.foreach(elem -> probe.getRef().tell(elem, ActorRef.noSender())), materializer);
|
2015-10-16 01:55:20 +02:00
|
|
|
|
|
|
|
|
probe.expectMsgEquals("[");
|
|
|
|
|
probe.expectMsgEquals("0");
|
|
|
|
|
probe.expectMsgEquals(",");
|
|
|
|
|
probe.expectMsgEquals("1");
|
|
|
|
|
probe.expectMsgEquals(",");
|
|
|
|
|
probe.expectMsgEquals("2");
|
|
|
|
|
probe.expectMsgEquals(",");
|
|
|
|
|
probe.expectMsgEquals("3");
|
|
|
|
|
probe.expectMsgEquals("]");
|
2016-01-21 16:37:26 +01:00
|
|
|
future.toCompletableFuture().get(200, TimeUnit.MILLISECONDS);
|
2015-10-16 01:55:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseIntersperseAndConcat() throws Exception {
|
|
|
|
|
final JavaTestKit probe = new JavaTestKit(system);
|
2016-01-21 16:37:26 +01:00
|
|
|
final Source<String, NotUsed> source = Source.from(Arrays.asList("0", "1", "2", "3"));
|
|
|
|
|
final Flow<String, String, NotUsed> flow = Flow.of(String.class).intersperse(",");
|
2015-10-16 01:55:20 +02:00
|
|
|
|
2016-01-21 16:37:26 +01:00
|
|
|
final CompletionStage<Done> future =
|
|
|
|
|
Source.single(">> ").concat(source.via(flow)).runWith(Sink.foreach(elem -> probe.getRef().tell(elem, ActorRef.noSender())), materializer);
|
2015-10-16 01:55:20 +02:00
|
|
|
|
|
|
|
|
probe.expectMsgEquals(">> ");
|
|
|
|
|
probe.expectMsgEquals("0");
|
|
|
|
|
probe.expectMsgEquals(",");
|
|
|
|
|
probe.expectMsgEquals("1");
|
|
|
|
|
probe.expectMsgEquals(",");
|
|
|
|
|
probe.expectMsgEquals("2");
|
|
|
|
|
probe.expectMsgEquals(",");
|
|
|
|
|
probe.expectMsgEquals("3");
|
2016-01-21 16:37:26 +01:00
|
|
|
future.toCompletableFuture().get(200, TimeUnit.MILLISECONDS);
|
2015-10-16 01:55:20 +02:00
|
|
|
}
|
|
|
|
|
|
2015-06-12 23:22:36 -04:00
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseTakeWhile() throws Exception {
|
|
|
|
|
final JavaTestKit probe = new JavaTestKit(system);
|
2016-01-21 16:37:26 +01:00
|
|
|
final Source<Integer, NotUsed> source = Source.from(Arrays.asList(0, 1, 2, 3));
|
|
|
|
|
final Flow<Integer, Integer, NotUsed> flow = Flow.of(Integer.class).takeWhile
|
2015-06-12 23:22:36 -04:00
|
|
|
(new Predicate<Integer>() {
|
|
|
|
|
public boolean test(Integer elem) {
|
|
|
|
|
return elem < 2;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2016-01-21 16:37:26 +01:00
|
|
|
final CompletionStage<Done> future =
|
|
|
|
|
source.via(flow).runWith(Sink.foreach(elem -> probe.getRef().tell(elem, ActorRef.noSender())), materializer);
|
2015-06-12 23:22:36 -04:00
|
|
|
|
|
|
|
|
probe.expectMsgEquals(0);
|
|
|
|
|
probe.expectMsgEquals(1);
|
|
|
|
|
|
|
|
|
|
FiniteDuration duration = Duration.apply(200, TimeUnit.MILLISECONDS);
|
|
|
|
|
|
|
|
|
|
probe.expectNoMsg(duration);
|
2016-01-21 16:37:26 +01:00
|
|
|
future.toCompletableFuture().get(200, TimeUnit.MILLISECONDS);
|
2015-06-12 23:22:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2015-10-31 14:46:10 +01:00
|
|
|
@Ignore("StatefulStage to be converted to GraphStage when Java Api is available (#18817)") @Test
|
2014-04-23 10:05:09 +02:00
|
|
|
public void mustBeAbleToUseTransform() {
|
|
|
|
|
final JavaTestKit probe = new JavaTestKit(system);
|
2014-10-20 20:53:03 +02:00
|
|
|
final Iterable<Integer> input = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7);
|
2014-04-23 10:05:09 +02:00
|
|
|
// duplicate each element, stop after 4 elements, and emit sum to the end
|
2016-01-21 16:37:26 +01:00
|
|
|
final Flow<Integer, Integer, NotUsed> flow = Flow.of(Integer.class).transform(new Creator<Stage<Integer, Integer>>() {
|
2014-04-23 10:05:09 +02:00
|
|
|
@Override
|
2014-11-12 10:43:39 +01:00
|
|
|
public PushPullStage<Integer, Integer> create() throws Exception {
|
|
|
|
|
return new StatefulStage<Integer, Integer>() {
|
2014-08-22 11:42:05 +02:00
|
|
|
int sum = 0;
|
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
|
|
@Override
|
2014-11-12 10:43:39 +01:00
|
|
|
public StageState<Integer, Integer> initial() {
|
|
|
|
|
return new StageState<Integer, Integer>() {
|
|
|
|
|
@Override
|
2015-04-09 22:28:16 +02:00
|
|
|
public SyncDirective onPush(Integer element, Context<Integer> ctx) {
|
2014-11-12 10:43:39 +01:00
|
|
|
sum += element;
|
|
|
|
|
count += 1;
|
|
|
|
|
if (count == 4) {
|
|
|
|
|
return emitAndFinish(Arrays.asList(element, element, sum).iterator(), ctx);
|
|
|
|
|
} else {
|
|
|
|
|
return emit(Arrays.asList(element, element).iterator(), ctx);
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-10-31 14:46:10 +01:00
|
|
|
|
2014-11-12 10:43:39 +01:00
|
|
|
};
|
2014-08-22 11:42:05 +02:00
|
|
|
}
|
2015-10-31 14:46:10 +01:00
|
|
|
|
2014-08-22 11:42:05 +02:00
|
|
|
@Override
|
2014-11-12 10:43:39 +01:00
|
|
|
public TerminationDirective onUpstreamFinish(Context<Integer> ctx) {
|
|
|
|
|
return terminationEmit(Collections.singletonList(sum).iterator(), ctx);
|
2014-08-22 11:42:05 +02:00
|
|
|
}
|
2015-10-31 14:46:10 +01:00
|
|
|
|
2014-08-22 11:42:05 +02:00
|
|
|
};
|
2014-04-23 10:05:09 +02:00
|
|
|
}
|
2014-12-03 10:33:21 +01:00
|
|
|
});
|
2015-01-26 14:57:05 +01:00
|
|
|
Source.from(input).via(flow).runForeach(new Procedure<Integer>() {
|
2014-04-23 10:05:09 +02:00
|
|
|
public void apply(Integer elem) {
|
|
|
|
|
probe.getRef().tell(elem, ActorRef.noSender());
|
|
|
|
|
}
|
2014-08-15 15:37:09 +02:00
|
|
|
}, materializer);
|
2014-04-23 10:05:09 +02:00
|
|
|
|
|
|
|
|
probe.expectMsgEquals(0);
|
|
|
|
|
probe.expectMsgEquals(0);
|
|
|
|
|
probe.expectMsgEquals(1);
|
|
|
|
|
probe.expectMsgEquals(1);
|
|
|
|
|
probe.expectMsgEquals(2);
|
|
|
|
|
probe.expectMsgEquals(2);
|
|
|
|
|
probe.expectMsgEquals(3);
|
|
|
|
|
probe.expectMsgEquals(3);
|
|
|
|
|
probe.expectMsgEquals(6);
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-25 19:58:48 +01:00
|
|
|
@SuppressWarnings("unchecked")
|
2014-04-23 10:05:09 +02:00
|
|
|
@Test
|
2015-11-25 19:58:48 +01:00
|
|
|
public void mustBeAbleToUseGroupBy() throws Exception {
|
2014-10-20 20:53:03 +02:00
|
|
|
final Iterable<String> input = Arrays.asList("Aaa", "Abb", "Bcc", "Cdd", "Cee");
|
2016-01-20 10:00:37 +02:00
|
|
|
final Flow<String, List<String>, NotUsed> flow = Flow
|
2015-11-25 19:58:48 +01:00
|
|
|
.of(String.class)
|
|
|
|
|
.groupBy(3, new Function<String, String>() {
|
2015-01-28 14:19:50 +01:00
|
|
|
public String apply(String elem) {
|
|
|
|
|
return elem.substring(0, 1);
|
|
|
|
|
}
|
2015-11-25 19:58:48 +01:00
|
|
|
})
|
|
|
|
|
.grouped(10)
|
|
|
|
|
.mergeSubstreams();
|
2015-12-17 11:48:30 +02:00
|
|
|
|
2016-01-21 16:37:26 +01:00
|
|
|
final CompletionStage<List<List<String>>> future =
|
2016-02-12 01:36:21 +08:00
|
|
|
Source.from(input).via(flow).limit(10).runWith(Sink.<List<String>> seq(), materializer);
|
2016-01-21 16:37:26 +01:00
|
|
|
final Object[] result = future.toCompletableFuture().get(1, TimeUnit.SECONDS).toArray();
|
2015-11-25 19:58:48 +01:00
|
|
|
Arrays.sort(result, (Comparator<Object>)(Object) new Comparator<List<String>>() {
|
2014-10-27 14:35:41 +01:00
|
|
|
@Override
|
2015-11-25 19:58:48 +01:00
|
|
|
public int compare(List<String> o1, List<String> o2) {
|
|
|
|
|
return o1.get(0).charAt(0) - o2.get(0).charAt(0);
|
2014-04-23 10:05:09 +02:00
|
|
|
}
|
2015-11-25 19:58:48 +01:00
|
|
|
});
|
2014-04-23 10:05:09 +02:00
|
|
|
|
2015-11-25 19:58:48 +01:00
|
|
|
assertArrayEquals(new Object[] { Arrays.asList("Aaa", "Abb"), Arrays.asList("Bcc"), Arrays.asList("Cdd", "Cee") }, result);
|
2014-04-23 10:05:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
2015-11-25 19:58:48 +01:00
|
|
|
public void mustBeAbleToUseSplitWhen() throws Exception {
|
2014-11-12 10:16:22 +01:00
|
|
|
final Iterable<String> input = Arrays.asList("A", "B", "C", ".", "D", ".", "E", "F");
|
2016-01-20 10:00:37 +02:00
|
|
|
final Flow<String, List<String>, NotUsed> flow = Flow
|
2015-11-25 19:58:48 +01:00
|
|
|
.of(String.class)
|
|
|
|
|
.splitWhen(new Predicate<String>() {
|
|
|
|
|
public boolean test(String elem) {
|
|
|
|
|
return elem.equals(".");
|
2014-10-27 14:35:41 +01:00
|
|
|
}
|
2015-11-25 19:58:48 +01:00
|
|
|
})
|
|
|
|
|
.grouped(10)
|
|
|
|
|
.concatSubstreams();
|
2014-04-23 10:05:09 +02:00
|
|
|
|
2016-01-21 16:37:26 +01:00
|
|
|
final CompletionStage<List<List<String>>> future =
|
2016-02-12 01:36:21 +08:00
|
|
|
Source.from(input).via(flow).limit(10).runWith(Sink.<List<String>> seq(), materializer);
|
2016-01-21 16:37:26 +01:00
|
|
|
final List<List<String>> result = future.toCompletableFuture().get(1, TimeUnit.SECONDS);
|
2014-04-23 10:05:09 +02:00
|
|
|
|
2015-11-25 19:58:48 +01:00
|
|
|
assertEquals(Arrays.asList(Arrays.asList("A", "B", "C"), Arrays.asList(".", "D"), Arrays.asList(".", "E", "F")), result);
|
2014-04-23 10:05:09 +02:00
|
|
|
}
|
|
|
|
|
|
2015-04-14 13:44:24 +02:00
|
|
|
@Test
|
2015-11-25 19:58:48 +01:00
|
|
|
public void mustBeAbleToUseSplitAfter() throws Exception {
|
|
|
|
|
final Iterable<String> input = Arrays.asList("A", "B", "C", ".", "D", ".", "E", "F");
|
2016-01-20 10:00:37 +02:00
|
|
|
final Flow<String, List<String>, NotUsed> flow = Flow
|
2015-11-25 19:58:48 +01:00
|
|
|
.of(String.class)
|
|
|
|
|
.splitAfter(new Predicate<String>() {
|
|
|
|
|
public boolean test(String elem) {
|
|
|
|
|
return elem.equals(".");
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.grouped(10)
|
|
|
|
|
.concatSubstreams();
|
|
|
|
|
|
2016-01-21 16:37:26 +01:00
|
|
|
final CompletionStage<List<List<String>>> future =
|
2016-02-12 01:36:21 +08:00
|
|
|
Source.from(input).via(flow).limit(10).runWith(Sink.<List<String>> seq(), materializer);
|
2016-01-21 16:37:26 +01:00
|
|
|
final List<List<String>> result = future.toCompletableFuture().get(1, TimeUnit.SECONDS);
|
2015-11-25 19:58:48 +01:00
|
|
|
|
|
|
|
|
assertEquals(Arrays.asList(Arrays.asList("A", "B", "C", "."), Arrays.asList("D", "."), Arrays.asList("E", "F")), result);
|
2015-04-14 13:44:24 +02:00
|
|
|
}
|
|
|
|
|
|
2014-11-20 21:59:33 +01:00
|
|
|
public <T> Creator<Stage<T, T>> op() {
|
2015-04-23 20:59:55 +02:00
|
|
|
return new akka.japi.function.Creator<Stage<T, T>>() {
|
2014-11-20 21:59:33 +01:00
|
|
|
@Override
|
|
|
|
|
public PushPullStage<T, T> create() throws Exception {
|
2015-10-31 14:46:10 +01:00
|
|
|
return new PushPullStage<T, T>() {
|
2014-11-20 21:59:33 +01:00
|
|
|
@Override
|
2015-04-09 22:28:16 +02:00
|
|
|
public SyncDirective onPush(T element, Context<T> ctx) {
|
2014-11-20 21:59:33 +01:00
|
|
|
return ctx.push(element);
|
|
|
|
|
}
|
2015-10-31 14:46:10 +01:00
|
|
|
|
2014-11-20 21:59:33 +01:00
|
|
|
@Override
|
2015-04-09 22:28:16 +02:00
|
|
|
public SyncDirective onPull(Context<T> ctx) {
|
2014-11-20 21:59:33 +01:00
|
|
|
return ctx.pull();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseMerge() throws Exception {
|
2016-01-20 10:00:37 +02:00
|
|
|
final Flow<String, String, NotUsed> f1 =
|
2015-04-09 15:16:59 +02:00
|
|
|
Flow.of(String.class).transform(FlowTest.this.<String> op()).named("f1");
|
2016-01-20 10:00:37 +02:00
|
|
|
final Flow<String, String, NotUsed> f2 =
|
2015-04-09 15:16:59 +02:00
|
|
|
Flow.of(String.class).transform(FlowTest.this.<String> op()).named("f2");
|
|
|
|
|
@SuppressWarnings("unused")
|
2016-01-20 10:00:37 +02:00
|
|
|
final Flow<String, String, NotUsed> f3 =
|
2015-04-09 15:16:59 +02:00
|
|
|
Flow.of(String.class).transform(FlowTest.this.<String> op()).named("f3");
|
2015-01-28 14:19:50 +01:00
|
|
|
|
2016-01-20 10:00:37 +02:00
|
|
|
final Source<String, NotUsed> in1 = Source.from(Arrays.asList("a", "b", "c"));
|
|
|
|
|
final Source<String, NotUsed> in2 = Source.from(Arrays.asList("d", "e", "f"));
|
2015-01-28 14:19:50 +01:00
|
|
|
|
2016-01-20 21:01:27 +01:00
|
|
|
final Sink<String, Publisher<String>> publisher = Sink.asPublisher(AsPublisher.WITHOUT_FANOUT);
|
2015-10-31 14:46:10 +01:00
|
|
|
|
2016-01-20 10:00:37 +02:00
|
|
|
final Source<String, NotUsed> source = Source.fromGraph(
|
|
|
|
|
GraphDSL.create(new Function<GraphDSL.Builder<NotUsed>, SourceShape<String>>() {
|
2015-10-21 22:45:39 +02:00
|
|
|
@Override
|
2016-01-20 10:00:37 +02:00
|
|
|
public SourceShape<String> apply(Builder<NotUsed> b) throws Exception {
|
2015-10-21 22:45:39 +02:00
|
|
|
final UniformFanInShape<String, String> merge = b.add(Merge.<String>create(2));
|
|
|
|
|
b.from(b.add(in1)).via(b.add(f1)).toInlet(merge.in(0));
|
|
|
|
|
b.from(b.add(in2)).via(b.add(f2)).toInlet(merge.in(1));
|
|
|
|
|
return new SourceShape<String>(merge.out());
|
|
|
|
|
}
|
|
|
|
|
}));
|
2014-11-20 21:59:33 +01:00
|
|
|
|
|
|
|
|
// collecting
|
2015-01-28 14:19:50 +01:00
|
|
|
final Publisher<String> pub = source.runWith(publisher, materializer);
|
2016-02-12 01:36:21 +08:00
|
|
|
final CompletionStage<List<String>> all = Source.fromPublisher(pub).limit(100).runWith(Sink.<String>seq(), materializer);
|
2014-11-20 21:59:33 +01:00
|
|
|
|
2016-01-21 16:37:26 +01:00
|
|
|
final List<String> result = all.toCompletableFuture().get(200, TimeUnit.MILLISECONDS);
|
2014-11-20 21:59:33 +01:00
|
|
|
assertEquals(new HashSet<Object>(Arrays.asList("a", "b", "c", "d", "e", "f")), new HashSet<String>(result));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseZip() {
|
|
|
|
|
final JavaTestKit probe = new JavaTestKit(system);
|
|
|
|
|
final Iterable<String> input1 = Arrays.asList("A", "B", "C");
|
|
|
|
|
final Iterable<Integer> input2 = Arrays.asList(1, 2, 3);
|
|
|
|
|
|
2016-01-20 10:00:37 +02:00
|
|
|
RunnableGraph.fromGraph(GraphDSL.create(new Function<Builder<NotUsed>, ClosedShape>(){
|
|
|
|
|
public ClosedShape apply(Builder<NotUsed> b) {
|
2015-12-14 14:52:06 +01:00
|
|
|
final Outlet<String> in1 = b.add(Source.from(input1)).out();
|
|
|
|
|
final Outlet<Integer> in2 = b.add(Source.from(input2)).out();
|
2015-10-21 22:45:39 +02:00
|
|
|
final FanInShape2<String, Integer, Pair<String, Integer>> zip = b.add(Zip.<String, Integer>create());
|
|
|
|
|
final SinkShape<Pair<String, Integer>> out =
|
|
|
|
|
b.add(Sink.foreach(new Procedure<Pair<String, Integer>>() {
|
|
|
|
|
@Override
|
|
|
|
|
public void apply(Pair<String, Integer> param) throws Exception {
|
|
|
|
|
probe.getRef().tell(param, ActorRef.noSender());
|
|
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
b.from(in1).toInlet(zip.in0());
|
|
|
|
|
b.from(in2).toInlet(zip.in1());
|
|
|
|
|
b.from(zip.out()).to(out);
|
|
|
|
|
return ClosedShape.getInstance();
|
|
|
|
|
}
|
|
|
|
|
})).run(materializer);
|
2014-11-20 21:59:33 +01:00
|
|
|
|
|
|
|
|
List<Object> output = Arrays.asList(probe.receiveN(3));
|
|
|
|
|
List<Pair<String, Integer>> expected = Arrays.asList(new Pair<String, Integer>("A", 1), new Pair<String, Integer>(
|
|
|
|
|
"B", 2), new Pair<String, Integer>("C", 3));
|
|
|
|
|
assertEquals(expected, output);
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-20 20:53:03 +02:00
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseConcat() {
|
|
|
|
|
final JavaTestKit probe = new JavaTestKit(system);
|
|
|
|
|
final Iterable<String> input1 = Arrays.asList("A", "B", "C");
|
|
|
|
|
final Iterable<String> input2 = Arrays.asList("D", "E", "F");
|
|
|
|
|
|
2016-01-21 16:37:26 +01:00
|
|
|
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);
|
2015-01-26 14:57:05 +01:00
|
|
|
in1.via(flow.concat(in2)).runForeach(new Procedure<String>() {
|
2014-10-20 20:53:03 +02:00
|
|
|
public void apply(String elem) {
|
|
|
|
|
probe.getRef().tell(elem, ActorRef.noSender());
|
|
|
|
|
}
|
|
|
|
|
}, materializer);
|
2015-12-07 12:16:59 +00:00
|
|
|
|
|
|
|
|
List<Object> output = Arrays.asList(probe.receiveN(6));
|
|
|
|
|
assertEquals(Arrays.asList("A", "B", "C", "D", "E", "F"), output);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUsePrepend() {
|
|
|
|
|
final JavaTestKit probe = new JavaTestKit(system);
|
|
|
|
|
final Iterable<String> input1 = Arrays.asList("A", "B", "C");
|
|
|
|
|
final Iterable<String> input2 = Arrays.asList("D", "E", "F");
|
|
|
|
|
|
2016-01-21 16:37:26 +01:00
|
|
|
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);
|
2015-12-07 12:16:59 +00:00
|
|
|
in2.via(flow.prepend(in1)).runForeach(new Procedure<String>() {
|
|
|
|
|
public void apply(String elem) {
|
|
|
|
|
probe.getRef().tell(elem, ActorRef.noSender());
|
|
|
|
|
}
|
|
|
|
|
}, materializer);
|
2014-10-20 20:53:03 +02:00
|
|
|
|
|
|
|
|
List<Object> output = Arrays.asList(probe.receiveN(6));
|
|
|
|
|
assertEquals(Arrays.asList("A", "B", "C", "D", "E", "F"), output);
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-16 14:21:15 +02:00
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUsePrefixAndTail() throws Exception {
|
|
|
|
|
final JavaTestKit probe = new JavaTestKit(system);
|
2014-10-20 20:53:03 +02:00
|
|
|
final Iterable<Integer> input = Arrays.asList(1, 2, 3, 4, 5, 6);
|
2016-01-21 16:37:26 +01:00
|
|
|
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 =
|
2016-01-20 10:00:37 +02:00
|
|
|
Source.from(input).via(flow).runWith(Sink.<Pair<List<Integer>, Source<Integer, NotUsed>>>head(), materializer);
|
2016-01-21 16:37:26 +01:00
|
|
|
Pair<List<Integer>, Source<Integer, NotUsed>> result = future.toCompletableFuture().get(3, TimeUnit.SECONDS);
|
2014-05-16 14:21:15 +02:00
|
|
|
assertEquals(Arrays.asList(1, 2, 3), result.first());
|
|
|
|
|
|
2016-02-12 01:36:21 +08:00
|
|
|
CompletionStage<List<Integer>> tailFuture = result.second().limit(4).runWith(Sink.<Integer>seq(), materializer);
|
2016-01-21 16:37:26 +01:00
|
|
|
List<Integer> tailResult = tailFuture.toCompletableFuture().get(3, TimeUnit.SECONDS);
|
2014-05-16 14:21:15 +02:00
|
|
|
assertEquals(Arrays.asList(4, 5, 6), tailResult);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
2014-10-03 17:33:14 +02:00
|
|
|
public void mustBeAbleToUseConcatAllWithSources() throws Exception {
|
2014-08-15 15:37:09 +02:00
|
|
|
final JavaTestKit probe = new JavaTestKit(system);
|
2014-10-20 20:53:03 +02:00
|
|
|
final Iterable<Integer> input1 = Arrays.asList(1, 2, 3);
|
|
|
|
|
final Iterable<Integer> input2 = Arrays.asList(4, 5);
|
2014-05-16 14:21:15 +02:00
|
|
|
|
2016-01-20 10:00:37 +02:00
|
|
|
final List<Source<Integer, NotUsed>> mainInputs = new ArrayList<Source<Integer,NotUsed>>();
|
2015-01-28 14:19:50 +01:00
|
|
|
mainInputs.add(Source.from(input1));
|
|
|
|
|
mainInputs.add(Source.from(input2));
|
2014-05-16 14:21:15 +02:00
|
|
|
|
2016-01-21 16:37:26 +01:00
|
|
|
final Flow<Source<Integer, NotUsed>, List<Integer>, NotUsed> flow = Flow.<Source<Integer, NotUsed>>create().
|
2016-01-20 10:00:37 +02:00
|
|
|
flatMapConcat(ConstantFun.<Source<Integer, NotUsed>>javaIdentityFunction()).grouped(6);
|
2016-01-21 16:37:26 +01:00
|
|
|
CompletionStage<List<Integer>> future = Source.from(mainInputs).via(flow)
|
2014-11-06 18:13:06 +01:00
|
|
|
.runWith(Sink.<List<Integer>>head(), materializer);
|
2014-05-16 14:21:15 +02:00
|
|
|
|
2016-01-21 16:37:26 +01:00
|
|
|
List<Integer> result = future.toCompletableFuture().get(3, TimeUnit.SECONDS);
|
2014-05-16 14:21:15 +02:00
|
|
|
|
2014-08-15 15:37:09 +02:00
|
|
|
assertEquals(Arrays.asList(1, 2, 3, 4, 5), result);
|
2014-05-16 14:21:15 +02:00
|
|
|
}
|
2015-12-17 11:48:30 +02:00
|
|
|
|
2015-12-01 18:03:30 +01:00
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseFlatMapMerge() throws Exception {
|
|
|
|
|
final JavaTestKit probe = new JavaTestKit(system);
|
|
|
|
|
final Iterable<Integer> input1 = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
|
|
|
|
|
final Iterable<Integer> input2 = Arrays.asList(10, 11, 12, 13, 14, 15, 16, 17, 18, 19);
|
|
|
|
|
final Iterable<Integer> input3 = Arrays.asList(20, 21, 22, 23, 24, 25, 26, 27, 28, 29);
|
|
|
|
|
final Iterable<Integer> input4 = Arrays.asList(30, 31, 32, 33, 34, 35, 36, 37, 38, 39);
|
|
|
|
|
|
2016-01-20 10:00:37 +02:00
|
|
|
final List<Source<Integer, NotUsed>> mainInputs = new ArrayList<Source<Integer,NotUsed>>();
|
2015-12-01 18:03:30 +01:00
|
|
|
mainInputs.add(Source.from(input1));
|
|
|
|
|
mainInputs.add(Source.from(input2));
|
|
|
|
|
mainInputs.add(Source.from(input3));
|
|
|
|
|
mainInputs.add(Source.from(input4));
|
|
|
|
|
|
2016-01-21 16:37:26 +01:00
|
|
|
final Flow<Source<Integer, NotUsed>, List<Integer>, NotUsed> flow = Flow.<Source<Integer, NotUsed>>create().
|
2016-01-20 10:00:37 +02:00
|
|
|
flatMapMerge(3, ConstantFun.<Source<Integer, NotUsed>>javaIdentityFunction()).grouped(60);
|
2016-01-21 16:37:26 +01:00
|
|
|
CompletionStage<List<Integer>> future = Source.from(mainInputs).via(flow)
|
2015-12-01 18:03:30 +01:00
|
|
|
.runWith(Sink.<List<Integer>>head(), materializer);
|
|
|
|
|
|
2016-01-21 16:37:26 +01:00
|
|
|
List<Integer> result = future.toCompletableFuture().get(3, TimeUnit.SECONDS);
|
2015-12-01 18:03:30 +01:00
|
|
|
final Set<Integer> set = new HashSet<Integer>();
|
|
|
|
|
for (Integer i: result) {
|
|
|
|
|
set.add(i);
|
|
|
|
|
}
|
|
|
|
|
final Set<Integer> expected = new HashSet<Integer>();
|
|
|
|
|
for (int i = 0; i < 40; ++i) {
|
|
|
|
|
expected.add(i);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assertEquals(expected, set);
|
|
|
|
|
}
|
2014-05-16 14:21:15 +02:00
|
|
|
|
2014-05-20 16:02:09 +02:00
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseBuffer() throws Exception {
|
|
|
|
|
final JavaTestKit probe = new JavaTestKit(system);
|
|
|
|
|
final List<String> input = Arrays.asList("A", "B", "C");
|
2016-01-20 10:00:37 +02:00
|
|
|
final Flow<String, List<String>, NotUsed> flow = Flow.of(String.class).buffer(2, OverflowStrategy.backpressure()).grouped(4);
|
2016-01-21 16:37:26 +01:00
|
|
|
final CompletionStage<List<String>> future = Source.from(input).via(flow)
|
2014-11-06 18:13:06 +01:00
|
|
|
.runWith(Sink.<List<String>>head(), materializer);
|
2014-10-03 17:33:14 +02:00
|
|
|
|
2016-01-21 16:37:26 +01:00
|
|
|
List<String> result = future.toCompletableFuture().get(3, TimeUnit.SECONDS);
|
2014-05-20 16:02:09 +02:00
|
|
|
assertEquals(input, result);
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-16 11:46:36 -05:00
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseWatchTermination() throws Exception {
|
|
|
|
|
final List<String> input = Arrays.asList("A", "B", "C");
|
2016-01-25 11:14:31 +01:00
|
|
|
CompletionStage<Done> future = Source.from(input)
|
|
|
|
|
.watchTermination(Keep.right())
|
2016-01-16 11:46:36 -05:00
|
|
|
.to(Sink.ignore()).run(materializer);
|
|
|
|
|
|
2016-01-25 11:14:31 +01:00
|
|
|
assertEquals(Done.getInstance(), future.toCompletableFuture().get(3, TimeUnit.SECONDS));
|
2016-01-16 11:46:36 -05:00
|
|
|
}
|
|
|
|
|
|
2014-05-20 16:02:09 +02:00
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseConflate() throws Exception {
|
2014-08-15 15:37:09 +02:00
|
|
|
final JavaTestKit probe = new JavaTestKit(system);
|
2014-12-03 10:33:21 +01:00
|
|
|
final List<String> input = Arrays.asList("A", "B", "C");
|
2016-01-22 15:22:30 +01:00
|
|
|
final Flow<String, String, NotUsed> flow = Flow.of(String.class).conflateWithSeed(new Function<String, String>() {
|
2014-08-15 15:37:09 +02:00
|
|
|
@Override
|
|
|
|
|
public String apply(String s) throws Exception {
|
|
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
}, new Function2<String, String, String>() {
|
|
|
|
|
@Override
|
2014-12-03 10:33:21 +01:00
|
|
|
public String apply(String aggr, String in) throws Exception {
|
|
|
|
|
return aggr + in;
|
2014-08-15 15:37:09 +02:00
|
|
|
}
|
2014-12-03 10:33:21 +01:00
|
|
|
});
|
2016-01-21 16:37:26 +01:00
|
|
|
CompletionStage<String> future = Source.from(input).via(flow).runFold("", (aggr, in) -> aggr + in, materializer);
|
|
|
|
|
String result = future.toCompletableFuture().get(3, TimeUnit.SECONDS);
|
2014-12-03 10:33:21 +01:00
|
|
|
assertEquals("ABC", result);
|
2016-01-22 15:22:30 +01:00
|
|
|
|
|
|
|
|
final Flow<String, String, NotUsed> flow2 = Flow.of(String.class).conflate((a, b) -> a + b);
|
|
|
|
|
|
|
|
|
|
CompletionStage<String> future2 = Source.from(input).via(flow2).runFold("", (a, b) -> a + b, materializer);
|
|
|
|
|
String result2 = future2.toCompletableFuture().get(3, TimeUnit.SECONDS);
|
|
|
|
|
assertEquals("ABC", result2);
|
2016-01-20 18:20:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseBatch() throws Exception {
|
|
|
|
|
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).batch(3L, new Function<String, String>() {
|
|
|
|
|
@Override
|
|
|
|
|
public String apply(String s) throws Exception {
|
|
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
}, new Function2<String, String, String>() {
|
|
|
|
|
@Override
|
|
|
|
|
public String apply(String aggr, String in) throws Exception {
|
|
|
|
|
return aggr + in;
|
|
|
|
|
}
|
|
|
|
|
});
|
2016-01-21 16:37:26 +01:00
|
|
|
CompletionStage<String> future = Source.from(input).via(flow).runFold("", (aggr, in) -> aggr + in, materializer);
|
|
|
|
|
String result = future.toCompletableFuture().get(3, TimeUnit.SECONDS);
|
2016-01-20 18:20:12 +02:00
|
|
|
assertEquals("ABC", result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseBatchWeighted() throws Exception {
|
|
|
|
|
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).batchWeighted(3L, new Function<String, Object>() {
|
|
|
|
|
@Override
|
|
|
|
|
public Object apply(String s) throws Exception {
|
|
|
|
|
return 1L;
|
|
|
|
|
}
|
|
|
|
|
}, new Function<String, String>() {
|
|
|
|
|
@Override
|
|
|
|
|
public String apply(String s) throws Exception {
|
|
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
}, new Function2<String, String, String>() {
|
|
|
|
|
@Override
|
|
|
|
|
public String apply(String aggr, String in) throws Exception {
|
|
|
|
|
return aggr + in;
|
|
|
|
|
}
|
|
|
|
|
});
|
2016-01-21 16:37:26 +01:00
|
|
|
CompletionStage<String> future = Source.from(input).via(flow).runFold("", (aggr, in) -> aggr + in, materializer);
|
|
|
|
|
String result = future.toCompletableFuture().get(3, TimeUnit.SECONDS);
|
2016-01-20 18:20:12 +02:00
|
|
|
assertEquals("ABC", result);
|
2014-05-20 16:02:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseExpand() throws Exception {
|
2014-08-15 15:37:09 +02:00
|
|
|
final JavaTestKit probe = new JavaTestKit(system);
|
|
|
|
|
final List<String> input = Arrays.asList("A", "B", "C");
|
2016-01-18 11:29:14 +01:00
|
|
|
final Flow<String, String, NotUsed> flow = Flow.of(String.class).expand(in -> Stream.iterate(in, i -> i).iterator());
|
2016-01-21 16:37:26 +01:00
|
|
|
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);
|
2014-08-15 15:37:09 +02:00
|
|
|
assertEquals("A", result);
|
2014-05-20 16:02:09 +02:00
|
|
|
}
|
2014-08-15 15:37:09 +02:00
|
|
|
|
2014-05-22 20:58:38 +02:00
|
|
|
@Test
|
2014-12-03 10:33:21 +01:00
|
|
|
public void mustBeAbleToUseMapAsync() throws Exception {
|
2014-05-23 13:52:39 +02:00
|
|
|
final JavaTestKit probe = new JavaTestKit(system);
|
2014-10-20 20:53:03 +02:00
|
|
|
final Iterable<String> input = Arrays.asList("a", "b", "c");
|
2016-01-21 16:37:26 +01:00
|
|
|
final Flow<String, String, NotUsed> flow = Flow.of(String.class).mapAsync(4, elem -> CompletableFuture.completedFuture(elem.toUpperCase()));
|
2015-01-26 14:57:05 +01:00
|
|
|
Source.from(input).via(flow).runForeach(new Procedure<String>() {
|
2014-05-23 13:52:39 +02:00
|
|
|
public void apply(String elem) {
|
|
|
|
|
probe.getRef().tell(elem, ActorRef.noSender());
|
|
|
|
|
}
|
2014-08-15 15:37:09 +02:00
|
|
|
}, materializer);
|
2014-05-23 13:52:39 +02:00
|
|
|
probe.expectMsgEquals("A");
|
|
|
|
|
probe.expectMsgEquals("B");
|
|
|
|
|
probe.expectMsgEquals("C");
|
|
|
|
|
}
|
2015-06-13 14:02:37 -04:00
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToRecover() throws Exception {
|
2015-08-16 00:04:04 -04:00
|
|
|
final TestPublisher.ManualProbe<Integer> publisherProbe = TestPublisher.manualProbe(true,system);
|
2015-06-13 14:02:37 -04:00
|
|
|
final JavaTestKit probe = new JavaTestKit(system);
|
2015-08-16 00:04:04 -04:00
|
|
|
|
2016-01-21 16:37:26 +01:00
|
|
|
final Source<Integer, NotUsed> source = Source.fromPublisher(publisherProbe);
|
|
|
|
|
final Flow<Integer, Integer, NotUsed> flow = Flow.of(Integer.class).map(
|
2015-06-13 14:02:37 -04:00
|
|
|
new Function<Integer, Integer>() {
|
|
|
|
|
public Integer apply(Integer elem) {
|
|
|
|
|
if (elem == 2) throw new RuntimeException("ex");
|
|
|
|
|
else return elem;
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.recover(new JavaPartialFunction<Throwable, Integer>() {
|
|
|
|
|
public Integer apply(Throwable elem, boolean isCheck) {
|
|
|
|
|
if (isCheck) return null;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2016-01-21 16:37:26 +01:00
|
|
|
final CompletionStage<Done> future =
|
|
|
|
|
source.via(flow).runWith(Sink.foreach(elem -> probe.getRef().tell(elem, ActorRef.noSender())), materializer);
|
2015-06-13 14:02:37 -04:00
|
|
|
|
2015-08-16 00:04:04 -04:00
|
|
|
final PublisherProbeSubscription<Integer> s = publisherProbe.expectSubscription();
|
|
|
|
|
|
|
|
|
|
s.sendNext(0);
|
2015-06-13 14:02:37 -04:00
|
|
|
probe.expectMsgEquals(0);
|
2015-08-16 00:04:04 -04:00
|
|
|
s.sendNext(1);
|
2015-06-13 14:02:37 -04:00
|
|
|
probe.expectMsgEquals(1);
|
2015-08-16 00:04:04 -04:00
|
|
|
s.sendNext(2);
|
2015-06-13 14:02:37 -04:00
|
|
|
probe.expectMsgEquals(0);
|
2016-01-21 16:37:26 +01:00
|
|
|
future.toCompletableFuture().get(200, TimeUnit.MILLISECONDS);
|
2015-06-13 14:02:37 -04:00
|
|
|
}
|
|
|
|
|
|
2016-01-29 22:06:36 -05:00
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToRecoverWithSource() throws Exception {
|
|
|
|
|
final TestPublisher.ManualProbe<Integer> publisherProbe = TestPublisher.manualProbe(true,system);
|
|
|
|
|
final JavaTestKit probe = new JavaTestKit(system);
|
|
|
|
|
final Iterable<Integer> recover = Arrays.asList(55, 0);
|
|
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
else return elem;
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.recoverWith(new JavaPartialFunction<Throwable, Source<Integer, NotUsed>>() {
|
|
|
|
|
public Source<Integer, NotUsed> apply(Throwable elem, boolean isCheck) {
|
|
|
|
|
if (isCheck) return null;
|
|
|
|
|
return Source.from(recover);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
final CompletionStage<Done> future =
|
|
|
|
|
source.via(flow).runWith(Sink.foreach(elem -> probe.getRef().tell(elem, ActorRef.noSender())), materializer);
|
|
|
|
|
|
|
|
|
|
final PublisherProbeSubscription<Integer> s = publisherProbe.expectSubscription();
|
|
|
|
|
|
|
|
|
|
s.sendNext(0);
|
|
|
|
|
probe.expectMsgEquals(0);
|
|
|
|
|
s.sendNext(1);
|
|
|
|
|
probe.expectMsgEquals(1);
|
|
|
|
|
s.sendNext(2);
|
|
|
|
|
probe.expectMsgEquals(55);
|
|
|
|
|
probe.expectMsgEquals(0);
|
|
|
|
|
future.toCompletableFuture().get(200, TimeUnit.MILLISECONDS);
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-24 00:22:24 -04:00
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToMaterializeIdentityWithJavaFlow() throws Exception {
|
|
|
|
|
final JavaTestKit probe = new JavaTestKit(system);
|
|
|
|
|
final List<String> input = Arrays.asList("A", "B", "C");
|
|
|
|
|
|
2016-01-21 16:37:26 +01:00
|
|
|
Flow<String,String,NotUsed> otherFlow = Flow.of(String.class);
|
2015-08-24 00:22:24 -04:00
|
|
|
|
2016-01-21 16:37:26 +01:00
|
|
|
Flow<String,String,NotUsed> myFlow = Flow.of(String.class).via(otherFlow);
|
2015-08-24 00:22:24 -04:00
|
|
|
Source.from(input).via(myFlow).runWith(Sink.foreach(new Procedure<String>() { // Scala Future
|
|
|
|
|
public void apply(String elem) {
|
|
|
|
|
probe.getRef().tell(elem, ActorRef.noSender());
|
|
|
|
|
}
|
|
|
|
|
}), materializer);
|
|
|
|
|
|
|
|
|
|
probe.expectMsgAllOf("A","B","C");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToMaterializeIdentityToJavaSink() throws Exception {
|
|
|
|
|
final JavaTestKit probe = new JavaTestKit(system);
|
|
|
|
|
final List<String> input = Arrays.asList("A", "B", "C");
|
2016-01-21 16:37:26 +01:00
|
|
|
Flow<String,String,NotUsed> otherFlow = Flow.of(String.class);
|
2015-08-24 00:22:24 -04:00
|
|
|
|
2016-01-20 10:00:37 +02:00
|
|
|
Sink<String,NotUsed> sink = Flow.of(String.class).to(otherFlow.to(Sink.foreach(new Procedure<String>() { // Scala Future
|
2015-08-24 00:22:24 -04:00
|
|
|
public void apply(String elem) {
|
|
|
|
|
probe.getRef().tell(elem, ActorRef.noSender());
|
|
|
|
|
}
|
|
|
|
|
})));
|
|
|
|
|
|
|
|
|
|
Source.from(input).to(sink).run(materializer);
|
|
|
|
|
probe.expectMsgAllOf("A","B","C");
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-02 16:59:32 +10:00
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToBroadcastEagerCancel() throws Exception {
|
2016-01-20 10:00:37 +02:00
|
|
|
final Sink<String, NotUsed> sink = Sink.fromGraph(
|
|
|
|
|
GraphDSL.create(new Function<GraphDSL.Builder<NotUsed>, SinkShape<String>>() {
|
2015-10-21 22:45:39 +02:00
|
|
|
@Override
|
2016-01-20 10:00:37 +02:00
|
|
|
public SinkShape<String> apply(Builder<NotUsed> b) throws Exception {
|
2015-10-21 22:45:39 +02:00
|
|
|
final UniformFanOutShape<String, String> broadcast = b.add(Broadcast.<String>create(2, true));
|
|
|
|
|
final SinkShape<String> out1 = b.add(Sink.<String>cancelled());
|
|
|
|
|
final SinkShape<String> out2 = b.add(Sink.<String>ignore());
|
|
|
|
|
b.from(broadcast.out(0)).to(out1);
|
|
|
|
|
b.from(broadcast.out(1)).to(out2);
|
|
|
|
|
return new SinkShape<String>(broadcast.in());
|
|
|
|
|
}
|
|
|
|
|
}));
|
2015-09-02 16:59:32 +10:00
|
|
|
|
|
|
|
|
final JavaTestKit probe = new JavaTestKit(system);
|
|
|
|
|
Source<String, ActorRef> source = Source.actorRef(1, OverflowStrategy.dropNew());
|
2016-01-20 10:00:37 +02:00
|
|
|
final ActorRef actor = source.toMat(sink, Keep.<ActorRef, NotUsed>left()).run(materializer);
|
2015-09-02 16:59:32 +10:00
|
|
|
probe.watch(actor);
|
|
|
|
|
probe.expectTerminated(actor);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2015-09-21 08:10:45 -04:00
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseZipWith() throws Exception {
|
|
|
|
|
final JavaTestKit probe = new JavaTestKit(system);
|
|
|
|
|
final Iterable<String> input1 = Arrays.asList("A", "B", "C");
|
|
|
|
|
final Iterable<String> input2 = Arrays.asList("D", "E", "F");
|
|
|
|
|
|
|
|
|
|
Source.from(input1).via(Flow.of(String.class).zipWith(Source.from(input2), new Function2<String, String, String>() {
|
|
|
|
|
public String apply(String s1, String s2) {
|
|
|
|
|
return s1 + "-" + s2;
|
|
|
|
|
}
|
|
|
|
|
})).runForeach(new Procedure<String>() {
|
|
|
|
|
public void apply(String elem) {
|
|
|
|
|
probe.getRef().tell(elem, ActorRef.noSender());
|
|
|
|
|
}
|
|
|
|
|
}, materializer);
|
|
|
|
|
|
|
|
|
|
probe.expectMsgEquals("A-D");
|
|
|
|
|
probe.expectMsgEquals("B-E");
|
|
|
|
|
probe.expectMsgEquals("C-F");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseZip2() throws Exception {
|
|
|
|
|
final JavaTestKit probe = new JavaTestKit(system);
|
|
|
|
|
final Iterable<String> input1 = Arrays.asList("A", "B", "C");
|
|
|
|
|
final Iterable<String> input2 = Arrays.asList("D", "E", "F");
|
|
|
|
|
|
|
|
|
|
Source.from(input1).via(Flow.of(String.class).zip(Source.from(input2)))
|
|
|
|
|
.runForeach(new Procedure<Pair<String, String>>() {
|
|
|
|
|
public void apply(Pair<String, String> elem) {
|
|
|
|
|
probe.getRef().tell(elem, ActorRef.noSender());
|
|
|
|
|
}
|
|
|
|
|
}, materializer);
|
|
|
|
|
|
|
|
|
|
probe.expectMsgEquals(new Pair<String,String>("A", "D"));
|
|
|
|
|
probe.expectMsgEquals(new Pair<String,String>("B", "E"));
|
|
|
|
|
probe.expectMsgEquals(new Pair<String,String>("C", "F"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseMerge2() {
|
|
|
|
|
final JavaTestKit probe = new JavaTestKit(system);
|
|
|
|
|
final Iterable<String> input1 = Arrays.asList("A", "B", "C");
|
|
|
|
|
final Iterable<String> input2 = Arrays.asList("D", "E", "F");
|
|
|
|
|
|
|
|
|
|
Source.from(input1).via(Flow.of(String.class).merge(Source.from(input2)))
|
|
|
|
|
.runForeach(new Procedure<String>() {
|
|
|
|
|
public void apply(String elem) {
|
|
|
|
|
probe.getRef().tell(elem, ActorRef.noSender());
|
|
|
|
|
}
|
|
|
|
|
}, materializer);
|
|
|
|
|
|
|
|
|
|
probe.expectMsgAllOf("A", "B", "C", "D", "E", "F");
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-02 15:30:10 +01:00
|
|
|
@Test
|
2016-01-21 16:37:26 +01:00
|
|
|
public void mustBeAbleToUseInitialTimeout() throws Throwable {
|
2015-11-02 15:30:10 +01:00
|
|
|
try {
|
2016-01-21 16:37:26 +01:00
|
|
|
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) {
|
2015-11-02 15:30:10 +01:00
|
|
|
// expected
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
2016-01-21 16:37:26 +01:00
|
|
|
public void mustBeAbleToUseCompletionTimeout() throws Throwable {
|
2015-11-02 15:30:10 +01:00
|
|
|
try {
|
2016-01-21 16:37:26 +01:00
|
|
|
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) {
|
2015-11-02 15:30:10 +01:00
|
|
|
// expected
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
2016-01-21 16:37:26 +01:00
|
|
|
public void mustBeAbleToUseIdleTimeout() throws Throwable {
|
2015-11-02 15:30:10 +01:00
|
|
|
try {
|
2016-01-21 16:37:26 +01:00
|
|
|
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) {
|
2015-11-02 15:30:10 +01:00
|
|
|
// expected
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseKeepAlive() throws Exception {
|
2016-01-21 16:37:26 +01:00
|
|
|
Integer result =
|
2015-11-02 15:30:10 +01:00
|
|
|
Source.<Integer>maybe()
|
|
|
|
|
.via(Flow.of(Integer.class)
|
|
|
|
|
.keepAlive(Duration.create(1, "second"), new Creator<Integer>() {
|
|
|
|
|
public Integer create() {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
.takeWithin(Duration.create(1500, "milliseconds"))
|
2016-01-21 16:37:26 +01:00
|
|
|
.runWith(Sink.<Integer>head(), materializer)
|
|
|
|
|
.toCompletableFuture().get(3, TimeUnit.SECONDS);
|
2015-11-02 15:30:10 +01:00
|
|
|
|
|
|
|
|
assertEquals((Object) 0, result);
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-10 12:18:24 +01:00
|
|
|
@Test
|
|
|
|
|
public void shouldBePossibleToCreateFromFunction() throws Exception {
|
|
|
|
|
List<Integer> out = Source.range(0, 2).via(Flow.fromFunction((Integer x) -> x + 1))
|
|
|
|
|
.runWith(Sink.seq(), materializer).toCompletableFuture().get(3, TimeUnit.SECONDS);
|
|
|
|
|
|
|
|
|
|
assertEquals(Arrays.asList(1, 2, 3), out);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-22 20:56:02 +01:00
|
|
|
public void mustSuitablyOverrideAttributeHandlingMethods() {
|
|
|
|
|
@SuppressWarnings("unused")
|
2016-01-20 10:00:37 +02:00
|
|
|
final Flow<Integer, Integer, NotUsed> f =
|
2015-12-22 20:56:02 +01:00
|
|
|
Flow.of(Integer.class).withAttributes(Attributes.name("")).addAttributes(Attributes.asyncBoundary()).named("");
|
|
|
|
|
}
|
2014-04-23 10:05:09 +02:00
|
|
|
}
|