-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #833 from mijicd/twitter-futures-interop
Support Twitter futures
- Loading branch information
Showing
16 changed files
with
192 additions
and
55 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
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
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
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
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,31 @@ | ||
--- | ||
id: interop_twitter | ||
title: "Twitter" | ||
--- | ||
|
||
`interop-twitter` module provides capability to convert Twitter `Future` into ZIO `Task`. | ||
|
||
### Example | ||
|
||
```scala | ||
import com.twitter.util.Future | ||
import scalaz.zio.{ App, Task } | ||
import scalaz.zio.console._ | ||
import scalaz.zio.interop.twitter._ | ||
|
||
object Example extends App { | ||
def run(args: List[String]) = { | ||
val program = | ||
for { | ||
_ <- putStrLn("Hello! What is your name?") | ||
name <- getStrLn | ||
greeting <- Task.fromTwitterFuture(Task(greet(name))) | ||
_ <- putStrLn(greeting) | ||
} yield () | ||
|
||
program.fold(_ => 1, _ => 0) | ||
} | ||
|
||
private def greet(name: String): Future[String] = Future.value(s"Hello, $name!") | ||
} | ||
``` |
6 changes: 3 additions & 3 deletions
6
interop-monix/jvm/src/test/scala/scalaz/zio/interop/MonixSpec.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
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
36 changes: 36 additions & 0 deletions
36
interop-twitter/jvm/src/main/scala/scalaz/zio/interop/twitter.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,36 @@ | ||
/* | ||
* 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 scalaz.zio.interop | ||
|
||
import com.twitter.util.{ Future, FutureCancelledException, Return, Throw } | ||
import scalaz.zio.{ Task, UIO } | ||
|
||
package object twitter { | ||
implicit class TaskObjOps(private val obj: Task.type) extends AnyVal { | ||
final def fromTwitterFuture[A](future: Task[Future[A]]): Task[A] = | ||
future.flatMap { f => | ||
Task.effectAsyncInterrupt { cb => | ||
f.respond { | ||
case Return(a) => cb(Task.succeed(a)) | ||
case Throw(e) => cb(Task.fail(e)) | ||
} | ||
|
||
Left(UIO(f.raise(new FutureCancelledException))) | ||
} | ||
} | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
interop-twitter/jvm/src/test/scala/scalaz/zio/interop/TwitterSpec.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,55 @@ | ||
package scalaz.zio.interop | ||
|
||
import java.util.concurrent.atomic.AtomicInteger | ||
|
||
import com.twitter.util.{ Duration => TwitterDuration, Future, JavaTimer } | ||
import org.specs2.concurrent.ExecutionEnv | ||
import scalaz.zio.{ FiberFailure, Task, TestRuntime } | ||
import scalaz.zio.Exit.Cause.Fail | ||
import scalaz.zio.duration.Duration | ||
import scalaz.zio.interop.twitter._ | ||
|
||
import scala.concurrent.duration._ | ||
|
||
class TwitterSpec(implicit ee: ExecutionEnv) extends TestRuntime { | ||
def is = | ||
"Twitter spec".title ^ s2""" | ||
`Task.fromTwitterFuture` must | ||
return failing `Task` if future failed. $propagateFailures | ||
return successful `Task` if future succeeded. $propagateResults | ||
ensure future is interrupted together with task. $propagateInterrupts | ||
""" | ||
|
||
private def propagateFailures = { | ||
val error = new Exception | ||
val future = Task(Future.exception[Int](error)) | ||
val task = Task.fromTwitterFuture(future) | ||
|
||
unsafeRun(task) must throwAn(FiberFailure(Fail(error))) | ||
} | ||
|
||
private def propagateResults = { | ||
val value = 10 | ||
val future = Task(Future.value(value)) | ||
val task = Task.fromTwitterFuture(future) | ||
|
||
unsafeRun(task) ==== value | ||
} | ||
|
||
private def propagateInterrupts = { | ||
implicit val timer = new JavaTimer(true) | ||
|
||
val value = new AtomicInteger(0) | ||
val futureDelay = TwitterDuration.fromMilliseconds(300) | ||
val future = Task(Future.sleep(futureDelay).map(_ => value.incrementAndGet())) | ||
|
||
val taskTimeout = Duration.fromScala(100.millis) | ||
val task = Task.fromTwitterFuture(future).timeout(taskTimeout) | ||
|
||
unsafeRun(task) must beNone | ||
|
||
MILLISECONDS.sleep(500) | ||
|
||
value.get() ==== 0 | ||
} | ||
} |
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
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