2014-12-16 11:48:42 +01:00
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2014 Typesafe Inc. <http://www.typesafe.com>
|
|
|
|
|
*/
|
2014-12-03 10:33:21 +01:00
|
|
|
package akka.stream.javadsl;
|
|
|
|
|
|
|
|
|
|
import akka.actor.ActorRef;
|
2015-04-24 14:35:41 +02:00
|
|
|
import akka.japi.Pair;
|
2015-01-28 14:19:50 +01:00
|
|
|
import akka.stream.*;
|
|
|
|
|
import akka.stream.javadsl.FlowGraph.Builder;
|
2015-04-23 20:59:55 +02:00
|
|
|
import akka.stream.stage.*;
|
|
|
|
|
import akka.japi.function.*;
|
2014-12-03 10:33:21 +01:00
|
|
|
import akka.stream.testkit.AkkaSpec;
|
|
|
|
|
import akka.testkit.JavaTestKit;
|
2015-03-30 14:22:12 +02:00
|
|
|
import akka.testkit.TestProbe;
|
2015-04-09 15:16:59 +02:00
|
|
|
|
2014-12-03 10:33:21 +01:00
|
|
|
import org.junit.ClassRule;
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
import org.reactivestreams.Publisher;
|
|
|
|
|
import scala.concurrent.Await;
|
|
|
|
|
import scala.concurrent.Future;
|
2014-12-06 14:51:40 +01:00
|
|
|
import scala.concurrent.duration.Duration;
|
2014-12-03 10:33:21 +01:00
|
|
|
import scala.runtime.BoxedUnit;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
|
|
|
|
|
|
|
public class FlowGraphTest extends StreamTest {
|
|
|
|
|
public FlowGraphTest() {
|
|
|
|
|
super(actorSystemResource);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ClassRule
|
|
|
|
|
public static AkkaJUnitActorSystemResource actorSystemResource = new AkkaJUnitActorSystemResource("FlowGraphTest",
|
|
|
|
|
AkkaSpec.testConf());
|
|
|
|
|
|
2015-04-09 15:16:59 +02:00
|
|
|
@SuppressWarnings("serial")
|
2014-12-03 10:33:21 +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-12-03 10:33:21 +01:00
|
|
|
@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-12-03 10:33:21 +01:00
|
|
|
return ctx.push(element);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2015-04-09 22:28:16 +02:00
|
|
|
public SyncDirective onPull(Context<T> ctx) {
|
2014-12-03 10:33:21 +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(FlowGraphTest.this.<String> op()).named("f1");
|
|
|
|
|
final Flow<String, String, BoxedUnit> f2 =
|
|
|
|
|
Flow.of(String.class).transform(FlowGraphTest.this.<String> op()).named("f2");
|
|
|
|
|
@SuppressWarnings("unused")
|
|
|
|
|
final Flow<String, String, BoxedUnit> f3 =
|
|
|
|
|
Flow.of(String.class).transform(FlowGraphTest.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-12-03 10:33:21 +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-12-03 10:33:21 +01:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// collecting
|
2015-01-28 14:19:50 +01:00
|
|
|
final Publisher<String> pub = source.runWith(publisher, materializer);
|
2014-12-03 10:33:21 +01:00
|
|
|
final Future<List<String>> all = Source.from(pub).grouped(100).runWith(Sink.<List<String>>head(), materializer);
|
|
|
|
|
|
2015-01-28 14:19:50 +01:00
|
|
|
final List<String> result = Await.result(all, Duration.apply(200, TimeUnit.MILLISECONDS));
|
2014-12-03 10:33:21 +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);
|
|
|
|
|
|
2015-03-30 14:22:12 +02:00
|
|
|
final Builder<BoxedUnit> b = FlowGraph.builder();
|
2015-01-28 14:19:50 +01:00
|
|
|
final Source<String, BoxedUnit> in1 = Source.from(input1);
|
|
|
|
|
final Source<Integer, BoxedUnit> in2 = Source.from(input2);
|
2015-03-30 14:22:12 +02:00
|
|
|
final FanInShape2<String, Integer, Pair<String,Integer>> zip = b.graph(Zip.<String, Integer>create());
|
2015-01-28 14:19:50 +01:00
|
|
|
final Sink<Pair<String, Integer>, Future<BoxedUnit>> out = Sink
|
2014-12-03 10:33:21 +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(b.source(in1), zip.in0());
|
|
|
|
|
b.edge(b.source(in2), zip.in1());
|
|
|
|
|
b.edge(zip.out(), b.sink(out));
|
|
|
|
|
b.run(materializer);
|
2014-12-03 10:33:21 +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>(
|
2015-03-30 14:22:12 +02:00
|
|
|
"B", 2), new Pair<String, Integer>("C", 3));
|
2014-12-03 10:33:21 +01:00
|
|
|
assertEquals(expected, output);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseUnzip() {
|
|
|
|
|
final JavaTestKit probe1 = new JavaTestKit(system);
|
|
|
|
|
final JavaTestKit probe2 = new JavaTestKit(system);
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
|
final List<Pair<String, Integer>> input = Arrays.asList(new Pair<String, Integer>("A", 1),
|
2015-01-28 14:19:50 +01:00
|
|
|
new Pair<String, Integer>("B", 2), new Pair<String, Integer>("C", 3));
|
2014-12-03 10:33:21 +01:00
|
|
|
|
|
|
|
|
final Iterable<String> expected1 = Arrays.asList("A", "B", "C");
|
|
|
|
|
final Iterable<Integer> expected2 = Arrays.asList(1, 2, 3);
|
|
|
|
|
|
2015-03-30 14:22:12 +02:00
|
|
|
final Builder<BoxedUnit> b = FlowGraph.builder();
|
2015-01-28 14:19:50 +01:00
|
|
|
final Outlet<Pair<String, Integer>> in = b.source(Source.from(input));
|
2015-03-30 14:22:12 +02:00
|
|
|
final FanOutShape2<Pair<String, Integer>, String, Integer> unzip = b.graph(Unzip.<String, Integer>create());
|
2014-12-03 10:33:21 +01:00
|
|
|
|
2015-01-28 14:19:50 +01:00
|
|
|
final Sink<String, Future<BoxedUnit>> out1 = Sink.foreach(new Procedure<String>() {
|
2014-12-03 10:33:21 +01:00
|
|
|
@Override
|
|
|
|
|
public void apply(String param) throws Exception {
|
|
|
|
|
probe1.getRef().tell(param, ActorRef.noSender());
|
|
|
|
|
}
|
|
|
|
|
});
|
2015-01-28 14:19:50 +01:00
|
|
|
final Sink<Integer, Future<BoxedUnit>> out2 = Sink.foreach(new Procedure<Integer>() {
|
2014-12-03 10:33:21 +01:00
|
|
|
@Override
|
|
|
|
|
public void apply(Integer param) throws Exception {
|
|
|
|
|
probe2.getRef().tell(param, ActorRef.noSender());
|
|
|
|
|
}
|
|
|
|
|
});
|
2015-01-28 14:19:50 +01:00
|
|
|
|
|
|
|
|
b.edge(in, unzip.in());
|
|
|
|
|
b.edge(unzip.out0(), b.sink(out1));
|
|
|
|
|
b.edge(unzip.out1(), b.sink(out2));
|
|
|
|
|
b.run(materializer);
|
2014-12-03 10:33:21 +01:00
|
|
|
|
|
|
|
|
List<Object> output1 = Arrays.asList(probe1.receiveN(3));
|
|
|
|
|
List<Object> output2 = Arrays.asList(probe2.receiveN(3));
|
|
|
|
|
assertEquals(expected1, output1);
|
|
|
|
|
assertEquals(expected2, output2);
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-06 14:51:40 +01:00
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseZipWith() throws Exception {
|
2015-01-28 14:19:50 +01:00
|
|
|
final Source<Integer, BoxedUnit> in1 = Source.single(1);
|
|
|
|
|
final Source<Integer, BoxedUnit> in2 = Source.single(10);
|
2014-12-06 14:51:40 +01:00
|
|
|
|
2015-01-28 14:19:50 +01:00
|
|
|
final Graph<FanInShape2<Integer, Integer, Integer>, BoxedUnit> sumZip = ZipWith.create(
|
2014-12-06 14:51:40 +01:00
|
|
|
new Function2<Integer, Integer, Integer>() {
|
|
|
|
|
@Override public Integer apply(Integer l, Integer r) throws Exception {
|
|
|
|
|
return l + r;
|
|
|
|
|
}
|
|
|
|
|
});
|
2015-01-28 14:19:50 +01:00
|
|
|
|
2015-03-30 14:22:12 +02:00
|
|
|
final Future<Integer> future = FlowGraph.factory().closed(Sink.<Integer> head(), new Procedure2<Builder<Future<Integer> >, SinkShape<Integer>>() {
|
2015-01-28 14:19:50 +01:00
|
|
|
@Override
|
2015-03-30 14:22:12 +02:00
|
|
|
public void apply(Builder<Future<Integer> > b, SinkShape<Integer> out) throws Exception {
|
2015-01-28 14:19:50 +01:00
|
|
|
final FanInShape2<Integer, Integer, Integer> zip = b.graph(sumZip);
|
|
|
|
|
b.edge(b.source(in1), zip.in0());
|
|
|
|
|
b.edge(b.source(in2), zip.in1());
|
|
|
|
|
b.edge(zip.out(), out.inlet());
|
|
|
|
|
}
|
|
|
|
|
}).run(materializer);
|
2014-12-06 14:51:40 +01:00
|
|
|
|
2015-01-28 14:19:50 +01:00
|
|
|
final Integer result = Await.result(future, Duration.create(300, TimeUnit.MILLISECONDS));
|
2014-12-06 14:51:40 +01:00
|
|
|
assertEquals(11, (int) result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
2015-03-30 14:22:12 +02:00
|
|
|
public void mustBeAbleToUseZip4With() throws Exception {
|
2015-01-28 14:19:50 +01:00
|
|
|
final Source<Integer, BoxedUnit> in1 = Source.single(1);
|
|
|
|
|
final Source<Integer, BoxedUnit> in2 = Source.single(10);
|
|
|
|
|
final Source<Integer, BoxedUnit> in3 = Source.single(100);
|
|
|
|
|
final Source<Integer, BoxedUnit> in4 = Source.single(1000);
|
|
|
|
|
|
2015-04-07 15:51:23 +02:00
|
|
|
final Graph<FanInShape4<Integer, Integer, Integer, Integer, Integer>, BoxedUnit> sumZip = ZipWith.create4(
|
2015-03-30 14:22:12 +02:00
|
|
|
new Function4<Integer, Integer, Integer, Integer, Integer>() {
|
|
|
|
|
@Override public Integer apply(Integer i1, Integer i2, Integer i3, Integer i4) throws Exception {
|
|
|
|
|
return i1 + i2 + i3 + i4;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
final Future<Integer> future = FlowGraph.factory().closed(Sink.<Integer> head(), new Procedure2<Builder<Future<Integer>>, SinkShape<Integer>>() {
|
2014-12-06 14:51:40 +01:00
|
|
|
@Override
|
2015-03-30 14:22:12 +02:00
|
|
|
public void apply(Builder<Future<Integer>> b, SinkShape<Integer> out) throws Exception {
|
2015-01-28 14:19:50 +01:00
|
|
|
final FanInShape4<Integer, Integer, Integer, Integer, Integer> zip = b.graph(sumZip);
|
|
|
|
|
b.edge(b.source(in1), zip.in0());
|
|
|
|
|
b.edge(b.source(in2), zip.in1());
|
|
|
|
|
b.edge(b.source(in3), zip.in2());
|
|
|
|
|
b.edge(b.source(in4), zip.in3());
|
|
|
|
|
b.edge(zip.out(), out.inlet());
|
2014-12-06 14:51:40 +01:00
|
|
|
}
|
2015-01-28 14:19:50 +01:00
|
|
|
}).run(materializer);
|
2014-12-06 14:51:40 +01:00
|
|
|
|
2015-01-28 14:19:50 +01:00
|
|
|
final Integer result = Await.result(future, Duration.create(300, TimeUnit.MILLISECONDS));
|
2014-12-06 14:51:40 +01:00
|
|
|
assertEquals(1111, (int) result);
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-30 14:22:12 +02:00
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseMatValue() throws Exception {
|
|
|
|
|
final Source<Integer, BoxedUnit> in1 = Source.single(1);
|
|
|
|
|
final TestProbe probe = TestProbe.apply(system);
|
|
|
|
|
|
|
|
|
|
final Future<Integer> future = FlowGraph.factory().closed(Sink.<Integer> head(), new Procedure2<Builder<Future<Integer>>, SinkShape<Integer>>() {
|
|
|
|
|
@Override
|
|
|
|
|
public void apply(Builder<Future<Integer>> b, SinkShape<Integer> out) throws Exception {
|
|
|
|
|
b.from(Source.single(1)).to(out);
|
|
|
|
|
b.from(b.matValue()).to(Sink.foreach(new Procedure<Future<Integer>>(){
|
|
|
|
|
public void apply(Future<Integer> mat) throws Exception {
|
|
|
|
|
probe.ref().tell(mat, ActorRef.noSender());
|
|
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
}).run(materializer);
|
|
|
|
|
|
|
|
|
|
final Integer result = Await.result(future, Duration.create(300, TimeUnit.MILLISECONDS));
|
|
|
|
|
assertEquals(1, (int) result);
|
|
|
|
|
|
|
|
|
|
final Future<Integer> future2 = probe.expectMsgClass(Future.class);
|
|
|
|
|
|
|
|
|
|
final Integer result2 = Await.result(future2, Duration.create(300, TimeUnit.MILLISECONDS));
|
|
|
|
|
assertEquals(1, (int) result2);
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-03 10:33:21 +01:00
|
|
|
}
|