Skip to content

Remove uses of deprecated Future APIs #188

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 1 commit into from
Feb 27, 2018
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
4 changes: 2 additions & 2 deletions src/test/scala/scala/async/run/anf/AnfTransformSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package run
package anf

import language.{reflectiveCalls, postfixOps}
import scala.concurrent.{Future, ExecutionContext, future, Await}
import scala.concurrent.{Future, ExecutionContext, Await}
import scala.concurrent.duration._
import scala.async.Async.{async, await}
import org.junit.Test
Expand All @@ -18,7 +18,7 @@ class AnfTestClass {

import ExecutionContext.Implicits.global

def base(x: Int): Future[Int] = future {
def base(x: Int): Future[Int] = Future {
x + 2
}

Expand Down
12 changes: 6 additions & 6 deletions src/test/scala/scala/async/run/await0/Await0Spec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ package await0

import language.{reflectiveCalls, postfixOps}

import scala.concurrent.{Future, ExecutionContext, future, Await}
import scala.concurrent.{Future, ExecutionContext, Await}
import scala.concurrent.duration._
import scala.async.Async.{async, await}
import org.junit.Test
Expand All @@ -21,23 +21,23 @@ class Await0Class {

import ExecutionContext.Implicits.global

def m1(x: Double): Future[Double] = future {
def m1(x: Double): Future[Double] = Future {
x + 2.0
}

def m2(x: Float): Future[Float] = future {
def m2(x: Float): Future[Float] = Future {
x + 2.0f
}

def m3(x: Char): Future[Char] = future {
def m3(x: Char): Future[Char] = Future {
(x.toInt + 2).toChar
}

def m4(x: Short): Future[Short] = future {
def m4(x: Short): Future[Short] = Future {
(x + 2).toShort
}

def m5(x: Byte): Future[Byte] = future {
def m5(x: Byte): Future[Byte] = Future {
(x + 2).toByte
}

Expand Down
4 changes: 2 additions & 2 deletions src/test/scala/scala/async/run/block0/AsyncSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package run
package block0

import language.{reflectiveCalls, postfixOps}
import scala.concurrent.{Future, ExecutionContext, future, Await}
import scala.concurrent.{Future, ExecutionContext, Await}
import scala.concurrent.duration._
import scala.async.Async.{async, await}
import org.junit.Test
Expand All @@ -17,7 +17,7 @@ class Test1Class {

import ExecutionContext.Implicits.global

def m1(x: Int): Future[Int] = future {
def m1(x: Int): Future[Int] = Future {
x + 2
}

Expand Down
4 changes: 2 additions & 2 deletions src/test/scala/scala/async/run/block1/block1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package run
package block1

import language.{reflectiveCalls, postfixOps}
import scala.concurrent.{Future, ExecutionContext, future, Await}
import scala.concurrent.{Future, ExecutionContext, Await}
import scala.concurrent.duration._
import scala.async.Async.{async, await}
import org.junit.Test
Expand All @@ -17,7 +17,7 @@ class Test1Class {

import ExecutionContext.Implicits.global

def m1(x: Int): Future[Int] = future {
def m1(x: Int): Future[Int] = Future {
x + 2
}

Expand Down
12 changes: 6 additions & 6 deletions src/test/scala/scala/async/run/exceptions/ExceptionsSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package exceptions

import scala.async.Async.{async, await}

import scala.concurrent.{future, ExecutionContext, Await}
import scala.concurrent.{Future, ExecutionContext, Await}
import ExecutionContext.Implicits._
import scala.concurrent.duration._
import scala.reflect.ClassTag
Expand All @@ -25,7 +25,7 @@ class ExceptionsSpec {

@Test
def `uncaught exception within async after await`() {
val base = future { "five!".length }
val base = Future { "five!".length }
val fut = async {
val len = await(base)
throw new Exception(s"illegal length: $len")
Expand All @@ -35,7 +35,7 @@ class ExceptionsSpec {

@Test
def `await failing future within async`() {
val base = future[Int] { throw new Exception("problem") }
val base = Future[Int] { throw new Exception("problem") }
val fut = async {
val x = await(base)
x * 2
Expand All @@ -45,11 +45,11 @@ class ExceptionsSpec {

@Test
def `await failing future within async after await`() {
val base = future[Any] { "five!".length }
val base = Future[Any] { "five!".length }
val fut = async {
val a = await(base.mapTo[Int]) // result: 5
val b = await((future { (a * 2).toString }).mapTo[Int]) // result: ClassCastException
val c = await(future { (7 * 2).toString }) // result: "14"
val b = await((Future { (a * 2).toString }).mapTo[Int]) // result: ClassCastException
val c = await(Future { (7 * 2).toString }) // result: "14"
b + "-" + c
}
intercept[ClassCastException] { Await.result(fut, 2.seconds) }
Expand Down
55 changes: 32 additions & 23 deletions src/test/scala/scala/async/run/futures/FutureSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class FutureSpec {
/* some utils */

def testAsync(s: String)(implicit ec: ExecutionContext): Future[String] = s match {
case "Hello" => future { "World" }
case "Hello" => Future { "World" }
case "Failure" => Future.failed(new RuntimeException("Expected exception; to test fault-tolerance"))
case "NoReply" => Promise[String]().future
}
Expand All @@ -42,7 +42,7 @@ class FutureSpec {

class ThrowableTest(m: String) extends Throwable(m)

val f1 = future[Any] {
val f1 = Future[Any] {
throw new ThrowableTest("test")
}

Expand All @@ -51,7 +51,7 @@ class FutureSpec {
}

val latch = new TestLatch
val f2 = future {
val f2 = Future {
Await.ready(latch, 5 seconds)
"success"
}
Expand All @@ -72,7 +72,7 @@ class FutureSpec {

Await.result(f3, defaultTimeout) mustBe ("SUCCESS")

val waiting = future {
val waiting = Future {
Thread.sleep(1000)
}
Await.ready(waiting, 2000 millis)
Expand All @@ -86,8 +86,8 @@ class FutureSpec {
@Test def `A future with global ExecutionContext should compose with for-comprehensions`() {
import scala.reflect.ClassTag

def asyncInt(x: Int) = future { (x * 2).toString }
val future0 = future[Any] {
def asyncInt(x: Int) = Future { (x * 2).toString }
val future0 = Future[Any] {
"five!".length
}

Expand All @@ -100,8 +100,8 @@ class FutureSpec {

val future2 = async {
val a = await(future0.mapTo[Int])
val b = await((future { (a * 2).toString }).mapTo[Int])
val c = await(future { (7 * 2).toString })
val b = await((Future { (a * 2).toString }).mapTo[Int])
val c = await(Future { (7 * 2).toString })
b + "-" + c
}

Expand All @@ -115,8 +115,8 @@ class FutureSpec {
case class Req[T](req: T)
case class Res[T](res: T)
def asyncReq[T](req: Req[T]) = req match {
case Req(s: String) => future { Res(s.length) }
case Req(i: Int) => future { Res((i * 2).toString) }
case Req(s: String) => Future { Res(s.length) }
case Req(i: Int) => Future { Res((i * 2).toString) }
}

val future1 = for {
Expand Down Expand Up @@ -217,7 +217,7 @@ class FutureSpec {
@Test def `andThen like a boss`() {
val q = new java.util.concurrent.LinkedBlockingQueue[Int]
for (i <- 1 to 1000) {
val chained = future {
val chained = Future {
q.add(1); 3
} andThen {
case _ => q.add(2)
Expand All @@ -244,7 +244,7 @@ class FutureSpec {
}

@Test def `find`() {
val futures = for (i <- 1 to 10) yield future {
val futures = for (i <- 1 to 10) yield Future {
i
}

Expand Down Expand Up @@ -279,27 +279,29 @@ class FutureSpec {

@Test def `fold`() {
val timeout = 10000 millis
def async(add: Int, wait: Int) = future {
def async(add: Int, wait: Int) = Future {
Thread.sleep(wait)
add
}

val futures = (0 to 9) map {
idx => async(idx, idx * 20)
}
// TODO: change to `foldLeft` after support for 2.11 is dropped
val folded = Future.fold(futures)(0)(_ + _)
Await.result(folded, timeout) mustBe (45)

val futuresit = (0 to 9) map {
idx => async(idx, idx * 20)
}
// TODO: change to `foldLeft` after support for 2.11 is dropped
val foldedit = Future.fold(futures)(0)(_ + _)
Await.result(foldedit, timeout) mustBe (45)
}

@Test def `fold by composing`() {
val timeout = 10000 millis
def async(add: Int, wait: Int) = future {
def async(add: Int, wait: Int) = Future {
Thread.sleep(wait)
add
}
Expand All @@ -314,14 +316,15 @@ class FutureSpec {

@Test def `fold with an exception`() {
val timeout = 10000 millis
def async(add: Int, wait: Int) = future {
def async(add: Int, wait: Int) = Future {
Thread.sleep(wait)
if (add == 6) throw new IllegalArgumentException("shouldFoldResultsWithException: expected")
add
}
def futures = (0 to 9) map {
idx => async(idx, idx * 10)
}
// TODO: change to `foldLeft` after support for 2.11 is dropped
val folded = Future.fold(futures)(0)(_ + _)
intercept[IllegalArgumentException] {
Await.result(folded, timeout)
Expand All @@ -332,6 +335,7 @@ class FutureSpec {
import scala.collection.mutable.ArrayBuffer
def test(testNumber: Int) {
val fs = (0 to 1000) map (i => Future(i))
// TODO: change to `foldLeft` after support for 2.11 is dropped
val f = Future.fold(fs)(ArrayBuffer.empty[AnyRef]) {
case (l, i) if i % 2 == 0 => l += i.asInstanceOf[AnyRef]
case (l, _) => l
Expand All @@ -345,28 +349,31 @@ class FutureSpec {
}

@Test def `return zero value if folding empty list`() {
// TODO: change to `foldLeft` after support for 2.11 is dropped
val zero = Future.fold(List[Future[Int]]())(0)(_ + _)
Await.result(zero, defaultTimeout) mustBe (0)
}

@Test def `shouldReduceResults`() {
def async(idx: Int) = future {
def async(idx: Int) = Future {
Thread.sleep(idx * 20)
idx
}
val timeout = 10000 millis

val futures = (0 to 9) map { async }
// TODO: change to `reduceLeft` after support for 2.11 is dropped
val reduced = Future.reduce(futures)(_ + _)
Await.result(reduced, timeout) mustBe (45)

val futuresit = (0 to 9) map { async }
// TODO: change to `reduceLeft` after support for 2.11 is dropped
val reducedit = Future.reduce(futuresit)(_ + _)
Await.result(reducedit, timeout) mustBe (45)
}

@Test def `shouldReduceResultsWithException`() {
def async(add: Int, wait: Int) = future {
def async(add: Int, wait: Int) = Future {
Thread.sleep(wait)
if (add == 6) throw new IllegalArgumentException("shouldFoldResultsWithException: expected")
else add
Expand All @@ -375,6 +382,7 @@ class FutureSpec {
def futures = (1 to 10) map {
idx => async(idx, idx * 10)
}
// TODO: change to `reduceLeft` after support for 2.11 is dropped
val failed = Future.reduce(futures)(_ + _)
intercept[IllegalArgumentException] {
Await.result(failed, timeout)
Expand All @@ -383,6 +391,7 @@ class FutureSpec {

@Test def `shouldReduceThrowNSEEOnEmptyInput`() {
intercept[java.util.NoSuchElementException] {
// TODO: change to `reduceLeft` after support for 2.11 is dropped
val emptyreduced = Future.reduce(List[Future[Int]]())(_ + _)
Await.result(emptyreduced, defaultTimeout)
}
Expand All @@ -397,7 +406,7 @@ class FutureSpec {
}
}

val oddFutures = List.fill(100)(future { counter.incAndGet() }).iterator
val oddFutures = List.fill(100)(Future { counter.incAndGet() }).iterator
val traversed = Future.sequence(oddFutures)
Await.result(traversed, defaultTimeout).sum mustBe (10000)

Expand All @@ -413,11 +422,11 @@ class FutureSpec {
@Test def `shouldBlockUntilResult`() {
val latch = new TestLatch

val f = future {
val f = Future {
Await.ready(latch, 5 seconds)
5
}
val f2 = future {
val f2 = Future {
val res = Await.result(f, Inf)
res + 9
}
Expand All @@ -430,7 +439,7 @@ class FutureSpec {

Await.result(f2, defaultTimeout) mustBe (14)

val f3 = future {
val f3 = Future {
Thread.sleep(100)
5
}
Expand All @@ -443,7 +452,7 @@ class FutureSpec {
@Test def `run callbacks async`() {
val latch = Vector.fill(10)(new TestLatch)

val f1 = future {
val f1 = Future {
latch(0).open()
Await.ready(latch(1), TestLatch.DefaultTimeout)
"Hello"
Expand Down Expand Up @@ -535,7 +544,7 @@ class FutureSpec {

@Test def `should not throw when Await.ready`() {
val expected = try Success(5 / 0) catch { case a: ArithmeticException => Failure(a) }
val f = async { await(future(5)) / 0 }
val f = async { await(Future(5)) / 0 }
Await.ready(f, defaultTimeout).value.get.toString mustBe expected.toString
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/scala/scala/async/run/ifelse0/IfElse0.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package run
package ifelse0

import language.{reflectiveCalls, postfixOps}
import scala.concurrent.{Future, ExecutionContext, future, Await}
import scala.concurrent.{Future, ExecutionContext, Await}
import scala.concurrent.duration._
import scala.async.Async.{async, await}
import org.junit.Test
Expand All @@ -18,7 +18,7 @@ class TestIfElseClass {

import ExecutionContext.Implicits.global

def m1(x: Int): Future[Int] = future {
def m1(x: Int): Future[Int] = Future {
x + 2
}

Expand Down
Loading