Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement Source.future operator #32

Merged
merged 1 commit into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions core/src/main/scala/ox/channels/SourceOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import java.util
import java.util.concurrent.{CountDownLatch, Semaphore}
import scala.collection.{IterableOnce, mutable}
import scala.concurrent.duration.FiniteDuration
import scala.concurrent.{ExecutionContext, ExecutionException, Future}
import scala.util.{Failure, Success}

trait SourceOps[+T] { this: Source[T] =>
// view ops (lazy)
Expand Down Expand Up @@ -993,6 +995,39 @@ trait SourceCompanionOps:
}
c

/** Creates a source that emits a single value when `from` completes or fails otherwise. The `from` completion is performed on the
* provided [[scala.concurrent.ExecutionContext]]. Note that when `from` fails with [[scala.concurrent.ExecutionException]] then its
* cause is returned as source failure.
*
* @param from
* A [[scala.concurrent.Future]] that returns value upon completion.
* @return
* A source that will emit value upon a `from` [[scala.concurrent.Future]] completion.
* @example
* {{{
* import ox.*
* import ox.channels.Source
*
* import scala.concurrent.ExecutionContext.Implicits.global
* import scala.concurrent.Future
geminicaprograms marked this conversation as resolved.
Show resolved Hide resolved
*
* supervised {
* Source
* .future(Future.failed(new RuntimeException("future failed")))
* .receive() // ChannelClosed.Error(Some(java.lang.RuntimeException: future failed))
* Source.future(Future.successful(1)).toList // List(1)
* }
* }}}
*/
def future[T](from: Future[T])(using StageCapacity, ExecutionContext): Source[T] =
val c = StageCapacity.newChannel[T]
from.onComplete {
case Success(value) => c.send(value); c.done()
case Failure(ex: ExecutionException) => c.error(ex.getCause)
case Failure(ex) => c.error(ex)
}
c

/** Creates a source that fails immediately with the given [[java.lang.Throwable]]
*
* @param t
Expand Down
29 changes: 29 additions & 0 deletions core/src/test/scala/ox/channels/SourceOpsFutureTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package ox.channels

import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
import ox.*

import scala.concurrent.Future

class SourceOpsFutureTest extends AnyFlatSpec with Matchers {
import scala.concurrent.ExecutionContext.Implicits.global

behavior of "Source.future"

it should "return the original future failure when future fails" in supervised {
val failure = new RuntimeException("future failed")
Source.future(Future.failed(failure)).receive() shouldBe ChannelClosed.Error(Some(failure))
}

it should "return the original future failure when future fails with ExecutionException" in supervised {
// according to https://docs.scala-lang.org/overviews/core/futures.html#exceptions
// the InterruptedException is one of the exceptions wrapped in ExecutionException
val failure = new InterruptedException("future interrupted")
Source.future(Future.failed(failure)).receive() shouldBe ChannelClosed.Error(Some(failure))
}

it should "return future value" in supervised {
Source.future(Future.successful(1)).toList shouldBe List(1)
}
}