/
GraphUnzipSpec.scala
executable file
·193 lines (166 loc) · 6 KB
/
GraphUnzipSpec.scala
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
190
191
192
193
/**
* Copyright (C) 2014-2018 Lightbend Inc. <https://www.lightbend.com>
*/
package akka.stream.scaladsl
import scala.concurrent.duration._
import akka.stream.{ ClosedShape, OverflowStrategy, ActorMaterializerSettings, ActorMaterializer }
import akka.stream.testkit._
import akka.stream.testkit.Utils._
class GraphUnzipSpec extends StreamSpec {
val settings = ActorMaterializerSettings(system)
.withInputBuffer(initialSize = 2, maxSize = 16)
implicit val materializer = ActorMaterializer(settings)
"A unzip" must {
import GraphDSL.Implicits._
"unzip to two subscribers" in assertAllStagesStopped {
val c1 = TestSubscriber.manualProbe[Int]()
val c2 = TestSubscriber.manualProbe[String]()
RunnableGraph.fromGraph(GraphDSL.create() { implicit b ⇒
val unzip = b.add(Unzip[Int, String]())
Source(List(1 → "a", 2 → "b", 3 → "c")) ~> unzip.in
unzip.out1 ~> Flow[String].buffer(16, OverflowStrategy.backpressure) ~> Sink.fromSubscriber(c2)
unzip.out0 ~> Flow[Int].buffer(16, OverflowStrategy.backpressure).map(_ * 2) ~> Sink.fromSubscriber(c1)
ClosedShape
}).run()
val sub1 = c1.expectSubscription()
val sub2 = c2.expectSubscription()
sub1.request(1)
sub2.request(2)
c1.expectNext(1 * 2)
c1.expectNoMsg(100.millis)
c2.expectNext("a")
c2.expectNext("b")
c2.expectNoMsg(100.millis)
sub1.request(3)
c1.expectNext(2 * 2)
c1.expectNext(3 * 2)
c1.expectComplete()
sub2.request(3)
c2.expectNext("c")
c2.expectComplete()
}
"produce to right downstream even though left downstream cancels" in {
val c1 = TestSubscriber.manualProbe[Int]()
val c2 = TestSubscriber.manualProbe[String]()
RunnableGraph.fromGraph(GraphDSL.create() { implicit b ⇒
val unzip = b.add(Unzip[Int, String]())
Source(List(1 → "a", 2 → "b", 3 → "c")) ~> unzip.in
unzip.out0 ~> Sink.fromSubscriber(c1)
unzip.out1 ~> Sink.fromSubscriber(c2)
ClosedShape
}).run()
val sub1 = c1.expectSubscription()
val sub2 = c2.expectSubscription()
sub1.cancel()
sub2.request(3)
c2.expectNext("a")
c2.expectNext("b")
c2.expectNext("c")
c2.expectComplete()
}
"produce to left downstream even though right downstream cancels" in {
val c1 = TestSubscriber.manualProbe[Int]()
val c2 = TestSubscriber.manualProbe[String]()
RunnableGraph.fromGraph(GraphDSL.create() { implicit b ⇒
val unzip = b.add(Unzip[Int, String]())
Source(List(1 → "a", 2 → "b", 3 → "c")) ~> unzip.in
unzip.out0 ~> Sink.fromSubscriber(c1)
unzip.out1 ~> Sink.fromSubscriber(c2)
ClosedShape
}).run()
val sub1 = c1.expectSubscription()
val sub2 = c2.expectSubscription()
sub2.cancel()
sub1.request(3)
c1.expectNext(1)
c1.expectNext(2)
c1.expectNext(3)
c1.expectComplete()
}
"not push twice when pull is followed by cancel before element has been pushed" in {
val c1 = TestSubscriber.manualProbe[Int]()
val c2 = TestSubscriber.manualProbe[String]()
RunnableGraph.fromGraph(GraphDSL.create() { implicit b ⇒
val unzip = b.add(Unzip[Int, String]())
Source(List(1 → "a", 2 → "b", 3 → "c")) ~> unzip.in
unzip.out0 ~> Sink.fromSubscriber(c1)
unzip.out1 ~> Sink.fromSubscriber(c2)
ClosedShape
}).run()
val sub1 = c1.expectSubscription()
val sub2 = c2.expectSubscription()
sub2.request(3)
sub1.request(3)
sub2.cancel()
c1.expectNext(1)
c1.expectNext(2)
c1.expectNext(3)
c1.expectComplete()
}
"not lose elements when pull is followed by cancel before other sink has requested" in {
val c1 = TestSubscriber.manualProbe[Int]()
val c2 = TestSubscriber.manualProbe[String]()
RunnableGraph.fromGraph(GraphDSL.create() { implicit b ⇒
val unzip = b.add(Unzip[Int, String]())
Source(List(1 → "a", 2 → "b", 3 → "c")) ~> unzip.in
unzip.out0 ~> Sink.fromSubscriber(c1)
unzip.out1 ~> Sink.fromSubscriber(c2)
ClosedShape
}).run()
val sub1 = c1.expectSubscription()
val sub2 = c2.expectSubscription()
sub2.request(3)
sub2.cancel()
sub1.request(3)
c1.expectNext(1)
c1.expectNext(2)
c1.expectNext(3)
c1.expectComplete()
}
"cancel upstream when downstreams cancel" in {
val p1 = TestPublisher.manualProbe[(Int, String)]()
val c1 = TestSubscriber.manualProbe[Int]()
val c2 = TestSubscriber.manualProbe[String]()
RunnableGraph.fromGraph(GraphDSL.create() { implicit b ⇒
val unzip = b.add(Unzip[Int, String]())
Source.fromPublisher(p1.getPublisher) ~> unzip.in
unzip.out0 ~> Sink.fromSubscriber(c1)
unzip.out1 ~> Sink.fromSubscriber(c2)
ClosedShape
}).run()
val p1Sub = p1.expectSubscription()
val sub1 = c1.expectSubscription()
val sub2 = c2.expectSubscription()
sub1.request(3)
sub2.request(3)
p1.expectRequest(p1Sub, 16)
p1Sub.sendNext(1 → "a")
c1.expectNext(1)
c2.expectNext("a")
p1Sub.sendNext(2 → "b")
c1.expectNext(2)
c2.expectNext("b")
sub1.cancel()
sub2.cancel()
p1Sub.expectCancellation()
}
"work with zip" in assertAllStagesStopped {
val c1 = TestSubscriber.manualProbe[(Int, String)]()
RunnableGraph.fromGraph(GraphDSL.create() { implicit b ⇒
val zip = b.add(Zip[Int, String]())
val unzip = b.add(Unzip[Int, String]())
Source(List(1 → "a", 2 → "b", 3 → "c")) ~> unzip.in
unzip.out0 ~> zip.in0
unzip.out1 ~> zip.in1
zip.out ~> Sink.fromSubscriber(c1)
ClosedShape
}).run()
val sub1 = c1.expectSubscription()
sub1.request(5)
c1.expectNext(1 → "a")
c1.expectNext(2 → "b")
c1.expectNext(3 → "c")
c1.expectComplete()
}
}
}