-
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.
feat: implement
Source.futureSource
operator
Creates a source that emits elements from future source when it completes or fails otherwise. The future completion is performed on the provided `ExecutionContext` whereas elements are emitted through `Supervised`. Note that when Future fails with `ExecutionException` then its cause is returned as source failure. Examples: Source .futureSource(Future.failed(new RuntimeException("future failed"))) .receive() // ChannelClosed.Error(Some(java.lang.RuntimeException: future failed)) Source.futureSource(Future.successful(Source.fromValues(1, 2))).toList // List(1, 2)
- Loading branch information
1 parent
f18c916
commit ffe22ae
Showing
2 changed files
with
70 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
28 changes: 28 additions & 0 deletions
28
core/src/test/scala/ox/channels/SourceOpsFutureSourceTest.scala
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,28 @@ | ||
package ox.channels | ||
|
||
import org.scalatest.flatspec.AnyFlatSpec | ||
import org.scalatest.matchers.should.Matchers | ||
import ox.* | ||
|
||
import scala.concurrent.Future | ||
|
||
class SourceOpsFutureSourceTest extends AnyFlatSpec with Matchers { | ||
import scala.concurrent.ExecutionContext.Implicits.global | ||
|
||
behavior of "SourceOps.futureSource" | ||
|
||
it should "fail to create source from the null future" in supervised { | ||
the[IllegalArgumentException] thrownBy { | ||
Source.futureSource(null) | ||
} should have message "requirement failed: from cannot be null" | ||
} | ||
|
||
it should "return the original future failure when future fails" in supervised { | ||
val failure = new RuntimeException("future failed") | ||
Source.futureSource(Future.failed(failure)).receive() shouldBe ChannelClosed.Error(Some(failure)) | ||
} | ||
|
||
it should "return future's source values" in supervised { | ||
Source.futureSource(Future.successful(Source.fromValues(1, 2))).toList shouldBe List(1, 2) | ||
} | ||
} |