!str #18692 javadsl.FlowGraph.Builder.add()

* also make factories more consistent by only offering
  FlowGraph.create()
* also remove secondary (edge-based) FlowGraph.Builder DSL
* also improve naming for conversions from Graph to
  Source/Flow/BidiFlow/Sink
This commit is contained in:
Viktor Klang 2015-10-21 22:45:39 +02:00 committed by Roland Kuhn
parent 0f99a42df9
commit f29d7affbd
120 changed files with 1535 additions and 1897 deletions

View file

@ -35,84 +35,83 @@ public class BidiFlowTest extends StreamTest {
"FlowTest", AkkaSpec.testConf());
private final BidiFlow<Integer, Long, ByteString, String, BoxedUnit> bidi = BidiFlow
.factory()
.create(
new Function<FlowGraph.Builder<BoxedUnit>, BidiShape<Integer, Long, ByteString, String>>() {
@Override
public BidiShape<Integer, Long, ByteString, String> apply(Builder<BoxedUnit> b)
throws Exception {
final FlowShape<Integer, Long> top = b.graph(Flow
.<Integer> empty().map(new Function<Integer, Long>() {
@Override
public Long apply(Integer arg) {
return (long) ((int) arg) + 2;
}
}));
final FlowShape<ByteString, String> bottom = b.graph(Flow
.<ByteString> empty().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());
}
});
.fromGraph(FlowGraph.create(
new Function<FlowGraph.Builder<BoxedUnit>, BidiShape<Integer, Long, ByteString, String>>() {
@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());
}
}));
private final BidiFlow<Long, Integer, String, ByteString, BoxedUnit> inverse = BidiFlow
.factory()
.create(
new Function<FlowGraph.Builder<BoxedUnit>, BidiShape<Long, Integer, String, ByteString>>() {
@Override
public BidiShape<Long, Integer, String, ByteString> apply(Builder<BoxedUnit> b)
throws Exception {
final FlowShape<Long, Integer> top = b.graph(Flow.<Long> empty()
.map(new Function<Long, Integer>() {
@Override
public Integer apply(Long arg) {
return (int) ((long) arg) + 2;
}
}));
final FlowShape<String, ByteString> bottom = b.graph(Flow
.<String> empty().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());
}
});
.fromGraph(
FlowGraph.create(
new Function<FlowGraph.Builder<BoxedUnit>, BidiShape<Long, Integer, String, ByteString>>() {
@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());
}
}));
private final BidiFlow<Integer, Long, ByteString, String, Future<Integer>> bidiMat = BidiFlow
.factory()
.create(
Sink.<Integer> head(),
new Function2<FlowGraph.Builder<Future<Integer>>, SinkShape<Integer>, BidiShape<Integer, Long, ByteString, String>>() {
@Override
public BidiShape<Integer, Long, ByteString, String> apply(Builder<Future<Integer>> b, SinkShape<Integer> sink)
throws Exception {
b.from(b.graph(Source.single(42))).to(sink);
final FlowShape<Integer, Long> top = b.graph(Flow
.<Integer> empty().map(new Function<Integer, Long>() {
@Override
public Long apply(Integer arg) {
return (long) ((int) arg) + 2;
}
}));
final FlowShape<ByteString, String> bottom = b.graph(Flow
.<ByteString> empty().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());
}
});
private final BidiFlow<Integer, Long, ByteString, String, Future<Integer>> bidiMat =
BidiFlow.fromGraph(
FlowGraph.create(
Sink.<Integer>head(),
new Function2<FlowGraph.Builder<Future<Integer>>, SinkShape<Integer>, BidiShape<Integer, Long, ByteString, String>>() {
@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());
}
}));
private final String str = "Hello World";
private final ByteString bytes = ByteString.fromString(str);
@ -126,22 +125,23 @@ public class BidiFlowTest extends StreamTest {
@Test
public void mustWorkInIsolation() throws Exception {
final Pair<Future<Long>, Future<String>> p = FlowGraph
.factory()
.closed(Sink.<Long> head(), Sink.<String> head(),
Keep.<Future<Long>, Future<String>> both(),
new Procedure3<Builder<Pair<Future<Long>, Future<String>>>, SinkShape<Long>, SinkShape<String>>() {
@Override
public void apply(Builder<Pair<Future<Long>, Future<String>>> b, SinkShape<Long> st,
SinkShape<String> sb) throws Exception {
final BidiShape<Integer, Long, ByteString, String> s = b
.graph(bidi);
b.from(b.graph(Source.single(1))).toInlet(s.in1());
b.from(s.out1()).to(st);
b.from(b.graph(Source.single(bytes))).toInlet(s.in2());
b.from(s.out2()).to(sb);
}
}).run(materializer);
final Pair<Future<Long>, Future<String>> p =
RunnableGraph.fromGraph(FlowGraph
.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);
final Long rt = Await.result(p.first(), oneSec);
final String rb = Await.result(p.second(), oneSec);
@ -152,7 +152,7 @@ public class BidiFlowTest extends StreamTest {
@Test
public void mustWorkAsAFlowThatIsOpenOnTheLeft() throws Exception {
final Flow<Integer, String, BoxedUnit> f = bidi.join(Flow.<Long> empty().map(
final Flow<Integer, String, BoxedUnit> f = bidi.join(Flow.of(Long.class).map(
new Function<Long, ByteString>() {
@Override public ByteString apply(Long arg) {
return ByteString.fromString("Hello " + arg);
@ -164,7 +164,7 @@ public class BidiFlowTest extends StreamTest {
@Test
public void mustWorkAsAFlowThatIsOpenOnTheRight() throws Exception {
final Flow<ByteString, Long, BoxedUnit> f = Flow.<String> empty().map(
final Flow<ByteString, Long, BoxedUnit> f = Flow.of(String.class).map(
new Function<String, Integer>() {
@Override public Integer apply(String arg) {
return Integer.valueOf(arg);
@ -177,7 +177,7 @@ public class BidiFlowTest extends StreamTest {
@Test
public void mustWorkWhenAtopItsInverse() throws Exception {
final Flow<Integer,String,BoxedUnit> f = bidi.atop(inverse).join(Flow.<Integer> empty().map(
final Flow<Integer,String,BoxedUnit> f = bidi.atop(inverse).join(Flow.of(Integer.class).map(
new Function<Integer, String>() {
@Override public String apply(Integer arg) {
return arg.toString();
@ -189,7 +189,7 @@ public class BidiFlowTest extends StreamTest {
@Test
public void mustWorkWhenReversed() throws Exception {
final Flow<Integer,String,BoxedUnit> f = Flow.<Integer> empty().map(
final Flow<Integer,String,BoxedUnit> f = Flow.of(Integer.class).map(
new Function<Integer, String>() {
@Override public String apply(Integer arg) {
return arg.toString();
@ -201,61 +201,66 @@ public class BidiFlowTest extends StreamTest {
@Test
public void mustMaterializeToItsValue() throws Exception {
final Future<Integer> f = FlowGraph.factory().closed(bidiMat, new Procedure2<Builder<Future<Integer> >, BidiShape<Integer, Long, ByteString, String>>() {
final Future<Integer> f = RunnableGraph.fromGraph(
FlowGraph.create(bidiMat,
new Function2<Builder<Future<Integer> >, BidiShape<Integer, Long, ByteString, String>, ClosedShape>() {
@Override
public void apply(Builder<Future<Integer>> b,
public ClosedShape apply(Builder<Future<Integer>> b,
BidiShape<Integer, Long, ByteString, String> shape) throws Exception {
final FlowShape<String, Integer> left = b.graph(Flow.<String> empty().map(
new Function<String, Integer>() {
@Override public Integer apply(String arg) {
return Integer.valueOf(arg);
}
}));
final FlowShape<Long, ByteString> right = b.graph(Flow.<Long> empty().map(
new Function<Long, ByteString>() {
@Override public ByteString apply(Long arg) {
return ByteString.fromString("Hello " + arg);
}
}));
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);
}
}));
b.from(shape.out2()).via(left).toInlet(shape.in1())
.from(shape.out1()).via(right).toInlet(shape.in2());
return ClosedShape.getInstance();
}
}).run(materializer);
})).run(materializer);
assertEquals((Integer) 42, Await.result(f, oneSec));
}
@Test
public void mustCombineMaterializationValues() throws Exception {
final Flow<String, Integer, Future<Integer>> left = Flow.factory().create(
Sink.<Integer> head(), new Function2<Builder<Future<Integer> >, SinkShape<Integer>, Pair<Inlet<String>, Outlet<Integer>>>() {
@Override
public Pair<Inlet<String>, Outlet<Integer>> apply(Builder<Future<Integer>> b,
SinkShape<Integer> sink) throws Exception {
final UniformFanOutShape<Integer, Integer> bcast = b.graph(Broadcast.<Integer> create(2));
final UniformFanInShape<Integer, Integer> merge = b.graph(Merge.<Integer> create(2));
final FlowShape<String, Integer> flow = b.graph(Flow.<String> empty().map(
new Function<String, Integer>() {
@Override
public Integer apply(String arg) {
return Integer.valueOf(arg);
}
}));
b.from(bcast).to(sink)
.from(b.graph(Source.single(1))).viaFanOut(bcast).toFanIn(merge)
.from(flow).toFanIn(merge);
return new Pair<Inlet<String>, Outlet<Integer>>(flow.inlet(), merge.out());
}
});
final Flow<Long, ByteString, Future<List<Long>>> right = Flow.factory().create(
Sink.<List<Long>> head(), new Function2<Builder<Future<List<Long>>>, SinkShape<List<Long>>, Pair<Inlet<Long>, Outlet<ByteString>>>() {
@Override
public Pair<Inlet<Long>, Outlet<ByteString>> apply(Builder<Future<List<Long>>> b,
SinkShape<List<Long>> sink) throws Exception {
final FlowShape<Long, List<Long>> flow = b.graph(Flow.<Long> empty().grouped(10));
b.from(flow).to(sink);
return new Pair<Inlet<Long>, Outlet<ByteString>>(flow.inlet(), b.source(Source.single(ByteString.fromString("10"))));
}
});
final Flow<String, Integer, Future<Integer>> left = Flow.fromGraph(FlowGraph.create(
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());
}
}));
final Flow<Long, ByteString, Future<List<Long>>> right = Flow.fromGraph(FlowGraph.create(
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());
}
}));
final Pair<Pair<Future<Integer>, Future<Integer>>, Future<List<Long>>> result =
left.joinMat(bidiMat, Keep.<Future<Integer>, Future<Integer>> both()).joinMat(right, Keep.<Pair<Future<Integer>, Future<Integer>>, Future<List<Long>>> both()).run(materializer);
final Future<Integer> l = result.first().first();

View file

@ -69,15 +69,16 @@ public class FlowGraphTest extends StreamTest {
final Sink<String, Publisher<String>> publisher = Sink.publisher();
final Source<String, BoxedUnit> source = Source.factory().create(new Function<FlowGraph.Builder<BoxedUnit>, Outlet<String>>() {
@Override
public Outlet<String> apply(Builder<BoxedUnit> b) throws Exception {
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();
}
});
final Source<String, BoxedUnit> source = Source.fromGraph(
FlowGraph.create(new Function<FlowGraph.Builder<BoxedUnit>, SourceShape<String>>() {
@Override
public SourceShape<String> apply(Builder<BoxedUnit> b) throws Exception {
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());
}
}));
// collecting
final Publisher<String> pub = source.runWith(publisher, materializer);
@ -93,16 +94,21 @@ public class FlowGraphTest extends StreamTest {
final Iterable<String> input1 = Arrays.asList("A", "B", "C");
final Iterable<Integer> input2 = Arrays.asList(1, 2, 3);
final Builder<BoxedUnit> b = FlowGraph.builder();
final Source<String, BoxedUnit> in1 = Source.from(input1);
final Source<Integer, BoxedUnit> in2 = Source.from(input2);
final FanInShape2<String, Integer, Pair<String,Integer>> zip = b.graph(Zip.<String, Integer>create());
final Sink<Pair<String, Integer>, BoxedUnit> out = createSink(probe);
RunnableGraph.fromGraph( FlowGraph.create(
new Function<Builder<BoxedUnit>,ClosedShape>() {
@Override
public ClosedShape apply(final Builder<BoxedUnit> b) throws Exception {
final Source<String, BoxedUnit> in1 = Source.from(input1);
final Source<Integer, BoxedUnit> in2 = Source.from(input2);
final FanInShape2<String, Integer, Pair<String,Integer>> zip = b.add(Zip.<String, Integer>create());
final Sink<Pair<String, Integer>, BoxedUnit> out = createSink(probe);
b.edge(b.source(in1), zip.in0());
b.edge(b.source(in2), zip.in1());
b.edge(zip.out(), b.sink(out));
b.run(materializer);
b.from(b.add(in1)).toInlet(zip.in0());
b.from(b.add(in2)).toInlet(zip.in1());
b.from(zip.out()).to(b.add(out));
return ClosedShape.getInstance();
}
})).run(materializer);
List<Object> output = Arrays.asList(probe.receiveN(3));
@SuppressWarnings("unchecked")
@ -123,17 +129,22 @@ public class FlowGraphTest extends StreamTest {
final Iterable<String> expected1 = Arrays.asList("A", "B", "C");
final Iterable<Integer> expected2 = Arrays.asList(1, 2, 3);
final Builder<BoxedUnit> b = FlowGraph.builder();
final Outlet<Pair<String, Integer>> in = b.source(Source.from(input));
final FanOutShape2<Pair<String, Integer>, String, Integer> unzip = b.graph(Unzip.<String, Integer>create());
RunnableGraph.fromGraph(FlowGraph.create(
new Function<Builder<BoxedUnit>, ClosedShape>() {
@Override
public ClosedShape apply(final Builder<BoxedUnit> b) throws Exception {
final SourceShape<Pair<String, Integer>> in = b.add(Source.from(input));
final FanOutShape2<Pair<String, Integer>, String, Integer> unzip = b.add(Unzip.<String, Integer>create());
final Sink<String, BoxedUnit> out1 = createSink(probe1);
final Sink<Integer, BoxedUnit> out2 = createSink(probe2);
final SinkShape<String> out1 = b.add(FlowGraphTest.<String>createSink(probe1));
final SinkShape<Integer> out2 = b.add(FlowGraphTest.<Integer>createSink(probe2));
b.edge(in, unzip.in());
b.edge(unzip.out0(), b.sink(out1));
b.edge(unzip.out1(), b.sink(out2));
b.run(materializer);
b.from(in).toInlet(unzip.in());
b.from(unzip.out0()).to(out1);
b.from(unzip.out1()).to(out2);
return ClosedShape.getInstance();
}
})).run(materializer);
List<Object> output1 = Arrays.asList(probe1.receiveN(3));
List<Object> output2 = Arrays.asList(probe2.receiveN(3));
@ -150,24 +161,31 @@ public class FlowGraphTest extends StreamTest {
final JavaTestKit probe1 = new JavaTestKit(system);
final JavaTestKit probe2 = new JavaTestKit(system);
final Builder<BoxedUnit> b = FlowGraph.builder();
final Source<Integer, BoxedUnit> in = Source.single(1);
RunnableGraph.fromGraph(FlowGraph.create(
new Function<Builder<BoxedUnit>, ClosedShape>() {
@Override
public ClosedShape apply(final Builder<BoxedUnit> b) throws Exception {
final Source<Integer, BoxedUnit> in = Source.single(1);
final FanOutShape2<Integer, String, Integer> unzip = b.graph(UnzipWith.create(
new Function<Integer, Pair<String, Integer>>() {
@Override public Pair<String, Integer> apply(Integer l) throws Exception {
return new Pair<String, Integer>(l + "!", l);
final FanOutShape2<Integer, String, Integer> unzip = b.add(UnzipWith.create(
new Function<Integer, Pair<String, Integer>>() {
@Override
public Pair<String, Integer> apply(Integer l) throws Exception {
return new Pair<String, Integer>(l + "!", l);
}
})
);
final SinkShape<String> out1 = b.add(FlowGraphTest.<String>createSink(probe1));
final SinkShape<Integer> out2 = b.add(FlowGraphTest.<Integer>createSink(probe2));
b.from(b.add(in)).toInlet(unzip.in());
b.from(unzip.out0()).to(out1);
b.from(unzip.out1()).to(out2);
return ClosedShape.getInstance();
}
})
);
final Sink<String, BoxedUnit> out1 = createSink(probe1);
final Sink<Integer, BoxedUnit> out2 = createSink(probe2);
b.edge(b.source(in), unzip.in());
b.edge(unzip.out0(), b.sink(out1));
b.edge(unzip.out1(), b.sink(out2));
b.run(materializer);
}
)).run(materializer);
Duration d = Duration.create(300, TimeUnit.MILLISECONDS);
@ -186,28 +204,34 @@ public class FlowGraphTest extends StreamTest {
final JavaTestKit probe3 = new JavaTestKit(system);
final JavaTestKit probe4 = new JavaTestKit(system);
final Builder<BoxedUnit> b = FlowGraph.builder();
final Source<Integer, BoxedUnit> in = Source.single(1);
RunnableGraph.fromGraph(FlowGraph.create(
new Function<Builder<BoxedUnit>, ClosedShape>() {
@Override
public ClosedShape apply(final Builder<BoxedUnit> b) throws Exception {
final Source<Integer, BoxedUnit> in = Source.single(1);
final FanOutShape4<Integer, String, Integer, String, Integer> unzip = b.graph(UnzipWith.create4(
new Function<Integer, Tuple4<String, Integer, String, Integer>>() {
@Override public Tuple4<String, Integer, String, Integer> apply(Integer l) throws Exception {
return new Tuple4<String, Integer, String, Integer>(l.toString(), l, l + "+" + l, l + l);
final FanOutShape4<Integer, String, Integer, String, Integer> unzip = b.add(UnzipWith.create4(
new Function<Integer, Tuple4<String, Integer, String, Integer>>() {
@Override
public Tuple4<String, Integer, String, Integer> apply(Integer l) throws Exception {
return new Tuple4<String, Integer, String, Integer>(l.toString(), l, l + "+" + l, l + l);
}
})
);
final SinkShape<String> out1 = b.add(FlowGraphTest.<String>createSink(probe1));
final SinkShape<Integer> out2 = b.add(FlowGraphTest.<Integer>createSink(probe2));
final SinkShape<String> out3 = b.add(FlowGraphTest.<String>createSink(probe3));
final SinkShape<Integer> out4 = b.add(FlowGraphTest.<Integer>createSink(probe4));
b.from(b.add(in)).toInlet(unzip.in());
b.from(unzip.out0()).to(out1);
b.from(unzip.out1()).to(out2);
b.from(unzip.out2()).to(out3);
b.from(unzip.out3()).to(out4);
return ClosedShape.getInstance();
}
})
);
final Sink<String, BoxedUnit> out1 = createSink(probe1);
final Sink<Integer, BoxedUnit> out2 = createSink(probe2);
final Sink<String, BoxedUnit> out3 = createSink(probe3);
final Sink<Integer, BoxedUnit> out4 = createSink(probe4);
b.edge(b.source(in), unzip.in());
b.edge(unzip.out0(), b.sink(out1));
b.edge(unzip.out1(), b.sink(out2));
b.edge(unzip.out2(), b.sink(out3));
b.edge(unzip.out3(), b.sink(out4));
b.run(materializer);
})).run(materializer);
Duration d = Duration.create(300, TimeUnit.MILLISECONDS);
@ -234,15 +258,17 @@ public class FlowGraphTest extends StreamTest {
}
});
final Future<Integer> future = FlowGraph.factory().closed(Sink.<Integer> head(), new Procedure2<Builder<Future<Integer> >, SinkShape<Integer>>() {
final Future<Integer> future = RunnableGraph.fromGraph(FlowGraph.create(Sink.<Integer>head(),
new Function2<Builder<Future<Integer>>, SinkShape<Integer>, ClosedShape>() {
@Override
public void apply(Builder<Future<Integer> > b, SinkShape<Integer> out) throws Exception {
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());
public ClosedShape apply(Builder<Future<Integer>> b, SinkShape<Integer> out) throws Exception {
final FanInShape2<Integer, Integer, Integer> zip = b.add(sumZip);
b.from(b.add(in1)).toInlet(zip.in0());
b.from(b.add(in2)).toInlet(zip.in1());
b.from(zip.out()).to(out);
return ClosedShape.getInstance();
}
}).run(materializer);
})).run(materializer);
final Integer result = Await.result(future, Duration.create(300, TimeUnit.MILLISECONDS));
assertEquals(11, (int) result);
@ -262,17 +288,20 @@ public class FlowGraphTest extends StreamTest {
}
});
final Future<Integer> future = FlowGraph.factory().closed(Sink.<Integer> head(), new Procedure2<Builder<Future<Integer>>, SinkShape<Integer>>() {
final Future<Integer> future = RunnableGraph.fromGraph(
FlowGraph.create(Sink.<Integer>head(),
new Function2<Builder<Future<Integer>>, SinkShape<Integer>, ClosedShape>() {
@Override
public void apply(Builder<Future<Integer>> b, SinkShape<Integer> out) throws Exception {
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());
public ClosedShape apply(Builder<Future<Integer>> b, SinkShape<Integer> out) throws Exception {
final FanInShape4<Integer, Integer, Integer, Integer, Integer> zip = b.add(sumZip);
b.from(b.add(in1)).toInlet(zip.in0());
b.from(b.add(in2)).toInlet(zip.in1());
b.from(b.add(in3)).toInlet(zip.in2());
b.from(b.add(in4)).toInlet(zip.in3());
b.from(zip.out()).to(out);
return ClosedShape.getInstance();
}
}).run(materializer);
})).run(materializer);
final Integer result = Await.result(future, Duration.create(300, TimeUnit.MILLISECONDS));
assertEquals(1111, (int) result);
@ -284,17 +313,19 @@ public class FlowGraphTest extends StreamTest {
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>>() {
final Future<Integer> future = RunnableGraph.fromGraph(
FlowGraph.create(Sink.<Integer> head(), new Function2<Builder<Future<Integer>>, SinkShape<Integer>, ClosedShape>() {
@Override
public void apply(Builder<Future<Integer>> b, SinkShape<Integer> out) throws Exception {
b.from(b.graph(Source.single(1))).to(out);
b.from(b.materializedValue()).to(b.graph(Sink.foreach(new Procedure<Future<Integer>>(){
public ClosedShape apply(Builder<Future<Integer>> b, SinkShape<Integer> out) throws Exception {
b.from(b.add(Source.single(1))).to(out);
b.from(b.materializedValue()).to(b.add(Sink.foreach(new Procedure<Future<Integer>>(){
public void apply(Future<Integer> mat) throws Exception {
Patterns.pipe(mat, system.dispatcher()).to(probe.ref());
}
})));
return ClosedShape.getInstance();
}
}).run(materializer);
})).run(materializer);
final Integer result = Await.result(future, Duration.create(300, TimeUnit.MILLISECONDS));
assertEquals(1, (int) result);

View file

@ -385,15 +385,16 @@ public class FlowTest extends StreamTest {
final Sink<String, Publisher<String>> publisher = Sink.publisher();
final Source<String, BoxedUnit> source = Source.factory().create(new Function<FlowGraph.Builder<BoxedUnit>, Outlet<String>>() {
@Override
public Outlet<String> apply(Builder<BoxedUnit> b) throws Exception {
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();
}
});
final Source<String, BoxedUnit> source = Source.fromGraph(
FlowGraph.create(new Function<FlowGraph.Builder<BoxedUnit>, SourceShape<String>>() {
@Override
public SourceShape<String> apply(Builder<BoxedUnit> b) throws Exception {
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());
}
}));
// collecting
final Publisher<String> pub = source.runWith(publisher, materializer);
@ -409,23 +410,25 @@ public class FlowTest extends StreamTest {
final Iterable<String> input1 = Arrays.asList("A", "B", "C");
final Iterable<Integer> input2 = Arrays.asList(1, 2, 3);
final Builder<BoxedUnit> b = FlowGraph.<BoxedUnit>builder();
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
.foreach(new Procedure<Pair<String, Integer>>() {
@Override
public void apply(Pair<String, Integer> param) throws Exception {
probe.getRef().tell(param, ActorRef.noSender());
}
}));
b.edge(in1, zip.in0());
b.edge(in2, zip.in1());
b.edge(zip.out(), out);
b.run(materializer);
RunnableGraph.fromGraph(FlowGraph.create(new Function<Builder<BoxedUnit>, ClosedShape>(){
public ClosedShape apply(Builder<BoxedUnit> b) {
final Outlet<String> in1 = b.add(Source.from(input1)).outlet();
final Outlet<Integer> in2 = b.add(Source.from(input2)).outlet();
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);
List<Object> output = Arrays.asList(probe.receiveN(3));
@SuppressWarnings("unchecked")
@ -638,19 +641,18 @@ public class FlowTest extends StreamTest {
@Test
public void mustBeAbleToBroadcastEagerCancel() throws Exception {
final Sink<String, BoxedUnit> out1 = Sink.cancelled();
final Sink<String, ?> out2 = Sink.ignore();
final Sink<String, BoxedUnit> sink = Sink.factory().<String>create(new Function<FlowGraph.Builder<BoxedUnit>, Inlet<String>>() {
@Override
public Inlet<String> apply(Builder<BoxedUnit> b) throws Exception {
final UniformFanOutShape<String, String> broadcast = b.graph(Broadcast.<String>create(2, true));
b.from(broadcast.out(0)).to(b.graph(out1));
b.from(broadcast.out(1)).to(b.graph(out2));
return broadcast.in();
}
});
final Sink<String, BoxedUnit> sink = Sink.fromGraph(
FlowGraph.create(new Function<FlowGraph.Builder<BoxedUnit>, SinkShape<String>>() {
@Override
public SinkShape<String> apply(Builder<BoxedUnit> b) throws Exception {
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());
}
}));
final JavaTestKit probe = new JavaTestKit(system);
Source<String, ActorRef> source = Source.actorRef(1, OverflowStrategy.dropNew());

View file

@ -35,7 +35,7 @@ public class TcpTest extends StreamTest {
final Sink<IncomingConnection, Future<BoxedUnit>> echoHandler =
Sink.foreach(new Procedure<IncomingConnection>() {
public void apply(IncomingConnection conn) {
conn.handleWith(Flow.<ByteString>empty(), materializer);
conn.handleWith(Flow.of(ByteString.class), materializer);
}
});