-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The `fold` operation returns combined value retrieved from running function `f` on all source elements in a cumulative manner where result of the previous call is used as an input value to the next e.g.: Source.empty[Int].fold(0)((acc, n) => acc + n) // 0 Source.fromValues(2, 3).fold(5)((acc, n) => acc - n) // 0 Note that in case when `receive()` operation fails then ChannelClosedException.Error exception is thrown.
- Loading branch information
1 parent
2e33ffc
commit 2740fe7
Showing
2 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package ox.channels | ||
|
||
import org.scalatest.flatspec.AnyFlatSpec | ||
import org.scalatest.matchers.should.Matchers | ||
import ox.* | ||
|
||
class SourceOpsFoldTest extends AnyFlatSpec with Matchers { | ||
behavior of "Source.fold" | ||
|
||
it should "throw ChannelClosedException.Error with exception and message that was thrown during retrieval" in supervised { | ||
the[ChannelClosedException.Error] thrownBy { | ||
Source | ||
.failed[Int](new RuntimeException("source is broken")) | ||
.fold(0)((acc, n) => acc + n) | ||
} should have message "java.lang.RuntimeException: source is broken" | ||
} | ||
|
||
it should "throw ChannelClosedException.Error for source failed without exception" in supervised { | ||
the[ChannelClosedException.Error] thrownBy { | ||
Source | ||
.failedWithoutReason[Int]() | ||
.fold(0)((acc, n) => acc + n) | ||
} | ||
} | ||
|
||
it should "return `zero` value from fold on the empty source" in supervised { | ||
Source.empty[Int].fold(0)((acc, n) => acc + n) shouldBe 0 | ||
} | ||
|
||
it should "return fold on non-empty source" in supervised { | ||
Source.fromValues(1, 2).fold(0)((acc, n) => acc + n) shouldBe 3 | ||
} | ||
|
||
it should "drain the source" in supervised { | ||
val s = Source.fromValues(1) | ||
s.fold(0)((acc, n) => acc + n) shouldBe 1 | ||
s.receive() shouldBe ChannelClosed.Done | ||
} | ||
} |