File tree Expand file tree Collapse file tree 3 files changed +14
-6
lines changed
main/scala/com/github/yruslan/channel
test/scala/com/github/yruslan/channel Expand file tree Collapse file tree 3 files changed +14
-6
lines changed Original file line number Diff line number Diff line change @@ -67,8 +67,12 @@ abstract class Channel[T] extends ReadChannel[T] with WriteChannel[T] {
67
67
}
68
68
69
69
final override def fornew [U ](f : T => U ): Unit = {
70
- val valueOpt = tryRecv()
71
- valueOpt.foreach(v => f(v))
70
+ var valueOpt = tryRecv()
71
+
72
+ while (valueOpt.nonEmpty) {
73
+ valueOpt.foreach(v => f(v))
74
+ valueOpt = tryRecv()
75
+ }
72
76
}
73
77
74
78
final override def foreach [U ](f : T => U ): Unit = {
Original file line number Diff line number Diff line change @@ -166,18 +166,19 @@ class ChannelFilterSuite extends AnyWordSpec {
166
166
}
167
167
168
168
" filter input channel on fornew()" in {
169
- val ch1 = Channel .make[Int ](2 )
169
+ val ch1 = Channel .make[Int ](3 )
170
170
171
171
val ch2 = ch1.filter(v => v != 2 )
172
172
173
173
ch1.send(1 )
174
174
ch1.send(2 )
175
+ ch1.send(3 )
175
176
176
177
var v1 = 0
177
178
178
179
ch2.fornew(v => v1 = v)
179
180
180
- assert(v1 == 1 )
181
+ assert(v1 == 3 )
181
182
}
182
183
183
184
" filter input channel on foreach()" in {
Original file line number Diff line number Diff line change @@ -605,8 +605,10 @@ class ChannelSuite extends AnyWordSpec with BeforeAndAfterAll {
605
605
ch.fornew(v => processed += v)
606
606
607
607
assert(processed.nonEmpty)
608
- assert(processed.size == 1 )
608
+ assert(processed.size == 3 )
609
609
assert(processed.head == " test1" )
610
+ assert(processed(1 ) == " test2" )
611
+ assert(processed(2 ) == " test3" )
610
612
}
611
613
612
614
" there are no data" in {
@@ -887,7 +889,8 @@ class ChannelSuite extends AnyWordSpec with BeforeAndAfterAll {
887
889
888
890
assert(lst == List (2 , 6 ))
889
891
}
890
-
892
+ }
893
+ " for comprehension" should {
891
894
" test for comprehension with yield" in {
892
895
val ch1 = Channel .make[Int ](3 )
893
896
You can’t perform that action at this time.
0 commit comments