-
Notifications
You must be signed in to change notification settings - Fork 1.4k
CancelableFuture #1655
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
CancelableFuture #1655
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
4e1d0ee
Introduce CancelableFuture
fsvehla 3d30fe7
Fix race in CancelableFutureSpec
fsvehla ae138f7
Impure cancel
fsvehla 8d42592
Merge remote-tracking branch 'upstream/master' into ferdinand/cancela…
fsvehla 1c7d86b
Inline Cancelable
fsvehla 19a9bdc
Make CancelableFuture public
fsvehla 5301826
cancel returns a Future
fsvehla f1abb56
Merge remote-tracking branch 'upstream/master' into ferdinand/cancela…
fsvehla 4d36075
Format
fsvehla e80ed91
Fix mdoc
fsvehla 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
41 changes: 41 additions & 0 deletions
41
core-tests/shared/src/test/scala/zio/CancelableFutureSpec.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,41 @@ | ||
| package zio | ||
|
|
||
| import zio.test.Assertion._ | ||
| import zio.test._ | ||
|
|
||
| object CancelableFutureSpec | ||
| extends ZIOBaseSpec( | ||
| suite("CancelableFutureSpec")( | ||
| testM("interrupts the underlying task on cancel") { | ||
| for { | ||
| p <- Promise.make[Nothing, Unit] | ||
| p2 <- Promise.make[Nothing, Int] | ||
| f <- (p.succeed(()) *> IO.never).onInterrupt(p2.succeed(42)).toFuture | ||
| _ <- p.await | ||
| _ <- UIO(f.cancel) | ||
| test <- p2.await | ||
| } yield assert(test, equalTo(42)) | ||
| }, | ||
| testM("cancel returns the exit reason") { | ||
| for { | ||
| t <- UIO(new Exception("test")) | ||
| p1 <- Promise.make[Nothing, Unit] | ||
| p2 <- Promise.make[Nothing, Unit] | ||
| f1 <- (ZIO.succeed(42) <* p1.succeed(())).toFuture | ||
| f2 <- ZIO.fail(t).onError(_ => p2.succeed(())).toFuture | ||
| _ <- p1.await | ||
| _ <- p2.await | ||
| e1 <- ZIO.fromFuture(_ => f1.cancel) | ||
| e2 <- ZIO.fromFuture(_ => f2.cancel) | ||
| } yield assert(e1.succeeded, isTrue) && assert(e2.succeeded, isFalse) | ||
| }, | ||
| testM("is a scala.concurrent.Future") { | ||
| for { | ||
| f <- ZIO(42).toFuture | ||
| v <- ZIO.fromFuture(_ => f) | ||
| } yield { | ||
| assert(v, equalTo(42)) | ||
| } | ||
| } | ||
| ) | ||
| ) | ||
20 changes: 20 additions & 0 deletions
20
core/shared/src/main/scala-2.11/zio/FutureTransformCompat.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,20 @@ | ||
| /* | ||
| * Copyright 2017-2019 John A. De Goes and the ZIO Contributors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package zio | ||
|
|
||
| private[zio] trait FutureTransformCompat[+A] { this: CancelableFuture[Any, A] => | ||
| } |
28 changes: 28 additions & 0 deletions
28
core/shared/src/main/scala-2.12+/zio/FutureTransformCompat.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,28 @@ | ||
| /* | ||
| * Copyright 2017-2019 John A. De Goes and the ZIO Contributors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package zio | ||
|
|
||
| import scala.concurrent.{ ExecutionContext, Future } | ||
| import scala.util.Try | ||
|
|
||
| private[zio] trait FutureTransformCompat[+A] { this: CancelableFuture[Any, A] => | ||
| def transform[S](f: Try[A] => Try[S])(implicit executor: ExecutionContext): Future[S] = | ||
| future.transform(f)(executor) | ||
|
|
||
| def transformWith[S](f: Try[A] => Future[S])(implicit executor: ExecutionContext): Future[S] = | ||
| future.transformWith(f)(executor) | ||
| } |
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,46 @@ | ||
| /* | ||
| * Copyright 2017-2019 John A. De Goes and the ZIO Contributors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package zio | ||
|
|
||
| import scala.concurrent.{ CanAwait, ExecutionContext, Future } | ||
| import scala.concurrent.duration.Duration | ||
| import scala.util.Try | ||
|
|
||
| abstract class CancelableFuture[+E, +A](val future: Future[A]) extends Future[A] with FutureTransformCompat[A] { | ||
|
|
||
| /** | ||
| * Immediately cancels the operation and returns a [[scala.concurrent.Future]] containing the result | ||
| */ | ||
| def cancel: Future[Exit[E, A]] | ||
|
|
||
| def onComplete[U](f: Try[A] => U)(implicit executor: ExecutionContext): Unit = | ||
| future.onComplete(f)(executor) | ||
|
|
||
| def isCompleted: Boolean = | ||
| future.isCompleted | ||
|
|
||
| def value: Option[Try[A]] = | ||
| future.value | ||
|
|
||
| def ready(atMost: Duration)(implicit permit: CanAwait): this.type = { | ||
| future.ready(atMost)(permit) | ||
| this | ||
| } | ||
|
|
||
| def result(atMost: Duration)(implicit permit: CanAwait): A = | ||
| future.result(atMost) | ||
| } |
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 |
|---|---|---|
|
|
@@ -257,7 +257,7 @@ trait Fiber[+E, +A] { self => | |
| * @param ev implicit witness that E is a subtype of Throwable | ||
| * @return `UIO[Future[A]]` | ||
| */ | ||
| final def toFuture(implicit ev: E <:< Throwable): UIO[Future[A]] = | ||
| final def toFuture(implicit ev: E <:< Throwable): UIO[CancelableFuture[E, A]] = | ||
| toFutureWith(ev) | ||
|
|
||
| /** | ||
|
|
@@ -267,17 +267,20 @@ trait Fiber[+E, +A] { self => | |
| * @param f function to the error into a Throwable | ||
| * @return `UIO[Future[A]]` | ||
| */ | ||
| final def toFutureWith(f: E => Throwable): UIO[Future[A]] = | ||
| final def toFutureWith(f: E => Throwable): UIO[CancelableFuture[E, A]] = | ||
| UIO.effectTotal { | ||
| val p: concurrent.Promise[A] = scala.concurrent.Promise[A]() | ||
|
|
||
| def failure(cause: Cause[E]): UIO[p.type] = UIO(p.failure(cause.squashWith(f))) | ||
| def success(value: A): UIO[p.type] = UIO(p.success(value)) | ||
|
|
||
| UIO.effectTotal(p.future) <* self.await | ||
| ZIO.runtime[Any].map { runtime => | ||
| new CancelableFuture[E, A](p.future) { | ||
| def cancel: Future[Exit[E, A]] = runtime.unsafeRunToFuture(interrupt) | ||
| } | ||
| } <* self.await | ||
| .flatMap[Any, Nothing, p.type](_.foldM[Any, Nothing, p.type](failure, success)) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if type annotations could be avoided if |
||
| .fork | ||
|
|
||
| }.flatten | ||
|
|
||
| /** | ||
|
|
||
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Love the new ZIO Test!