2014-12-16 11:48:42 +01:00
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2014 Typesafe Inc. <http://www.typesafe.com>
|
|
|
|
|
*/
|
2014-04-23 10:05:09 +02:00
|
|
|
package akka.stream.javadsl;
|
|
|
|
|
|
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;
|
2014-10-03 17:33:14 +02:00
|
|
|
import akka.japi.Pair;
|
2015-01-28 14:19:50 +01:00
|
|
|
import akka.stream.Outlet;
|
2014-11-03 15:29:02 +01:00
|
|
|
import akka.stream.OverflowStrategy;
|
2014-11-07 15:00:50 +01:00
|
|
|
import akka.stream.StreamTest;
|
2014-11-12 10:43:39 +01:00
|
|
|
import akka.stream.stage.*;
|
2015-01-28 14:19:50 +01:00
|
|
|
import akka.stream.javadsl.FlowGraph.Builder;
|
2014-10-03 17:33:14 +02:00
|
|
|
import akka.stream.javadsl.japi.*;
|
2015-01-28 14:19:50 +01:00
|
|
|
import akka.stream.*;
|
2014-08-22 11:42:05 +02:00
|
|
|
import akka.stream.testkit.AkkaSpec;
|
|
|
|
|
import akka.testkit.JavaTestKit;
|
2014-11-12 10:43:39 +01:00
|
|
|
|
2014-11-20 21:59:33 +01:00
|
|
|
import org.reactivestreams.Publisher;
|
2015-04-02 16:52:30 +02:00
|
|
|
import scala.runtime.Boxed;
|
2014-11-20 21:59:33 +01:00
|
|
|
import scala.runtime.BoxedUnit;
|
2014-04-23 10:05:09 +02:00
|
|
|
import org.junit.ClassRule;
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
import scala.concurrent.Await;
|
|
|
|
|
import scala.concurrent.Future;
|
|
|
|
|
import scala.concurrent.duration.FiniteDuration;
|
2014-11-20 21:59:33 +01:00
|
|
|
import scala.concurrent.duration.Duration;
|
2014-08-22 11:42:05 +02:00
|
|
|
import java.util.*;
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
import static org.junit.Assert.assertEquals;
|
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);
|
2015-01-28 14:19:50 +01:00
|
|
|
final Source<Integer, ?> ints = Source.from(input);
|
|
|
|
|
final Flow<Integer, String, ?> 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
|
|
|
});
|
2015-01-28 14:19:50 +01:00
|
|
|
final Flow<String, String, ?> flow2 = Flow.of(String.class).grouped(2
|
2014-12-03 10:33:21 +01:00
|
|
|
).mapConcat(new Function<java.util.List<String>, java.util.List<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)
|
|
|
|
|
).mapConcat(new Function<java.util.List<String>, java.util.List<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
|
|
|
});
|
|
|
|
|
|
2015-01-26 14:57:05 +01:00
|
|
|
ints.via(flow1.via(flow2)).runFold("", new Function2<String, String, String>() {
|
2014-10-27 14:35:41 +01:00
|
|
|
public String apply(String acc, String elem) {
|
|
|
|
|
return acc + elem;
|
|
|
|
|
}
|
2014-12-03 10:33:21 +01:00
|
|
|
}, materializer
|
|
|
|
|
).foreach(new Foreach<String>() { // Scala Future
|
2014-10-27 14:35:41 +01:00
|
|
|
public void each(String elem) {
|
|
|
|
|
probe.getRef().tell(elem, ActorRef.noSender());
|
|
|
|
|
}
|
|
|
|
|
}, system.dispatcher());
|
2014-04-23 10:05:09 +02:00
|
|
|
|
|
|
|
|
probe.expectMsgEquals("de");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
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
|
2015-01-28 14:19:50 +01:00
|
|
|
final Flow<Integer, Integer, ?> 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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
};
|
2014-08-22 11:42:05 +02:00
|
|
|
}
|
2014-11-12 10:43:39 +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
|
|
|
}
|
2014-11-12 10:43:39 +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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseGroupBy() {
|
|
|
|
|
final JavaTestKit probe = new JavaTestKit(system);
|
2014-10-20 20:53:03 +02:00
|
|
|
final Iterable<String> input = Arrays.asList("Aaa", "Abb", "Bcc", "Cdd", "Cee");
|
2015-01-28 14:19:50 +01:00
|
|
|
final Flow<String, Pair<String, Source<String, BoxedUnit>>, BoxedUnit> slsFlow = Flow
|
|
|
|
|
.of(String.class).groupBy(new Function<String, String>() {
|
|
|
|
|
public String apply(String elem) {
|
|
|
|
|
return elem.substring(0, 1);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
Source.from(input).via(slsFlow).runForeach(new Procedure<Pair<String, Source<String, BoxedUnit>>>() {
|
2014-10-27 14:35:41 +01:00
|
|
|
@Override
|
2015-01-28 14:19:50 +01:00
|
|
|
public void apply(final Pair<String, Source<String, BoxedUnit>> pair) throws Exception {
|
2015-01-26 14:57:05 +01:00
|
|
|
pair.second().runForeach(new Procedure<String>() {
|
2014-10-27 14:35:41 +01:00
|
|
|
@Override
|
|
|
|
|
public void apply(String elem) throws Exception {
|
2014-05-13 16:15:36 +02:00
|
|
|
probe.getRef().tell(new Pair<String, String>(pair.first(), elem), ActorRef.noSender());
|
2014-04-23 10:05:09 +02:00
|
|
|
}
|
2014-08-15 15:37:09 +02:00
|
|
|
}, materializer);
|
2014-04-23 10:05:09 +02:00
|
|
|
}
|
2014-08-15 15:37:09 +02:00
|
|
|
}, materializer);
|
2014-04-23 10:05:09 +02:00
|
|
|
|
|
|
|
|
Map<String, List<String>> grouped = new HashMap<String, List<String>>();
|
|
|
|
|
for (Object o : probe.receiveN(5)) {
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
|
Pair<String, String> p = (Pair<String, String>) o;
|
2014-05-13 16:15:36 +02:00
|
|
|
List<String> g = grouped.get(p.first());
|
2014-10-03 17:33:14 +02:00
|
|
|
if (g == null) {
|
2014-04-23 10:05:09 +02:00
|
|
|
g = new ArrayList<String>();
|
2014-10-03 17:33:14 +02:00
|
|
|
}
|
2014-05-13 16:15:36 +02:00
|
|
|
g.add(p.second());
|
|
|
|
|
grouped.put(p.first(), g);
|
2014-04-23 10:05:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assertEquals(Arrays.asList("Aaa", "Abb"), grouped.get("A"));
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseSplitWhen() {
|
|
|
|
|
final JavaTestKit probe = new JavaTestKit(system);
|
2014-11-12 10:16:22 +01:00
|
|
|
final Iterable<String> input = Arrays.asList("A", "B", "C", ".", "D", ".", "E", "F");
|
2015-01-28 14:19:50 +01:00
|
|
|
final Flow<String, Source<String, BoxedUnit>, ?> flow = Flow.of(String.class).splitWhen(new Predicate<String>() {
|
2014-04-23 10:05:09 +02:00
|
|
|
public boolean test(String elem) {
|
2014-11-12 10:16:22 +01:00
|
|
|
return elem.equals(".");
|
2014-04-23 10:05:09 +02:00
|
|
|
}
|
2014-12-03 10:33:21 +01:00
|
|
|
});
|
2015-01-28 14:19:50 +01:00
|
|
|
Source.from(input).via(flow).runForeach(new Procedure<Source<String, BoxedUnit>>() {
|
2014-10-27 14:35:41 +01:00
|
|
|
@Override
|
2015-01-28 14:19:50 +01:00
|
|
|
public void apply(Source<String, BoxedUnit> subStream) throws Exception {
|
2014-10-27 14:35:41 +01:00
|
|
|
subStream.filter(new Predicate<String>() {
|
|
|
|
|
@Override
|
|
|
|
|
public boolean test(String elem) {
|
2014-11-12 10:16:22 +01:00
|
|
|
return !elem.equals(".");
|
2014-10-27 14:35:41 +01:00
|
|
|
}
|
2015-01-26 14:57:05 +01:00
|
|
|
}).grouped(10).runForeach(new Procedure<List<String>>() {
|
2014-10-27 14:35:41 +01:00
|
|
|
@Override
|
|
|
|
|
public void apply(List<String> chunk) throws Exception {
|
|
|
|
|
probe.getRef().tell(chunk, ActorRef.noSender());
|
|
|
|
|
}
|
|
|
|
|
}, materializer);
|
2014-04-23 10:05:09 +02:00
|
|
|
}
|
2014-08-15 15:37:09 +02:00
|
|
|
}, materializer);
|
2014-04-23 10:05:09 +02:00
|
|
|
|
|
|
|
|
for (Object o : probe.receiveN(3)) {
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
|
List<String> chunk = (List<String>) o;
|
2014-10-03 17:33:14 +02:00
|
|
|
if (chunk.get(0).equals("A")) {
|
2014-04-23 10:05:09 +02:00
|
|
|
assertEquals(Arrays.asList("A", "B", "C"), chunk);
|
2014-10-03 17:33:14 +02:00
|
|
|
} else if (chunk.get(0).equals("D")) {
|
2014-04-23 10:05:09 +02:00
|
|
|
assertEquals(Arrays.asList("D"), chunk);
|
2014-10-03 17:33:14 +02:00
|
|
|
} else if (chunk.get(0).equals("E")) {
|
2014-04-23 10:05:09 +02:00
|
|
|
assertEquals(Arrays.asList("E", "F"), chunk);
|
2014-10-03 17:33:14 +02:00
|
|
|
} else {
|
2014-04-23 10:05:09 +02:00
|
|
|
assertEquals("[A, B, C] or [D] or [E, F]", chunk);
|
|
|
|
|
}
|
2014-10-03 17:33:14 +02:00
|
|
|
}
|
2014-04-23 10:05:09 +02:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-20 21:59:33 +01:00
|
|
|
public <T> Creator<Stage<T, T>> op() {
|
|
|
|
|
return new akka.stream.javadsl.japi.Creator<Stage<T, T>>() {
|
|
|
|
|
@Override
|
|
|
|
|
public PushPullStage<T, T> create() throws Exception {
|
|
|
|
|
return new PushPullStage<T, T>() {
|
|
|
|
|
@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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@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 {
|
2015-04-09 15:16:59 +02:00
|
|
|
final Flow<String, String, BoxedUnit> f1 =
|
|
|
|
|
Flow.of(String.class).transform(FlowTest.this.<String> op()).named("f1");
|
|
|
|
|
final Flow<String, String, BoxedUnit> f2 =
|
|
|
|
|
Flow.of(String.class).transform(FlowTest.this.<String> op()).named("f2");
|
|
|
|
|
@SuppressWarnings("unused")
|
|
|
|
|
final Flow<String, String, BoxedUnit> f3 =
|
|
|
|
|
Flow.of(String.class).transform(FlowTest.this.<String> op()).named("f3");
|
2015-01-28 14:19:50 +01:00
|
|
|
|
|
|
|
|
final Source<String, BoxedUnit> in1 = Source.from(Arrays.asList("a", "b", "c"));
|
|
|
|
|
final Source<String, BoxedUnit> in2 = Source.from(Arrays.asList("d", "e", "f"));
|
|
|
|
|
|
|
|
|
|
final Sink<String, Publisher<String>> publisher = Sink.publisher();
|
|
|
|
|
|
2015-03-30 14:22:12 +02:00
|
|
|
final Source<String, BoxedUnit> source = Source.factory().create(new Function<FlowGraph.Builder<BoxedUnit>, Outlet<String>>() {
|
2014-11-20 21:59:33 +01:00
|
|
|
@Override
|
2015-03-30 14:22:12 +02:00
|
|
|
public Outlet<String> apply(Builder<BoxedUnit> b) throws Exception {
|
2015-01-28 14:19:50 +01:00
|
|
|
final UniformFanInShape<String, String> merge = b.graph(Merge.<String> create(2));
|
|
|
|
|
b.flow(b.source(in1), f1, merge.in(0));
|
|
|
|
|
b.flow(b.source(in2), f2, merge.in(1));
|
|
|
|
|
return 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);
|
2014-11-20 21:59:33 +01:00
|
|
|
final Future<List<String>> all = Source.from(pub).grouped(100).runWith(Sink.<List<String>>head(), materializer);
|
|
|
|
|
|
|
|
|
|
final List<String> result = Await.result(all, Duration.apply(200, TimeUnit.MILLISECONDS));
|
|
|
|
|
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);
|
|
|
|
|
|
2015-03-30 14:22:12 +02:00
|
|
|
final Builder<BoxedUnit> b = FlowGraph.<BoxedUnit>builder();
|
2015-01-28 14:19:50 +01:00
|
|
|
final Outlet<String> in1 = b.source(Source.from(input1));
|
|
|
|
|
final Outlet<Integer> in2 = b.source(Source.from(input2));
|
|
|
|
|
final FanInShape2<String, Integer, Pair<String, Integer>> zip = b.graph(Zip.<String, Integer> create());
|
|
|
|
|
final Inlet<Pair<String, Integer>> out = b.sink(Sink
|
2014-11-20 21:59:33 +01:00
|
|
|
.foreach(new Procedure<Pair<String, Integer>>() {
|
|
|
|
|
@Override
|
|
|
|
|
public void apply(Pair<String, Integer> param) throws Exception {
|
|
|
|
|
probe.getRef().tell(param, ActorRef.noSender());
|
|
|
|
|
}
|
2015-01-28 14:19:50 +01:00
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
b.edge(in1, zip.in0());
|
|
|
|
|
b.edge(in2, zip.in1());
|
|
|
|
|
b.edge(zip.out(), out);
|
|
|
|
|
|
|
|
|
|
b.run(materializer);
|
2014-11-20 21:59:33 +01:00
|
|
|
|
|
|
|
|
List<Object> output = Arrays.asList(probe.receiveN(3));
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
|
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");
|
|
|
|
|
|
2015-01-28 14:19:50 +01:00
|
|
|
final Source<String, ?> in1 = Source.from(input1);
|
|
|
|
|
final Source<String, ?> in2 = Source.from(input2);
|
|
|
|
|
final Flow<String, String, ?> 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);
|
|
|
|
|
|
|
|
|
|
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);
|
2015-01-28 14:19:50 +01:00
|
|
|
final Flow<Integer, Pair<List<Integer>, Source<Integer, BoxedUnit>>, ?> flow = Flow.of(Integer.class).prefixAndTail(3);
|
|
|
|
|
Future<Pair<List<Integer>, Source<Integer, BoxedUnit>>> future =
|
|
|
|
|
Source.from(input).via(flow).runWith(Sink.<Pair<List<Integer>, Source<Integer, BoxedUnit>>>head(), materializer);
|
|
|
|
|
Pair<List<Integer>, Source<Integer, BoxedUnit>> result = Await.result(future,
|
2014-10-27 14:35:41 +01:00
|
|
|
probe.dilated(FiniteDuration.create(3, TimeUnit.SECONDS)));
|
2014-05-16 14:21:15 +02:00
|
|
|
assertEquals(Arrays.asList(1, 2, 3), result.first());
|
|
|
|
|
|
2014-11-06 18:13:06 +01:00
|
|
|
Future<List<Integer>> tailFuture = result.second().grouped(4).runWith(Sink.<List<Integer>>head(), materializer);
|
2014-08-15 15:37:09 +02:00
|
|
|
List<Integer> tailResult = Await.result(tailFuture, probe.dilated(FiniteDuration.create(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
|
|
|
|
2015-01-28 14:19:50 +01:00
|
|
|
final List<Source<Integer, BoxedUnit>> mainInputs = new ArrayList<Source<Integer,BoxedUnit>>();
|
|
|
|
|
mainInputs.add(Source.from(input1));
|
|
|
|
|
mainInputs.add(Source.from(input2));
|
2014-05-16 14:21:15 +02:00
|
|
|
|
2015-01-28 14:19:50 +01:00
|
|
|
final Flow<Source<Integer, BoxedUnit>, List<Integer>, BoxedUnit> flow = Flow.<Source<Integer, BoxedUnit>>create().
|
2015-04-02 16:52:30 +02:00
|
|
|
flatten(akka.stream.javadsl.FlattenStrategy.<Integer, BoxedUnit> concat()).grouped(6);
|
2014-12-03 10:33:21 +01:00
|
|
|
Future<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
|
|
|
|
2014-08-15 15:37:09 +02:00
|
|
|
List<Integer> result = Await.result(future, probe.dilated(FiniteDuration.create(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
|
|
|
}
|
|
|
|
|
|
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");
|
2015-01-28 14:19:50 +01:00
|
|
|
final Flow<String, List<String>, BoxedUnit> flow = Flow.of(String.class).buffer(2, OverflowStrategy.backpressure()).grouped(4);
|
2014-12-03 10:33:21 +01:00
|
|
|
Future<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
|
|
|
|
2014-05-20 16:02:09 +02:00
|
|
|
List<String> result = Await.result(future, probe.dilated(FiniteDuration.create(3, TimeUnit.SECONDS)));
|
|
|
|
|
assertEquals(input, result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@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");
|
2015-01-28 14:19:50 +01:00
|
|
|
final Flow<String, String, BoxedUnit> flow = Flow.of(String.class).conflate(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
|
|
|
});
|
2015-01-26 14:57:05 +01:00
|
|
|
Future <String> future = Source.from(input).via(flow).runFold("", new Function2<String, String, String>() {
|
2014-08-15 15:37:09 +02:00
|
|
|
@Override
|
|
|
|
|
public String apply(String aggr, String in) throws Exception {
|
2014-12-03 10:33:21 +01:00
|
|
|
return aggr + in;
|
2014-08-15 15:37:09 +02:00
|
|
|
}
|
2014-10-03 17:33:14 +02:00
|
|
|
}, materializer);
|
2014-08-15 15:37:09 +02:00
|
|
|
String result = Await.result(future, probe.dilated(FiniteDuration.create(3, TimeUnit.SECONDS)));
|
2014-12-03 10:33:21 +01: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");
|
2015-01-28 14:19:50 +01:00
|
|
|
final Flow<String, String, BoxedUnit> flow = Flow.of(String.class).expand(new Function<String, String>() {
|
2014-10-27 14:35:41 +01:00
|
|
|
@Override
|
|
|
|
|
public String apply(String in) throws Exception {
|
|
|
|
|
return in;
|
|
|
|
|
}
|
|
|
|
|
}, new Function<String, Pair<String, String>>() {
|
|
|
|
|
@Override
|
|
|
|
|
public Pair<String, String> apply(String in) throws Exception {
|
|
|
|
|
return new Pair<String, String>(in, in);
|
|
|
|
|
}
|
2014-12-03 10:33:21 +01:00
|
|
|
});
|
2015-01-28 14:19:50 +01:00
|
|
|
final Sink<String, Future<String>> sink = Sink.<String>head();
|
|
|
|
|
Future<String> future = Source.from(input).via(flow).runWith(sink, materializer);
|
2014-08-15 15:37:09 +02:00
|
|
|
String result = Await.result(future, probe.dilated(FiniteDuration.create(3, TimeUnit.SECONDS)));
|
|
|
|
|
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");
|
2015-04-09 22:28:16 +02:00
|
|
|
final Flow<String, String, BoxedUnit> flow = Flow.of(String.class).mapAsync(4, new Function<String, Future<String>>() {
|
2014-05-23 13:52:39 +02:00
|
|
|
public Future<String> apply(String elem) {
|
|
|
|
|
return Futures.successful(elem.toUpperCase());
|
|
|
|
|
}
|
2014-12-03 10:33:21 +01:00
|
|
|
});
|
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");
|
|
|
|
|
}
|
2014-04-23 10:05:09 +02:00
|
|
|
}
|