/
FeedbackLoopSpec.scala
192 lines (154 loc) · 5.89 KB
/
FeedbackLoopSpec.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
/*
* Copyright (C) 2016 Lightbend Inc. <http://www.lightbend.com>
*/
package akka.stream.contrib
import akka.stream.{Attributes, OverflowStrategy}
import akka.stream.scaladsl.{Flow, Keep, Source}
import akka.stream.testkit.scaladsl.{TestSink, TestSource}
import scala.concurrent.duration._
class FeedbackLoopSpecAutoFusingOn extends { val autoFusing = true } with FeedbackLoopSpec
class FeedbackLoopSpecAutoFusingOff extends { val autoFusing = false } with FeedbackLoopSpec
trait FeedbackLoopSpec extends BaseStreamSpec {
"Feedback" should {
"not deadlock on slow downstream" in {
val N = 1000
val forwardFlow = PartitionWith((i: Int) => if (i % 2 == 1) Left(i / 2) else Right(i))
val feedbackArc = Flow[Int]
val testedFlow = FeedbackLoop(forwardFlow, feedbackArc, N / 2)(Keep.none)
val sub = Source(1 to N)
.via(testedFlow)
.via(Flow[Int].delay(1.millis, OverflowStrategy.backpressure))
.toMat(TestSink.probe[Int])(Keep.right)
.run()
sub.request(n = N.toLong)
sub.expectNextN(N.toLong)
}
"not deadlock on slow feedback arc" in {
val bufferSize = 8
val forwardFlow = PartitionWith((i: Long) => if (i % 2 == 1) Left(i / 2) else Right(i))
val feedbackArc = Flow[Long]
.delay(1.millis, OverflowStrategy.backpressure)
.withAttributes(Attributes.inputBuffer(bufferSize, bufferSize))
val testedFlow = FeedbackLoop(forwardFlow, feedbackArc, bufferSize)(Keep.none)
val (pub, sub) = TestSource
.probe[Long]
.via(testedFlow)
.toMat(TestSink.probe[Long])(Keep.both)
.run()
val N = 1000L
sub.request(n = N)
for (i <- 1L to N) {
pub.sendNext(i)
}
sub.expectNextN(N)
}
"fail on too many messages in the feedback arc" in {
val forwardFlow = PartitionWith((i: Long) => if (i > 0) Left(i - 1) else Right(i))
val feedbackArc = Flow[Long].mapConcat(i => List(i, i))
val testedFlow = FeedbackLoop(forwardFlow, feedbackArc, 1000)(Keep.none)
val (pub, sub) = TestSource
.probe[Long]
.via(testedFlow)
.toMat(TestSink.probe[Long])(Keep.both)
.run()
sub.request(n = 1)
pub.sendNext(1000L)
sub.expectError()
}
"fail on forward flow failure" in {
import PartitionWith.Implicits._
val forwardFlow = Flow[Long]
.mapConcat(i => if (i > 0L) List(Left(i - 1), Right(i)) else throw new IllegalArgumentException(i.toString))
.partitionWith(identity)
val feedbackArc = Flow[Long]
val testedFlow = FeedbackLoop(forwardFlow, feedbackArc, 1)(Keep.none)
val (pub, sub) = TestSource
.probe[Long]
.via(testedFlow)
.toMat(TestSink.probe[Long])(Keep.both)
.run()
sub.request(n = 10)
pub.sendNext(5L)
sub.expectNext(5L, 4L, 3L, 2L, 1L)
val error = sub.expectError()
assert(error.asInstanceOf[IllegalArgumentException].getMessage == 0L.toString)
}
"fail on feedback arc failure" in {
import PartitionWith.Implicits._
val forwardFlow = Flow[Long]
.mapConcat(i => if (i > 0L) List(Left(i - 1), Right(i)) else List(Right(i)))
.partitionWith(identity)
val feedbackArc = Flow[Long].map(i => if (i > 2L) i else throw new IllegalArgumentException(i.toString))
val testedFlow = FeedbackLoop(forwardFlow, feedbackArc, 1)(Keep.none)
val (pub, sub) = TestSource
.probe[Long]
.via(testedFlow)
.toMat(TestSink.probe[Long])(Keep.both)
.run()
sub.request(n = 10)
pub.sendNext(5L)
sub.expectNext(5L, 4L, 3L)
val error = sub.expectError()
assert(error.asInstanceOf[IllegalArgumentException].getMessage == 2L.toString)
}
"fail on upstream failure" in {
import PartitionWith.Implicits._
import FeedbackLoop.Implicits._
val forwardFlow = Flow[Long]
.mapConcat(i => if (i > 0L) List(Left(i - 1), Right(i)) else List(Right(i)))
.partitionWith(identity)
val testedFlow = forwardFlow.feedback(1)
val (pub, sub) = TestSource
.probe[Long]
.via(testedFlow)
.toMat(TestSink.probe[Long])(Keep.both)
.run()
sub.request(n = 10)
pub.sendNext(5L)
sub.expectNext(5L, 4L, 3L, 2L, 1L, 0L)
pub.sendError(new IllegalArgumentException("foo"))
val error = sub.expectError()
assert(error.asInstanceOf[IllegalArgumentException].getMessage == "foo")
}
"keep working after feedback arc completes" in {
import PartitionWith.Implicits._
val forwardFlow = Flow[Long]
.mapConcat { i =>
if (i > 0L) List(Left(i - 1), Right(i)) else List(Right(i))
}
.partitionWith(identity)
val feedbackArc = Flow[Long].take(5)
val testedFlow = FeedbackLoop(forwardFlow, feedbackArc, 1)(Keep.none)
val (pub, sub) = TestSource
.probe[Long]
.via(testedFlow)
.toMat(TestSink.probe[Long])(Keep.both)
.run()
sub.request(n = 10)
pub.sendNext(10L)
sub.expectNext(10L, 9L, 8L, 7L, 6L, 5L)
pub.sendNext(4L)
sub.expectNext(4L)
pub.sendNext(3L)
sub.expectNext(3L)
}
"be able to compute the Fibonacci sequence" in {
import PartitionWith.Implicits._
import FeedbackLoop.Implicits._
val forwardFlow = Flow[(Int, Int)]
.mapConcat { case pair @ (n, _) => Right(n) :: Left(pair) :: Nil }
.partitionWith(identity)
val feedbackArc = Flow[(Int, Int)]
.map { case (n, n1) => (n1, n + n1) }
val testedFlow = forwardFlow.feedbackVia(feedbackArc, 1)
val (pub, sub) = TestSource
.probe[(Int, Int)]
.via(testedFlow)
.toMat(TestSink.probe[Int])(Keep.both)
.run()
sub.request(n = 20)
pub.sendNext((0, 1))
sub.expectNextN(List(0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181))
}
}
}