Skip to content

Commit 7363cb5

Browse files
committed
Change the semantics of fornew()
1 parent b182b14 commit 7363cb5

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

src/main/scala/com/github/yruslan/channel/Channel.scala

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,12 @@ abstract class Channel[T] extends ReadChannel[T] with WriteChannel[T] {
6767
}
6868

6969
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+
}
7276
}
7377

7478
final override def foreach[U](f: T => U): Unit = {

src/test/scala/com/github/yruslan/channel/ChannelFilterSuite.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,18 +166,19 @@ class ChannelFilterSuite extends AnyWordSpec {
166166
}
167167

168168
"filter input channel on fornew()" in {
169-
val ch1 = Channel.make[Int](2)
169+
val ch1 = Channel.make[Int](3)
170170

171171
val ch2 = ch1.filter(v => v != 2)
172172

173173
ch1.send(1)
174174
ch1.send(2)
175+
ch1.send(3)
175176

176177
var v1 = 0
177178

178179
ch2.fornew(v => v1 = v)
179180

180-
assert(v1 == 1)
181+
assert(v1 == 3)
181182
}
182183

183184
"filter input channel on foreach()" in {

src/test/scala/com/github/yruslan/channel/ChannelSuite.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -605,8 +605,10 @@ class ChannelSuite extends AnyWordSpec with BeforeAndAfterAll {
605605
ch.fornew(v => processed += v)
606606

607607
assert(processed.nonEmpty)
608-
assert(processed.size == 1)
608+
assert(processed.size == 3)
609609
assert(processed.head == "test1")
610+
assert(processed(1) == "test2")
611+
assert(processed(2) == "test3")
610612
}
611613

612614
"there are no data" in {
@@ -887,7 +889,8 @@ class ChannelSuite extends AnyWordSpec with BeforeAndAfterAll {
887889

888890
assert(lst == List(2, 6))
889891
}
890-
892+
}
893+
"for comprehension" should {
891894
"test for comprehension with yield" in {
892895
val ch1 = Channel.make[Int](3)
893896

0 commit comments

Comments
 (0)