Skip to content

Commit 2bc02e2

Browse files
committed
#20 Fix the bug in 'channel.filter(...)'.
1 parent 3176fe8 commit 2bc02e2

File tree

2 files changed

+10
-13
lines changed

2 files changed

+10
-13
lines changed

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

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,21 +53,18 @@ class ChannelDecoratorFilter[T](inputChannel: ReadChannel[T], pred: T => Boolean
5353

5454
val timeoutMilli = if (timeout.isFinite) timeout.toMillis else 0L
5555
val startInstant = Instant.now()
56-
var valueOpt = inputChannel.tryRecv(timeout)
57-
var found = valueOpt.isEmpty || valueOpt.forall(v => pred(v))
58-
var elapsedTime = java.time.Duration.between(startInstant, now).toMillis
56+
var elapsedTime = 0L
5957

60-
if (found || elapsedTime >= timeoutMilli) {
61-
valueOpt
62-
} else {
63-
while (!found && elapsedTime < timeoutMilli) {
64-
val newTimeout = Duration(timeoutMilli - elapsedTime, MILLISECONDS)
65-
valueOpt = inputChannel.tryRecv(newTimeout)
66-
found = valueOpt.isEmpty || valueOpt.forall(v => pred(v))
67-
elapsedTime = java.time.Duration.between(startInstant, now).toMillis
58+
while (elapsedTime < timeoutMilli) {
59+
val newTimeout = Duration(timeoutMilli - elapsedTime, MILLISECONDS)
60+
val valueOpt = inputChannel.tryRecv(newTimeout)
61+
val found = valueOpt.isEmpty || valueOpt.forall(v => pred(v))
62+
elapsedTime = java.time.Duration.between(startInstant, now).toMillis
63+
if (found) {
64+
return valueOpt
6865
}
69-
valueOpt
7066
}
67+
None
7168
}
7269

7370
override def recver(action: T => Unit): Selector = inputChannel.recver(t => if (pred(t)) action(t))

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class ChannelFilterSuite extends AnyWordSpec {
9595
}
9696

9797
"filter input channel on tryRecv(duration)" when {
98-
val timeout = Duration(2, MILLISECONDS)
98+
val timeout = Duration(200, MILLISECONDS)
9999
"values either available or not" in {
100100
val ch1 = Channel.make[Int](3)
101101

0 commit comments

Comments
 (0)