Use Future.successful/failed instead of Promise.s/f.future

This commit is contained in:
Evgeny Veretennikov 2017-12-03 16:45:38 +03:00
parent 20618844ec
commit 5e5a5d2c21
5 changed files with 32 additions and 32 deletions

View file

@ -114,7 +114,7 @@ object TypedActorSpec {
def pigdog = "Pigdog"
def futurePigdog(): Future[String] = Promise.successful(pigdog).future
def futurePigdog(): Future[String] = Future.successful(pigdog)
def futurePigdog(delay: FiniteDuration): Future[String] = {
Thread.sleep(delay.toMillis)
@ -123,7 +123,7 @@ object TypedActorSpec {
def futurePigdog(delay: FiniteDuration, numbered: Int): Future[String] = {
Thread.sleep(delay.toMillis)
Promise.successful(pigdog + numbered).future
Future.successful(pigdog + numbered)
}
def futureComposePigdogFrom(foo: Foo): Future[String] = {

View file

@ -84,10 +84,10 @@ class FutureSpec extends AkkaSpec with Checkers with BeforeAndAfterAll with Defa
"never completed" must {
behave like emptyFuture(_(Promise().future))
"return supplied value on timeout" in {
val failure = Promise.failed[String](new RuntimeException("br0ken")).future
val otherFailure = Promise.failed[String](new RuntimeException("last")).future
val failure = Future.failed(new RuntimeException("br0ken"))
val otherFailure = Future.failed(new RuntimeException("last"))
val empty = Promise[String]().future
val timedOut = Promise.successful[String]("Timedout").future
val timedOut = Future.successful("Timedout")
Await.result(failure fallbackTo timedOut, timeout.duration) should ===("Timedout")
Await.result(timedOut fallbackTo empty, timeout.duration) should ===("Timedout")
@ -99,22 +99,22 @@ class FutureSpec extends AkkaSpec with Checkers with BeforeAndAfterAll with Defa
}
"completed with a result" must {
val result = "test value"
val future = Promise[String]().complete(Success(result)).future
val future = Future.successful(result)
behave like futureWithResult(_(future, result))
}
"completed with an exception" must {
val message = "Expected Exception"
val future = Promise[String]().complete(Failure(new RuntimeException(message))).future
val future = Future.failed(new RuntimeException(message))
behave like futureWithException[RuntimeException](_(future, message))
}
"completed with an InterruptedException" must {
val message = "Boxed InterruptedException"
val future = Promise[String]().complete(Failure(new InterruptedException(message))).future
val future = Future.failed(new InterruptedException(message))
behave like futureWithException[RuntimeException](_(future, message))
}
"completed with a NonLocalReturnControl" must {
val result = "test value"
val future = Promise[String]().complete(Failure(new NonLocalReturnControl[String]("test", result))).future
val future = Future.failed(new NonLocalReturnControl("test", result))
behave like futureWithResult(_(future, result))
}
@ -366,16 +366,16 @@ class FutureSpec extends AkkaSpec with Checkers with BeforeAndAfterAll with Defa
"recoverWith from exceptions" in {
val o = new IllegalStateException("original")
val r = new IllegalStateException("recovered")
val yay = Promise.successful("yay!").future
val yay = Future.successful("yay!")
intercept[IllegalStateException] {
Await.result(Promise.failed[String](o).future recoverWith { case _ if false == true yay }, timeout.duration)
Await.result(Future.failed(o) recoverWith { case _ if false == true yay }, timeout.duration)
} should ===(o)
Await.result(Promise.failed[String](o).future recoverWith { case _ yay }, timeout.duration) should ===("yay!")
Await.result(Future.failed(o) recoverWith { case _ yay }, timeout.duration) should ===("yay!")
intercept[IllegalStateException] {
Await.result(Promise.failed[String](o).future recoverWith { case _ Promise.failed[String](r).future }, timeout.duration)
Await.result(Future.failed(o) recoverWith { case _ Future.failed(r) }, timeout.duration)
} should ===(r)
}
@ -391,7 +391,7 @@ class FutureSpec extends AkkaSpec with Checkers with BeforeAndAfterAll with Defa
}
"firstCompletedOf" in {
val futures = Vector.fill[Future[Int]](10)(Promise[Int]().future) :+ Promise.successful[Int](5).future
val futures = Vector.fill[Future[Int]](10)(Promise[Int]().future) :+ Future.successful(5)
Await.result(Future.firstCompletedOf(futures), timeout.duration) should ===(5)
}
@ -412,18 +412,18 @@ class FutureSpec extends AkkaSpec with Checkers with BeforeAndAfterAll with Defa
val timeout = 10000 millis
val f = new IllegalStateException("test")
intercept[IllegalStateException] {
Await.result(Promise.failed[String](f).future zip Promise.successful("foo").future, timeout)
Await.result(Future.failed(f) zip Future.successful("foo"), timeout)
} should ===(f)
intercept[IllegalStateException] {
Await.result(Promise.successful("foo").future zip Promise.failed[String](f).future, timeout)
Await.result(Future.successful("foo") zip Future.failed(f), timeout)
} should ===(f)
intercept[IllegalStateException] {
Await.result(Promise.failed[String](f).future zip Promise.failed[String](f).future, timeout)
Await.result(Future.failed(f) zip Future.failed(f), timeout)
} should ===(f)
Await.result(Promise.successful("foo").future zip Promise.successful("foo").future, timeout) should ===(("foo", "foo"))
Await.result(Future.successful("foo") zip Future.successful("foo"), timeout) should ===(("foo", "foo"))
}
"fold by composing" in {
@ -655,7 +655,7 @@ class FutureSpec extends AkkaSpec with Checkers with BeforeAndAfterAll with Defa
"transform result with map" in { f((future, result) Await.result((future map (_.toString.length)), timeout.duration) should ===(result.toString.length)) }
"compose result with flatMap" in {
f { (future, result)
val r = for (r future; p Promise.successful("foo").future) yield r.toString + p
val r = for (r future; p Future.successful("foo")) yield r.toString + p
Await.result(r, timeout.duration) should ===(result.toString + "foo")
}
}
@ -668,8 +668,8 @@ class FutureSpec extends AkkaSpec with Checkers with BeforeAndAfterAll with Defa
}
"zip properly" in {
f { (future, result)
Await.result(future zip Promise.successful("foo").future, timeout.duration) should ===((result, "foo"))
(intercept[RuntimeException] { Await.result(future zip Promise.failed(new RuntimeException("ohnoes")).future, timeout.duration) }).getMessage should ===("ohnoes")
Await.result(future zip Future.successful("foo"), timeout.duration) should ===((result, "foo"))
(intercept[RuntimeException] { Await.result(future zip Future.failed(new RuntimeException("ohnoes")), timeout.duration) }).getMessage should ===("ohnoes")
}
}
"not recover from exception" in { f((future, result) Await.result(future.recover({ case _ "pigdog" }), timeout.duration) should ===(result)) }
@ -704,11 +704,11 @@ class FutureSpec extends AkkaSpec with Checkers with BeforeAndAfterAll with Defa
}
}
"retain exception with map" in { f((future, message) (intercept[java.lang.Exception] { Await.result(future map (_.toString.length), timeout.duration) }).getMessage should ===(message)) }
"retain exception with flatMap" in { f((future, message) (intercept[java.lang.Exception] { Await.result(future flatMap (_ Promise.successful[Any]("foo").future), timeout.duration) }).getMessage should ===(message)) }
"retain exception with flatMap" in { f((future, message) (intercept[java.lang.Exception] { Await.result(future flatMap (_ Future.successful("foo")), timeout.duration) }).getMessage should ===(message)) }
"not perform action with foreach" is pending
"zip properly" in {
f { (future, message) (intercept[java.lang.Exception] { Await.result(future zip Promise.successful("foo").future, timeout.duration) }).getMessage should ===(message) }
f { (future, message) (intercept[java.lang.Exception] { Await.result(future zip Future.successful("foo"), timeout.duration) }).getMessage should ===(message) }
}
"recover from exception" in { f((future, message) Await.result(future.recover({ case e if e.getMessage == message "pigdog" }), timeout.duration) should ===("pigdog")) }
"not perform action on result" is pending

View file

@ -51,7 +51,7 @@ class PatternSpec extends AkkaSpec("akka.actor.serialize-messages = off") {
"pattern.after" must {
"be completed successfully eventually" in {
// TODO after is unfortunately shadowed by ScalaTest, fix as part of #3759
val f = akka.pattern.after(1 second, using = system.scheduler)(Promise.successful(5).future)
val f = akka.pattern.after(1 second, using = system.scheduler)(Future.successful(5))
val r = Future.firstCompletedOf(Seq(Promise[Int]().future, f))
Await.result(r, remainingOrDefault) should ===(5)
@ -59,7 +59,7 @@ class PatternSpec extends AkkaSpec("akka.actor.serialize-messages = off") {
"be completed abnormally eventually" in {
// TODO after is unfortunately shadowed by ScalaTest, fix as part of #3759
val f = akka.pattern.after(1 second, using = system.scheduler)(Promise.failed(new IllegalStateException("Mexico")).future)
val f = akka.pattern.after(1 second, using = system.scheduler)(Future.failed(new IllegalStateException("Mexico")))
val r = Future.firstCompletedOf(Seq(Promise[Int]().future, f))
intercept[IllegalStateException] { Await.result(r, remainingOrDefault) }.getMessage should ===("Mexico")

View file

@ -852,7 +852,7 @@ class CircuitBreaker(
callThrough(body, defineFailureFn)
else {
notifyCallBreakerOpenListeners()
Promise.failed[T](new CircuitBreakerOpenException(0.seconds)).future
Future.failed[T](new CircuitBreakerOpenException(0.seconds))
}
/**
@ -897,7 +897,7 @@ class CircuitBreaker(
*/
override def invoke[T](body: Future[T], defineFailureFn: Try[T] Boolean): Future[T] = {
notifyCallBreakerOpenListeners()
Promise.failed[T](new CircuitBreakerOpenException(remainingDuration())).future
Future.failed(new CircuitBreakerOpenException(remainingDuration()))
}
/**

View file

@ -2,7 +2,7 @@ package akka.remote.transport
import akka.testkit.{ DefaultTimeout, AkkaSpec }
import akka.remote.transport.TestTransport.SwitchableLoggedBehavior
import scala.concurrent.{ Await, Promise }
import scala.concurrent.{ Await, Future, Promise }
import scala.util.Failure
import akka.AkkaException
import scala.util.control.NoStackTrace
@ -14,7 +14,7 @@ object SwitchableLoggedBehaviorSpec {
class SwitchableLoggedBehaviorSpec extends AkkaSpec with DefaultTimeout {
import akka.remote.transport.SwitchableLoggedBehaviorSpec._
private def defaultBehavior = new SwitchableLoggedBehavior[Unit, Int]((_) Promise.successful(3).future, (_) ())
private def defaultBehavior = new SwitchableLoggedBehavior[Unit, Int]((_) Future.successful(3), (_) ())
"A SwitchableLoggedBehavior" must {
@ -27,10 +27,10 @@ class SwitchableLoggedBehaviorSpec extends AkkaSpec with DefaultTimeout {
"be able to push generic behavior" in {
val behavior = defaultBehavior
behavior.push((_) Promise.successful(4).future)
behavior.push((_) Future.successful(4))
Await.result(behavior(()), timeout.duration) should ===(4)
behavior.push((_) Promise.failed(TestException).future)
behavior.push((_) Future.failed(TestException))
behavior(()).value match {
case Some(Failure(`TestException`))
case _ fail("Expected exception")
@ -94,7 +94,7 @@ class SwitchableLoggedBehaviorSpec extends AkkaSpec with DefaultTimeout {
"log calls and parametrers" in {
val logPromise = Promise[Int]()
val behavior = new SwitchableLoggedBehavior[Int, Int]((i) Promise.successful(3).future, (i) logPromise.success(i))
val behavior = new SwitchableLoggedBehavior[Int, Int]((i) Future.successful(3), (i) logPromise.success(i))
behavior(11)
Await.result(logPromise.future, timeout.duration) should ===(11)