2015-03-04 15:22:33 +01:00
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2015 Typesafe Inc. <http://www.typesafe.com>
|
|
|
|
|
*/
|
|
|
|
|
package akka.stream.javadsl;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
|
|
import org.junit.ClassRule;
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
|
|
import scala.concurrent.Await;
|
|
|
|
|
import scala.concurrent.Future;
|
|
|
|
|
import scala.concurrent.duration.Duration;
|
|
|
|
|
import scala.concurrent.duration.FiniteDuration;
|
|
|
|
|
import scala.runtime.BoxedUnit;
|
|
|
|
|
import akka.japi.Pair;
|
|
|
|
|
import akka.stream.*;
|
|
|
|
|
import akka.stream.testkit.AkkaSpec;
|
2015-11-30 15:45:37 +01:00
|
|
|
import akka.stream.javadsl.GraphDSL.Builder;
|
2015-04-23 20:59:55 +02:00
|
|
|
import akka.japi.function.*;
|
2015-03-04 15:22:33 +01:00
|
|
|
import akka.util.ByteString;
|
|
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
|
|
import static org.junit.Assert.assertArrayEquals;
|
|
|
|
|
|
|
|
|
|
public class BidiFlowTest extends StreamTest {
|
|
|
|
|
public BidiFlowTest() {
|
|
|
|
|
super(actorSystemResource);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ClassRule
|
|
|
|
|
public static AkkaJUnitActorSystemResource actorSystemResource = new AkkaJUnitActorSystemResource(
|
|
|
|
|
"FlowTest", AkkaSpec.testConf());
|
|
|
|
|
|
|
|
|
|
private final BidiFlow<Integer, Long, ByteString, String, BoxedUnit> bidi = BidiFlow
|
2015-11-30 15:45:37 +01:00
|
|
|
.fromGraph(GraphDSL.create(
|
|
|
|
|
new Function<GraphDSL.Builder<BoxedUnit>, BidiShape<Integer, Long, ByteString, String>>() {
|
2015-10-21 22:45:39 +02:00
|
|
|
@Override
|
|
|
|
|
public BidiShape<Integer, Long, ByteString, String> apply(Builder<BoxedUnit> b)
|
|
|
|
|
throws Exception {
|
|
|
|
|
final FlowShape<Integer, Long> top = b.add(Flow
|
|
|
|
|
.of(Integer.class).map(new Function<Integer, Long>() {
|
|
|
|
|
@Override
|
|
|
|
|
public Long apply(Integer arg) {
|
|
|
|
|
return (long) ((int) arg) + 2;
|
|
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
final FlowShape<ByteString, String> bottom = b.add(Flow
|
|
|
|
|
.of(ByteString.class).map(new Function<ByteString, String>() {
|
|
|
|
|
@Override
|
|
|
|
|
public String apply(ByteString arg) {
|
|
|
|
|
return arg.decodeString("UTF-8");
|
|
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
return new BidiShape<Integer, Long, ByteString, String>(top
|
|
|
|
|
.inlet(), top.outlet(), bottom.inlet(), bottom.outlet());
|
|
|
|
|
}
|
|
|
|
|
}));
|
2015-03-04 15:22:33 +01:00
|
|
|
|
|
|
|
|
private final BidiFlow<Long, Integer, String, ByteString, BoxedUnit> inverse = BidiFlow
|
2015-10-21 22:45:39 +02:00
|
|
|
.fromGraph(
|
2015-11-30 15:45:37 +01:00
|
|
|
GraphDSL.create(
|
|
|
|
|
new Function<GraphDSL.Builder<BoxedUnit>, BidiShape<Long, Integer, String, ByteString>>() {
|
2015-10-21 22:45:39 +02:00
|
|
|
@Override
|
|
|
|
|
public BidiShape<Long, Integer, String, ByteString> apply(Builder<BoxedUnit> b)
|
|
|
|
|
throws Exception {
|
|
|
|
|
final FlowShape<Long, Integer> top = b.add(Flow.of(Long.class)
|
|
|
|
|
.map(new Function<Long, Integer>() {
|
|
|
|
|
@Override
|
|
|
|
|
public Integer apply(Long arg) {
|
|
|
|
|
return (int) ((long) arg) + 2;
|
|
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
final FlowShape<String, ByteString> bottom = b.add(Flow
|
|
|
|
|
.of(String.class).map(new Function<String, ByteString>() {
|
|
|
|
|
@Override
|
|
|
|
|
public ByteString apply(String arg) {
|
|
|
|
|
return ByteString.fromString(arg);
|
|
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
return new BidiShape<Long, Integer, String, ByteString>(top
|
|
|
|
|
.inlet(), top.outlet(), bottom.inlet(), bottom.outlet());
|
|
|
|
|
}
|
|
|
|
|
}));
|
2015-03-04 15:22:33 +01:00
|
|
|
|
2015-10-21 22:45:39 +02:00
|
|
|
private final BidiFlow<Integer, Long, ByteString, String, Future<Integer>> bidiMat =
|
|
|
|
|
BidiFlow.fromGraph(
|
2015-11-30 15:45:37 +01:00
|
|
|
GraphDSL.create(
|
2015-10-21 22:45:39 +02:00
|
|
|
Sink.<Integer>head(),
|
2015-11-30 15:45:37 +01:00
|
|
|
new Function2<GraphDSL.Builder<Future<Integer>>, SinkShape<Integer>, BidiShape<Integer, Long, ByteString, String>>() {
|
2015-10-21 22:45:39 +02:00
|
|
|
@Override
|
|
|
|
|
public BidiShape<Integer, Long, ByteString, String> apply(Builder<Future<Integer>> b, SinkShape<Integer> sink)
|
|
|
|
|
throws Exception {
|
|
|
|
|
b.from(b.add(Source.single(42))).to(sink);
|
|
|
|
|
final FlowShape<Integer, Long> top = b.add(Flow
|
|
|
|
|
.of(Integer.class).map(new Function<Integer, Long>() {
|
|
|
|
|
@Override
|
|
|
|
|
public Long apply(Integer arg) {
|
|
|
|
|
return (long) ((int) arg) + 2;
|
|
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
final FlowShape<ByteString, String> bottom = b.add(Flow
|
|
|
|
|
.of(ByteString.class).map(new Function<ByteString, String>() {
|
|
|
|
|
@Override
|
|
|
|
|
public String apply(ByteString arg) {
|
|
|
|
|
return arg.decodeString("UTF-8");
|
|
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
return new BidiShape<Integer, Long, ByteString, String>(top
|
|
|
|
|
.inlet(), top.outlet(), bottom.inlet(), bottom.outlet());
|
|
|
|
|
}
|
|
|
|
|
}));
|
2015-03-04 15:22:33 +01:00
|
|
|
|
|
|
|
|
private final String str = "Hello World";
|
|
|
|
|
private final ByteString bytes = ByteString.fromString(str);
|
|
|
|
|
private final List<Integer> list = new ArrayList<Integer>();
|
|
|
|
|
{
|
|
|
|
|
list.add(1);
|
|
|
|
|
list.add(2);
|
|
|
|
|
list.add(3);
|
|
|
|
|
}
|
|
|
|
|
private final FiniteDuration oneSec = Duration.create(1, TimeUnit.SECONDS);
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void mustWorkInIsolation() throws Exception {
|
2015-10-21 22:45:39 +02:00
|
|
|
final Pair<Future<Long>, Future<String>> p =
|
2015-11-30 15:45:37 +01:00
|
|
|
RunnableGraph.fromGraph(GraphDSL
|
2015-10-21 22:45:39 +02:00
|
|
|
.create(Sink.<Long> head(), Sink.<String> head(),
|
|
|
|
|
Keep.<Future<Long>, Future<String>> both(),
|
|
|
|
|
new Function3<Builder<Pair<Future<Long>, Future<String>>>, SinkShape<Long>, SinkShape<String>, ClosedShape>() {
|
|
|
|
|
@Override
|
|
|
|
|
public ClosedShape apply(Builder<Pair<Future<Long>, Future<String>>> b, SinkShape<Long> st,
|
|
|
|
|
SinkShape<String> sb) throws Exception {
|
|
|
|
|
final BidiShape<Integer, Long, ByteString, String> s =
|
|
|
|
|
b.add(bidi);
|
|
|
|
|
b.from(b.add(Source.single(1))).toInlet(s.in1());
|
|
|
|
|
b.from(s.out1()).to(st);
|
|
|
|
|
b.from(b.add(Source.single(bytes))).toInlet(s.in2());
|
|
|
|
|
b.from(s.out2()).to(sb);
|
|
|
|
|
return ClosedShape.getInstance();
|
|
|
|
|
}
|
|
|
|
|
})).run(materializer);
|
2015-03-04 15:22:33 +01:00
|
|
|
|
|
|
|
|
final Long rt = Await.result(p.first(), oneSec);
|
|
|
|
|
final String rb = Await.result(p.second(), oneSec);
|
|
|
|
|
|
|
|
|
|
assertEquals((Long) 3L, rt);
|
|
|
|
|
assertEquals(str, rb);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void mustWorkAsAFlowThatIsOpenOnTheLeft() throws Exception {
|
2015-10-21 22:45:39 +02:00
|
|
|
final Flow<Integer, String, BoxedUnit> f = bidi.join(Flow.of(Long.class).map(
|
2015-03-04 15:22:33 +01:00
|
|
|
new Function<Long, ByteString>() {
|
|
|
|
|
@Override public ByteString apply(Long arg) {
|
|
|
|
|
return ByteString.fromString("Hello " + arg);
|
|
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
final Future<List<String>> result = Source.from(list).via(f).grouped(10).runWith(Sink.<List<String>> head(), materializer);
|
|
|
|
|
assertEquals(Arrays.asList("Hello 3", "Hello 4", "Hello 5"), Await.result(result, oneSec));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void mustWorkAsAFlowThatIsOpenOnTheRight() throws Exception {
|
2015-10-21 22:45:39 +02:00
|
|
|
final Flow<ByteString, Long, BoxedUnit> f = Flow.of(String.class).map(
|
2015-03-04 15:22:33 +01:00
|
|
|
new Function<String, Integer>() {
|
|
|
|
|
@Override public Integer apply(String arg) {
|
|
|
|
|
return Integer.valueOf(arg);
|
|
|
|
|
}
|
|
|
|
|
}).join(bidi);
|
|
|
|
|
final List<ByteString> inputs = Arrays.asList(ByteString.fromString("1"), ByteString.fromString("2"));
|
|
|
|
|
final Future<List<Long>> result = Source.from(inputs).via(f).grouped(10).runWith(Sink.<List<Long>> head(), materializer);
|
|
|
|
|
assertEquals(Arrays.asList(3L, 4L), Await.result(result, oneSec));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void mustWorkWhenAtopItsInverse() throws Exception {
|
2015-10-21 22:45:39 +02:00
|
|
|
final Flow<Integer,String,BoxedUnit> f = bidi.atop(inverse).join(Flow.of(Integer.class).map(
|
2015-03-04 15:22:33 +01:00
|
|
|
new Function<Integer, String>() {
|
|
|
|
|
@Override public String apply(Integer arg) {
|
|
|
|
|
return arg.toString();
|
|
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
final Future<List<String>> result = Source.from(list).via(f).grouped(10).runWith(Sink.<List<String>> head(), materializer);
|
|
|
|
|
assertEquals(Arrays.asList("5", "6", "7"), Await.result(result, oneSec));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void mustWorkWhenReversed() throws Exception {
|
2015-10-21 22:45:39 +02:00
|
|
|
final Flow<Integer,String,BoxedUnit> f = Flow.of(Integer.class).map(
|
2015-03-04 15:22:33 +01:00
|
|
|
new Function<Integer, String>() {
|
|
|
|
|
@Override public String apply(Integer arg) {
|
|
|
|
|
return arg.toString();
|
|
|
|
|
}
|
|
|
|
|
}).join(inverse.reversed()).join(bidi.reversed());
|
|
|
|
|
final Future<List<String>> result = Source.from(list).via(f).grouped(10).runWith(Sink.<List<String>> head(), materializer);
|
|
|
|
|
assertEquals(Arrays.asList("5", "6", "7"), Await.result(result, oneSec));
|
|
|
|
|
}
|
2015-03-30 14:22:12 +02:00
|
|
|
|
2015-03-04 15:22:33 +01:00
|
|
|
@Test
|
|
|
|
|
public void mustMaterializeToItsValue() throws Exception {
|
2015-10-21 22:45:39 +02:00
|
|
|
final Future<Integer> f = RunnableGraph.fromGraph(
|
2015-11-30 15:45:37 +01:00
|
|
|
GraphDSL.create(bidiMat,
|
2015-10-21 22:45:39 +02:00
|
|
|
new Function2<Builder<Future<Integer> >, BidiShape<Integer, Long, ByteString, String>, ClosedShape>() {
|
2015-03-04 15:22:33 +01:00
|
|
|
@Override
|
2015-10-21 22:45:39 +02:00
|
|
|
public ClosedShape apply(Builder<Future<Integer>> b,
|
2015-03-04 15:22:33 +01:00
|
|
|
BidiShape<Integer, Long, ByteString, String> shape) throws Exception {
|
2015-10-21 22:45:39 +02:00
|
|
|
final FlowShape<String, Integer> left = b.add(Flow.of(String.class).map(
|
|
|
|
|
new Function<String, Integer>() {
|
|
|
|
|
@Override
|
|
|
|
|
public Integer apply(String arg) {
|
|
|
|
|
return Integer.valueOf(arg);
|
|
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
final FlowShape<Long, ByteString> right = b.add(Flow.of(Long.class).map(
|
|
|
|
|
new Function<Long, ByteString>() {
|
|
|
|
|
@Override
|
|
|
|
|
public ByteString apply(Long arg) {
|
|
|
|
|
return ByteString.fromString("Hello " + arg);
|
|
|
|
|
}
|
|
|
|
|
}));
|
2015-10-08 12:12:35 +02:00
|
|
|
b.from(shape.out2()).via(left).toInlet(shape.in1())
|
|
|
|
|
.from(shape.out1()).via(right).toInlet(shape.in2());
|
2015-10-21 22:45:39 +02:00
|
|
|
return ClosedShape.getInstance();
|
2015-03-04 15:22:33 +01:00
|
|
|
}
|
2015-10-21 22:45:39 +02:00
|
|
|
})).run(materializer);
|
2015-03-04 15:22:33 +01:00
|
|
|
assertEquals((Integer) 42, Await.result(f, oneSec));
|
|
|
|
|
}
|
2015-03-30 14:22:12 +02:00
|
|
|
|
2015-03-04 15:22:33 +01:00
|
|
|
@Test
|
|
|
|
|
public void mustCombineMaterializationValues() throws Exception {
|
2015-11-30 15:45:37 +01:00
|
|
|
final Flow<String, Integer, Future<Integer>> left = Flow.fromGraph(GraphDSL.create(
|
2015-10-21 22:45:39 +02:00
|
|
|
Sink.<Integer>head(), new Function2<Builder<Future<Integer>>, SinkShape<Integer>, FlowShape<String, Integer>>() {
|
|
|
|
|
@Override
|
|
|
|
|
public FlowShape<String, Integer> apply(Builder<Future<Integer>> b,
|
|
|
|
|
SinkShape<Integer> sink) throws Exception {
|
|
|
|
|
final UniformFanOutShape<Integer, Integer> bcast = b.add(Broadcast.<Integer>create(2));
|
|
|
|
|
final UniformFanInShape<Integer, Integer> merge = b.add(Merge.<Integer>create(2));
|
|
|
|
|
final FlowShape<String, Integer> flow = b.add(Flow.of(String.class).map(
|
|
|
|
|
new Function<String, Integer>() {
|
|
|
|
|
@Override
|
|
|
|
|
public Integer apply(String arg) {
|
|
|
|
|
return Integer.valueOf(arg);
|
|
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
b.from(bcast).to(sink)
|
|
|
|
|
.from(b.add(Source.single(1))).viaFanOut(bcast).toFanIn(merge)
|
|
|
|
|
.from(flow).toFanIn(merge);
|
|
|
|
|
return new FlowShape<String, Integer>(flow.inlet(), merge.out());
|
|
|
|
|
}
|
|
|
|
|
}));
|
2015-11-30 15:45:37 +01:00
|
|
|
final Flow<Long, ByteString, Future<List<Long>>> right = Flow.fromGraph(GraphDSL.create(
|
2015-10-21 22:45:39 +02:00
|
|
|
Sink.<List<Long>>head(), new Function2<Builder<Future<List<Long>>>, SinkShape<List<Long>>, FlowShape<Long, ByteString>>() {
|
|
|
|
|
@Override
|
|
|
|
|
public FlowShape<Long, ByteString> apply(Builder<Future<List<Long>>> b,
|
|
|
|
|
SinkShape<List<Long>> sink) throws Exception {
|
|
|
|
|
final FlowShape<Long, List<Long>> flow = b.add(Flow.of(Long.class).grouped(10));
|
|
|
|
|
b.from(flow).to(sink);
|
|
|
|
|
return new FlowShape<Long, ByteString>(flow.inlet(), b.add(Source.single(ByteString.fromString("10"))).outlet());
|
|
|
|
|
}
|
|
|
|
|
}));
|
2015-03-04 15:22:33 +01:00
|
|
|
final Pair<Pair<Future<Integer>, Future<Integer>>, Future<List<Long>>> result =
|
2015-06-23 16:24:06 +03:00
|
|
|
left.joinMat(bidiMat, Keep.<Future<Integer>, Future<Integer>> both()).joinMat(right, Keep.<Pair<Future<Integer>, Future<Integer>>, Future<List<Long>>> both()).run(materializer);
|
2015-03-04 15:22:33 +01:00
|
|
|
final Future<Integer> l = result.first().first();
|
|
|
|
|
final Future<Integer> m = result.first().second();
|
|
|
|
|
final Future<List<Long>> r = result.second();
|
|
|
|
|
assertEquals((Integer) 1, Await.result(l, oneSec));
|
|
|
|
|
assertEquals((Integer) 42, Await.result(m, oneSec));
|
2015-08-01 00:13:14 +02:00
|
|
|
final Long[] rr = Await.result(r, oneSec).toArray(new Long[2]);
|
2015-03-04 15:22:33 +01:00
|
|
|
Arrays.sort(rr);
|
|
|
|
|
assertArrayEquals(new Long[] { 3L, 12L }, rr);
|
|
|
|
|
}
|
|
|
|
|
}
|