-
Notifications
You must be signed in to change notification settings - Fork 23
Add Monix task utilities #543
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
839c89d
Add Monix task utilities
sebaciv f126d73
null edge case tests for Monix extensions
sebaciv 7eb8986
lazyTimeout - add Task.defer to make it lazy
sebaciv 54a8942
Implement Task.traverseMap and unit tests for the new extensions
sebaciv e8f8e74
Monix extensions - fix test
sebaciv 0d69f66
Monix extensions - move broken test to jvm only
sebaciv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
core/jvm/src/test/scala/com/avsystem/commons/concurrent/JvmTaskExtensionsTest.scala
This file contains hidden or 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,26 @@ | ||
package com.avsystem.commons | ||
package concurrent | ||
|
||
import monix.eval.Task | ||
import monix.execution.Scheduler | ||
import org.scalatest.concurrent.ScalaFutures | ||
import org.scalatest.funsuite.AnyFunSuite | ||
import org.scalatest.matchers.should.Matchers | ||
import org.scalatestplus.scalacheck.ScalaCheckDrivenPropertyChecks | ||
|
||
import scala.concurrent.TimeoutException | ||
import scala.concurrent.duration._ | ||
|
||
class JvmTaskExtensionsTest extends AnyFunSuite with Matchers with ScalaCheckDrivenPropertyChecks with ScalaFutures { | ||
|
||
import com.avsystem.commons.concurrent.TaskExtensions._ | ||
|
||
private implicit val scheduler: Scheduler = Scheduler.global | ||
|
||
// This test does not work in SJS runtime (but the method itself does) | ||
test("lazyTimeout") { | ||
val result = Task.never.lazyTimeout(50.millis, "Lazy timeout").runToFuture.failed.futureValue | ||
result shouldBe a[TimeoutException] | ||
result.getMessage shouldBe "Lazy timeout" | ||
} | ||
} |
This file contains hidden or 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
66 changes: 66 additions & 0 deletions
66
core/src/main/scala/com/avsystem/commons/concurrent/TaskExtensions.scala
This file contains hidden or 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,66 @@ | ||
package com.avsystem.commons | ||
package concurrent | ||
|
||
import com.avsystem.commons.concurrent.TaskExtensions.{TaskCompanionOps, TaskOps} | ||
import com.avsystem.commons.misc.Timestamp | ||
import monix.eval.Task | ||
import monix.reactive.Observable | ||
|
||
import java.util.concurrent.TimeUnit | ||
import scala.concurrent.TimeoutException | ||
import scala.concurrent.duration.FiniteDuration | ||
|
||
trait TaskExtensions { | ||
implicit def taskOps[T](task: Task[T]): TaskOps[T] = new TaskOps(task) | ||
|
||
implicit def taskCompanionOps(task: Task.type): TaskCompanionOps.type = TaskCompanionOps | ||
} | ||
|
||
object TaskExtensions extends TaskExtensions { | ||
final class TaskOps[T](private val task: Task[T]) extends AnyVal { | ||
/** | ||
* Similar to [[Task.timeoutWith]] but exception instance is created lazily (for performance) | ||
*/ | ||
def lazyTimeout(after: FiniteDuration, msg: => String): Task[T] = | ||
ddworak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
task.timeoutTo(after, Task.defer(Task.raiseError(new TimeoutException(msg)))) | ||
|
||
/** | ||
* Similar to [[Task.tapEval]], accepts simple consumer function as an argument | ||
*/ | ||
def tapL(f: T => Unit): Task[T] = | ||
task.map(_.setup(f)) | ||
|
||
/** | ||
* Similar to [[Task.tapError]], accepts [[PartialFunction]] as an argument | ||
*/ | ||
def tapErrorL[B](f: PartialFunction[Throwable, B]): Task[T] = | ||
task.tapError(t => Task(f.applyOpt(t))) | ||
} | ||
|
||
object TaskCompanionOps { | ||
ddworak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
import com.avsystem.commons.concurrent.ObservableExtensions.observableOps | ||
|
||
/** A [[Task]] of [[Opt.Empty]] */ | ||
def optEmpty[A]: Task[Opt[A]] = Task.pure(Opt.Empty) | ||
|
||
def traverseOpt[A, B](opt: Opt[A])(f: A => Task[B]): Task[Opt[B]] = | ||
opt.fold(Task.optEmpty[B])(a => f(a).map(_.opt)) | ||
|
||
def fromOpt[A](maybeTask: Opt[Task[A]]): Task[Opt[A]] = maybeTask match { | ||
case Opt(task) => task.map(_.opt) | ||
case Opt.Empty => Task.optEmpty | ||
} | ||
|
||
def traverseMap[K, V, A, B](map: Map[K, V])(f: (K, V) => Task[(A, B)]): Task[Map[A, B]] = | ||
Observable.fromIterable(map).mapEval({ case (key, value) => f(key, value) }).toL(Map) | ||
|
||
def traverseMapValues[K, A, B](map: Map[K, A])(f: (K, A) => Task[B]): Task[Map[K, B]] = | ||
traverseMap(map)({ case (key, value) => f(key, value).map(key -> _) }) | ||
|
||
def currentTimestamp: Task[Timestamp] = | ||
Task.clock.realTime(TimeUnit.MILLISECONDS).map(Timestamp(_)) | ||
|
||
def usingNow[T](useNow: Timestamp => Task[T]): Task[T] = | ||
ddworak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
currentTimestamp.flatMap(useNow) | ||
} | ||
} |
This file contains hidden or 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
43 changes: 43 additions & 0 deletions
43
core/src/test/scala/com/avsystem/commons/concurrent/TaskExtensionsTest.scala
This file contains hidden or 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,43 @@ | ||
package com.avsystem.commons | ||
package concurrent | ||
|
||
import monix.eval.Task | ||
import monix.execution.Scheduler | ||
import org.scalatest.concurrent.ScalaFutures | ||
import org.scalatest.funsuite.AnyFunSuite | ||
import org.scalatest.matchers.should.Matchers | ||
import org.scalatestplus.scalacheck.ScalaCheckDrivenPropertyChecks | ||
|
||
class TaskExtensionsTest extends AnyFunSuite with Matchers with ScalaCheckDrivenPropertyChecks with ScalaFutures { | ||
import com.avsystem.commons.concurrent.TaskExtensions._ | ||
|
||
private implicit val scheduler: Scheduler = Scheduler.global | ||
|
||
test("traverseOpt") { | ||
Task.traverseOpt(Opt.empty[Int])(i => Task.now(i)).runToFuture.futureValue shouldBe Opt.Empty | ||
Task.traverseOpt(Opt.some(123))(i => Task.now(i)).runToFuture.futureValue shouldBe Opt.some(123) | ||
} | ||
|
||
test("fromOpt") { | ||
Task.fromOpt(Opt.empty[Task[Int]]).runToFuture.futureValue shouldBe Opt.Empty | ||
Task.fromOpt(Opt.some(Task.now(123))).runToFuture.futureValue shouldBe Opt.some(123) | ||
} | ||
|
||
test("traverseMap") { | ||
forAll { data: List[(String, Int)] => | ||
val map = data.toMap | ||
val expected = map.view.map({ case (key, value) => (key + key, value + 2) }).toMap | ||
val result = Task.traverseMap(map)({ case (key, value) => Task((key + key, value + 2)) }).runToFuture.futureValue | ||
result shouldBe expected | ||
} | ||
} | ||
|
||
test("traverseMapValues") { | ||
forAll { data: List[(String, Int)] => | ||
val map = data.toMap | ||
val expected = map.view.mapValues(value => value + 2).toMap | ||
val result = Task.traverseMapValues(map)({ case (key, value) => Task(value + 2) }).runToFuture.futureValue | ||
result shouldBe expected | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.