forked from akka/akka
/
StreamPartialGraphDSLDocTest.java
189 lines (156 loc) · 6.57 KB
/
StreamPartialGraphDSLDocTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
* Copyright (C) 2015-2022 Lightbend Inc. <https://www.lightbend.com>
*/
package jdocs.stream;
import static org.junit.Assert.assertEquals;
import akka.Done;
import akka.NotUsed;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.japi.Pair;
import akka.stream.*;
import akka.stream.javadsl.*;
import akka.testkit.javadsl.TestKit;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.TimeUnit;
import jdocs.AbstractJavaTest;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
public class StreamPartialGraphDSLDocTest extends AbstractJavaTest {
static ActorSystem system;
@BeforeClass
public static void setup() {
system = ActorSystem.create("StreamPartialGraphDSLDocTest");
}
@AfterClass
public static void tearDown() {
TestKit.shutdownActorSystem(system);
system = null;
}
@Test
public void demonstrateBuildWithOpenPorts() throws Exception {
// #simple-partial-graph-dsl
final Graph<FanInShape2<Integer, Integer, Integer>, NotUsed> zip =
ZipWith.create((Integer left, Integer right) -> Math.max(left, right));
final Graph<UniformFanInShape<Integer, Integer>, NotUsed> pickMaxOfThree =
GraphDSL.create(
builder -> {
final FanInShape2<Integer, Integer, Integer> zip1 = builder.add(zip);
final FanInShape2<Integer, Integer, Integer> zip2 = builder.add(zip);
builder.from(zip1.out()).toInlet(zip2.in0());
// return the shape, which has three inputs and one output
return UniformFanInShape.<Integer, Integer>create(
zip2.out(), Arrays.asList(zip1.in0(), zip1.in1(), zip2.in1()));
});
final Sink<Integer, CompletionStage<Integer>> resultSink = Sink.<Integer>head();
final RunnableGraph<CompletionStage<Integer>> g =
RunnableGraph.<CompletionStage<Integer>>fromGraph(
GraphDSL.create(
resultSink,
(builder, sink) -> {
// import the partial graph explicitly
final UniformFanInShape<Integer, Integer> pm = builder.add(pickMaxOfThree);
builder.from(builder.add(Source.single(1))).toInlet(pm.in(0));
builder.from(builder.add(Source.single(2))).toInlet(pm.in(1));
builder.from(builder.add(Source.single(3))).toInlet(pm.in(2));
builder.from(pm.out()).to(sink);
return ClosedShape.getInstance();
}));
final CompletionStage<Integer> max = g.run(system);
// #simple-partial-graph-dsl
assertEquals(Integer.valueOf(3), max.toCompletableFuture().get(3, TimeUnit.SECONDS));
}
// #source-from-partial-graph-dsl
// first create an indefinite source of integer numbers
class Ints implements Iterator<Integer> {
private int next = 0;
@Override
public boolean hasNext() {
return true;
}
@Override
public Integer next() {
return next++;
}
}
// #source-from-partial-graph-dsl
@Test
public void demonstrateBuildSourceFromPartialGraphDSLCreate() throws Exception {
// #source-from-partial-graph-dsl
final Source<Integer, NotUsed> ints = Source.fromIterator(() -> new Ints());
final Source<Pair<Integer, Integer>, NotUsed> pairs =
Source.fromGraph(
GraphDSL.create(
builder -> {
final FanInShape2<Integer, Integer, Pair<Integer, Integer>> zip =
builder.add(Zip.create());
builder.from(builder.add(ints.filter(i -> i % 2 == 0))).toInlet(zip.in0());
builder.from(builder.add(ints.filter(i -> i % 2 == 1))).toInlet(zip.in1());
return SourceShape.of(zip.out());
}));
final CompletionStage<Pair<Integer, Integer>> firstPair =
pairs.runWith(Sink.<Pair<Integer, Integer>>head(), system);
// #source-from-partial-graph-dsl
assertEquals(new Pair<>(0, 1), firstPair.toCompletableFuture().get(3, TimeUnit.SECONDS));
}
@Test
public void demonstrateBuildFlowFromPartialGraphDSLCreate() throws Exception {
// #flow-from-partial-graph-dsl
final Flow<Integer, Pair<Integer, String>, NotUsed> pairs =
Flow.fromGraph(
GraphDSL.create(
b -> {
final UniformFanOutShape<Integer, Integer> bcast = b.add(Broadcast.create(2));
final FanInShape2<Integer, String, Pair<Integer, String>> zip =
b.add(Zip.create());
b.from(bcast).toInlet(zip.in0());
b.from(bcast)
.via(b.add(Flow.of(Integer.class).map(i -> i.toString())))
.toInlet(zip.in1());
return FlowShape.of(bcast.in(), zip.out());
}));
// #flow-from-partial-graph-dsl
final CompletionStage<Pair<Integer, String>> matSink =
// #flow-from-partial-graph-dsl
Source.single(1).via(pairs).runWith(Sink.<Pair<Integer, String>>head(), system);
// #flow-from-partial-graph-dsl
assertEquals(new Pair<>(1, "1"), matSink.toCompletableFuture().get(3, TimeUnit.SECONDS));
}
@Test
public void demonstrateBuildSourceWithCombine() throws Exception {
// #source-combine
Source<Integer, NotUsed> source1 = Source.single(1);
Source<Integer, NotUsed> source2 = Source.single(2);
final Source<Integer, NotUsed> sources =
Source.combine(source1, source2, new ArrayList<>(), i -> Merge.<Integer>create(i));
// #source-combine
final CompletionStage<Integer> result =
// #source-combine
sources.runWith(Sink.<Integer, Integer>fold(0, (a, b) -> a + b), system);
// #source-combine
assertEquals(Integer.valueOf(3), result.toCompletableFuture().get(3, TimeUnit.SECONDS));
}
@Test
public void demonstrateBuildSinkWithCombine() throws Exception {
final TestKit probe = new TestKit(system);
ActorRef actorRef = probe.getRef();
// #sink-combine
Sink<Integer, NotUsed> sendRemotely = Sink.actorRef(actorRef, "Done");
Sink<Integer, CompletionStage<Done>> localProcessing =
Sink.<Integer>foreach(
a -> {
/*do something useful*/
});
Sink<Integer, NotUsed> sinks =
Sink.combine(sendRemotely, localProcessing, new ArrayList<>(), a -> Broadcast.create(a));
Source.<Integer>from(Arrays.asList(new Integer[] {0, 1, 2})).runWith(sinks, system);
// #sink-combine
probe.expectMsgEquals(0);
probe.expectMsgEquals(1);
probe.expectMsgEquals(2);
}
}