!str #16902: Unify stream internal representation
also =str #16912: Fix StreamTcpSpec flakiness
This commit is contained in:
parent
cac9c9f2fb
commit
8d77fa8b29
230 changed files with 7814 additions and 9596 deletions
|
|
@ -5,7 +5,8 @@ package akka.stream.javadsl;
|
|||
|
||||
import akka.actor.ActorRef;
|
||||
import akka.japi.*;
|
||||
import akka.stream.StreamTest;
|
||||
import akka.stream.*;
|
||||
import akka.stream.javadsl.FlowGraph.Builder;
|
||||
import akka.stream.javadsl.japi.Creator;
|
||||
import akka.stream.javadsl.japi.Function;
|
||||
import akka.stream.javadsl.japi.Function2;
|
||||
|
|
@ -18,6 +19,7 @@ import akka.testkit.JavaTestKit;
|
|||
import org.junit.ClassRule;
|
||||
import org.junit.Test;
|
||||
import org.reactivestreams.Publisher;
|
||||
|
||||
import scala.concurrent.Await;
|
||||
import scala.concurrent.Future;
|
||||
import scala.concurrent.duration.Duration;
|
||||
|
|
@ -59,40 +61,60 @@ public class FlowGraphTest extends StreamTest {
|
|||
|
||||
@Test
|
||||
public void mustBeAbleToUseMerge() throws Exception {
|
||||
final JavaTestKit probe = new JavaTestKit(system);
|
||||
final Flow<String, String> f1 = Flow.of(String.class).section(OperationAttributes.name("f1"), new Function<Flow<String, String>, Flow<String, String>>() {
|
||||
final Flow<String, String, BoxedUnit> f1 = Flow
|
||||
.of(String.class)
|
||||
.section(
|
||||
OperationAttributes.name("f1"),
|
||||
new Function<Flow<String, String, BoxedUnit>, Flow<String, String, BoxedUnit>>() {
|
||||
@Override
|
||||
public Flow<String, String, BoxedUnit> apply(
|
||||
Flow<String, String, BoxedUnit> flow) {
|
||||
return flow.transform(FlowGraphTest.this.<String> op());
|
||||
}
|
||||
});
|
||||
final Flow<String, String, BoxedUnit> f2 = Flow
|
||||
.of(String.class)
|
||||
.section(
|
||||
OperationAttributes.name("f2"),
|
||||
new Function<Flow<String, String, BoxedUnit>, Flow<String, String, BoxedUnit>>() {
|
||||
@Override
|
||||
public Flow<String, String, BoxedUnit> apply(
|
||||
Flow<String, String, BoxedUnit> flow) {
|
||||
return flow.transform(FlowGraphTest.this.<String> op());
|
||||
}
|
||||
});
|
||||
final Flow<String, String, BoxedUnit> f3 = Flow
|
||||
.of(String.class)
|
||||
.section(
|
||||
OperationAttributes.name("f3"),
|
||||
new Function<Flow<String, String, BoxedUnit>, Flow<String, String, BoxedUnit>>() {
|
||||
@Override
|
||||
public Flow<String, String, BoxedUnit> apply(
|
||||
Flow<String, String, BoxedUnit> flow) {
|
||||
return flow.transform(FlowGraphTest.this.<String> op());
|
||||
}
|
||||
});
|
||||
|
||||
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();
|
||||
|
||||
final Source<String, BoxedUnit> source = Source.factory().create(new Function<FlowGraph.Builder, Outlet<String>>() {
|
||||
@Override
|
||||
public Flow<String, String> apply(Flow<String, String> flow) {
|
||||
return flow.transform(FlowGraphTest.this.<String>op());
|
||||
public Outlet<String> apply(Builder 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 Flow<String, String> f2 = Flow.of(String.class).section(OperationAttributes.name("f2"), new Function<Flow<String, String>, Flow<String, String>>() {
|
||||
@Override
|
||||
public Flow<String, String> apply(Flow<String, String> flow) {
|
||||
return flow.transform(FlowGraphTest.this.<String>op());
|
||||
}
|
||||
});
|
||||
final Flow<String, String> f3 = Flow.of(String.class).section(OperationAttributes.name("f3"), new Function<Flow<String, String>, Flow<String, String>>() {
|
||||
@Override
|
||||
public Flow<String, String> apply(Flow<String, String> flow) {
|
||||
return flow.transform(FlowGraphTest.this.<String>op());
|
||||
}
|
||||
});
|
||||
|
||||
final Source<String> in1 = Source.from(Arrays.asList("a", "b", "c"));
|
||||
final Source<String> in2 = Source.from(Arrays.asList("d", "e", "f"));
|
||||
|
||||
final KeyedSink<String, Publisher<String>> publisher = Sink.publisher();
|
||||
|
||||
final Merge<String> merge = Merge.<String>create();
|
||||
MaterializedMap m = FlowGraph.builder().addEdge(in1, f1, merge).addEdge(in2, f2, merge)
|
||||
.addEdge(merge, f3, publisher).build().run(materializer);
|
||||
|
||||
// collecting
|
||||
final Publisher<String> pub = m.get(publisher);
|
||||
final Publisher<String> pub = source.runWith(publisher, materializer);
|
||||
final Future<List<String>> all = Source.from(pub).grouped(100).runWith(Sink.<List<String>>head(), materializer);
|
||||
|
||||
final List<String> result = Await.result(all, probe.dilated(FiniteDuration.create(3, TimeUnit.SECONDS)));
|
||||
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));
|
||||
}
|
||||
|
||||
|
|
@ -102,10 +124,11 @@ public class FlowGraphTest extends StreamTest {
|
|||
final Iterable<String> input1 = Arrays.asList("A", "B", "C");
|
||||
final Iterable<Integer> input2 = Arrays.asList(1, 2, 3);
|
||||
|
||||
final Source<String> in1 = Source.from(input1);
|
||||
final Source<Integer> in2 = Source.from(input2);
|
||||
final Zip2With<String, Integer, Pair<String,Integer>> zip = Zip.create();
|
||||
final KeyedSink<Pair<String, Integer>, Future<BoxedUnit>> out = Sink
|
||||
final Builder 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>, Future<BoxedUnit>> out = Sink
|
||||
.foreach(new Procedure<Pair<String, Integer>>() {
|
||||
@Override
|
||||
public void apply(Pair<String, Integer> param) throws Exception {
|
||||
|
|
@ -113,7 +136,10 @@ public class FlowGraphTest extends StreamTest {
|
|||
}
|
||||
});
|
||||
|
||||
FlowGraph.builder().addEdge(in1, zip.left()).addEdge(in2, zip.right()).addEdge(zip.out(), out).run(materializer);
|
||||
b.edge(b.source(in1), zip.in0());
|
||||
b.edge(b.source(in2), zip.in1());
|
||||
b.edge(zip.out(), b.sink(out));
|
||||
b.run(materializer);
|
||||
|
||||
List<Object> output = Arrays.asList(probe.receiveN(3));
|
||||
@SuppressWarnings("unchecked")
|
||||
|
|
@ -129,29 +155,32 @@ public class FlowGraphTest extends StreamTest {
|
|||
|
||||
@SuppressWarnings("unchecked")
|
||||
final List<Pair<String, Integer>> input = Arrays.asList(new Pair<String, Integer>("A", 1),
|
||||
new Pair<String, Integer>("B", 2), new Pair<String, Integer>("C", 3));
|
||||
new Pair<String, Integer>("B", 2), new Pair<String, Integer>("C", 3));
|
||||
|
||||
final Iterable<String> expected1 = Arrays.asList("A", "B", "C");
|
||||
final Iterable<Integer> expected2 = Arrays.asList(1, 2, 3);
|
||||
|
||||
final Source<Pair<String, Integer>> in = Source.from(input);
|
||||
final Unzip<String, Integer> unzip = Unzip.create();
|
||||
final Builder 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());
|
||||
|
||||
final KeyedSink<String, Future<BoxedUnit>> out1 = Sink.foreach(new Procedure<String>() {
|
||||
final Sink<String, Future<BoxedUnit>> out1 = Sink.foreach(new Procedure<String>() {
|
||||
@Override
|
||||
public void apply(String param) throws Exception {
|
||||
probe1.getRef().tell(param, ActorRef.noSender());
|
||||
}
|
||||
});
|
||||
final KeyedSink<Integer, Future<BoxedUnit>> out2 = Sink.foreach(new Procedure<Integer>() {
|
||||
final Sink<Integer, Future<BoxedUnit>> out2 = Sink.foreach(new Procedure<Integer>() {
|
||||
@Override
|
||||
public void apply(Integer param) throws Exception {
|
||||
probe2.getRef().tell(param, ActorRef.noSender());
|
||||
}
|
||||
});
|
||||
|
||||
FlowGraph.builder().addEdge(in, unzip.in()).addEdge(unzip.left(), out1).addEdge(unzip.right(), out2)
|
||||
.run(materializer);
|
||||
|
||||
b.edge(in, unzip.in());
|
||||
b.edge(unzip.out0(), b.sink(out1));
|
||||
b.edge(unzip.out1(), b.sink(out2));
|
||||
b.run(materializer);
|
||||
|
||||
List<Object> output1 = Arrays.asList(probe1.receiveN(3));
|
||||
List<Object> output2 = Arrays.asList(probe2.receiveN(3));
|
||||
|
|
@ -161,56 +190,57 @@ public class FlowGraphTest extends StreamTest {
|
|||
|
||||
@Test
|
||||
public void mustBeAbleToUseZipWith() throws Exception {
|
||||
final Source<Integer> in1 = Source.single(1);
|
||||
final Source<Integer> in2 = Source.single(10);
|
||||
|
||||
final Zip2With<Integer, Integer, Integer> sumZip = ZipWith.create(
|
||||
final Source<Integer, BoxedUnit> in1 = Source.single(1);
|
||||
final Source<Integer, BoxedUnit> in2 = Source.single(10);
|
||||
|
||||
final Graph<FanInShape2<Integer, Integer, Integer>, BoxedUnit> sumZip = ZipWith.create(
|
||||
new Function2<Integer, Integer, Integer>() {
|
||||
@Override public Integer apply(Integer l, Integer r) throws Exception {
|
||||
return l + r;
|
||||
}
|
||||
});
|
||||
|
||||
final Future<Integer> future = FlowGraph.factory().closed(Sink.<Integer> head(), new Procedure2<Builder, SinkShape<Integer>>() {
|
||||
@Override
|
||||
public void apply(Builder 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());
|
||||
}
|
||||
}).run(materializer);
|
||||
|
||||
final KeyedSink<Integer, Future<Integer>> out = Sink.head();
|
||||
|
||||
MaterializedMap mat = FlowGraph.builder()
|
||||
.addEdge(in1, sumZip.left())
|
||||
.addEdge(in2, sumZip.right())
|
||||
.addEdge(sumZip.out(), out)
|
||||
.run(materializer);
|
||||
|
||||
final Integer result = Await.result(mat.get(out), Duration.create(300, TimeUnit.MILLISECONDS));
|
||||
final Integer result = Await.result(future, Duration.create(300, TimeUnit.MILLISECONDS));
|
||||
assertEquals(11, (int) result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mustBeAbleToUseZip4With() throws Exception {
|
||||
final Source<Integer> in1 = Source.single(1);
|
||||
final Source<Integer> in2 = Source.single(10);
|
||||
final Source<Integer> in3 = Source.single(100);
|
||||
final Source<Integer> in4 = Source.single(1000);
|
||||
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);
|
||||
|
||||
Function<ZipWith.Zip4WithInputs<Integer, Integer, Integer, Integer>, Integer> sum4 = new Function<ZipWith.Zip4WithInputs<Integer, Integer, Integer, Integer>, Integer>() {
|
||||
@Override
|
||||
public Integer apply(ZipWith.Zip4WithInputs<Integer, Integer, Integer, Integer> inputs) throws Exception {
|
||||
return inputs.t1() + inputs.t2() + inputs.t3() + inputs.t4();
|
||||
final Graph<FanInShape4<Integer, Integer, Integer, Integer, Integer>, BoxedUnit> sumZip = ZipWith.create(
|
||||
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, SinkShape<Integer>>() {
|
||||
@Override
|
||||
public void apply(Builder 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());
|
||||
}
|
||||
}).run(materializer);
|
||||
|
||||
Zip4With<Integer, Integer, Integer, Integer, Integer> sum4Zip = ZipWith.create(sum4);
|
||||
|
||||
final KeyedSink<Integer, Future<Integer>> out = Sink.head();
|
||||
|
||||
MaterializedMap mat = FlowGraph.builder()
|
||||
.addEdge(in1, sum4Zip.input1())
|
||||
.addEdge(in2, sum4Zip.input2())
|
||||
.addEdge(in3, sum4Zip.input3())
|
||||
.addEdge(in4, sum4Zip.input4())
|
||||
.addEdge(sum4Zip.out(), out)
|
||||
.run(materializer);
|
||||
|
||||
final Integer result = Await.result(mat.get(out), Duration.create(300, TimeUnit.MILLISECONDS));
|
||||
final Integer result = Await.result(future, Duration.create(300, TimeUnit.MILLISECONDS));
|
||||
assertEquals(1111, (int) result);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue