Skip to content

Commit b906b35

Browse files
authored
Merge pull request #188 from NthPortal/future-fixes
Remove uses of deprecated Future APIs
2 parents e5cde43 + 58e7e33 commit b906b35

File tree

13 files changed

+66
-57
lines changed

13 files changed

+66
-57
lines changed

src/test/scala/scala/async/run/anf/AnfTransformSpec.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ package run
77
package anf
88

99
import language.{reflectiveCalls, postfixOps}
10-
import scala.concurrent.{Future, ExecutionContext, future, Await}
10+
import scala.concurrent.{Future, ExecutionContext, Await}
1111
import scala.concurrent.duration._
1212
import scala.async.Async.{async, await}
1313
import org.junit.Test
@@ -18,7 +18,7 @@ class AnfTestClass {
1818

1919
import ExecutionContext.Implicits.global
2020

21-
def base(x: Int): Future[Int] = future {
21+
def base(x: Int): Future[Int] = Future {
2222
x + 2
2323
}
2424

src/test/scala/scala/async/run/await0/Await0Spec.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ package await0
1212

1313
import language.{reflectiveCalls, postfixOps}
1414

15-
import scala.concurrent.{Future, ExecutionContext, future, Await}
15+
import scala.concurrent.{Future, ExecutionContext, Await}
1616
import scala.concurrent.duration._
1717
import scala.async.Async.{async, await}
1818
import org.junit.Test
@@ -21,23 +21,23 @@ class Await0Class {
2121

2222
import ExecutionContext.Implicits.global
2323

24-
def m1(x: Double): Future[Double] = future {
24+
def m1(x: Double): Future[Double] = Future {
2525
x + 2.0
2626
}
2727

28-
def m2(x: Float): Future[Float] = future {
28+
def m2(x: Float): Future[Float] = Future {
2929
x + 2.0f
3030
}
3131

32-
def m3(x: Char): Future[Char] = future {
32+
def m3(x: Char): Future[Char] = Future {
3333
(x.toInt + 2).toChar
3434
}
3535

36-
def m4(x: Short): Future[Short] = future {
36+
def m4(x: Short): Future[Short] = Future {
3737
(x + 2).toShort
3838
}
3939

40-
def m5(x: Byte): Future[Byte] = future {
40+
def m5(x: Byte): Future[Byte] = Future {
4141
(x + 2).toByte
4242
}
4343

src/test/scala/scala/async/run/block0/AsyncSpec.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ package run
77
package block0
88

99
import language.{reflectiveCalls, postfixOps}
10-
import scala.concurrent.{Future, ExecutionContext, future, Await}
10+
import scala.concurrent.{Future, ExecutionContext, Await}
1111
import scala.concurrent.duration._
1212
import scala.async.Async.{async, await}
1313
import org.junit.Test
@@ -17,7 +17,7 @@ class Test1Class {
1717

1818
import ExecutionContext.Implicits.global
1919

20-
def m1(x: Int): Future[Int] = future {
20+
def m1(x: Int): Future[Int] = Future {
2121
x + 2
2222
}
2323

src/test/scala/scala/async/run/block1/block1.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ package run
77
package block1
88

99
import language.{reflectiveCalls, postfixOps}
10-
import scala.concurrent.{Future, ExecutionContext, future, Await}
10+
import scala.concurrent.{Future, ExecutionContext, Await}
1111
import scala.concurrent.duration._
1212
import scala.async.Async.{async, await}
1313
import org.junit.Test
@@ -17,7 +17,7 @@ class Test1Class {
1717

1818
import ExecutionContext.Implicits.global
1919

20-
def m1(x: Int): Future[Int] = future {
20+
def m1(x: Int): Future[Int] = Future {
2121
x + 2
2222
}
2323

src/test/scala/scala/async/run/exceptions/ExceptionsSpec.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ package exceptions
88

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

11-
import scala.concurrent.{future, ExecutionContext, Await}
11+
import scala.concurrent.{Future, ExecutionContext, Await}
1212
import ExecutionContext.Implicits._
1313
import scala.concurrent.duration._
1414
import scala.reflect.ClassTag
@@ -25,7 +25,7 @@ class ExceptionsSpec {
2525

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

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

4646
@Test
4747
def `await failing future within async after await`() {
48-
val base = future[Any] { "five!".length }
48+
val base = Future[Any] { "five!".length }
4949
val fut = async {
5050
val a = await(base.mapTo[Int]) // result: 5
51-
val b = await((future { (a * 2).toString }).mapTo[Int]) // result: ClassCastException
52-
val c = await(future { (7 * 2).toString }) // result: "14"
51+
val b = await((Future { (a * 2).toString }).mapTo[Int]) // result: ClassCastException
52+
val c = await(Future { (7 * 2).toString }) // result: "14"
5353
b + "-" + c
5454
}
5555
intercept[ClassCastException] { Await.result(fut, 2.seconds) }

src/test/scala/scala/async/run/futures/FutureSpec.scala

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class FutureSpec {
2424
/* some utils */
2525

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

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

45-
val f1 = future[Any] {
45+
val f1 = Future[Any] {
4646
throw new ThrowableTest("test")
4747
}
4848

@@ -51,7 +51,7 @@ class FutureSpec {
5151
}
5252

5353
val latch = new TestLatch
54-
val f2 = future {
54+
val f2 = Future {
5555
Await.ready(latch, 5 seconds)
5656
"success"
5757
}
@@ -72,7 +72,7 @@ class FutureSpec {
7272

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

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

89-
def asyncInt(x: Int) = future { (x * 2).toString }
90-
val future0 = future[Any] {
89+
def asyncInt(x: Int) = Future { (x * 2).toString }
90+
val future0 = Future[Any] {
9191
"five!".length
9292
}
9393

@@ -100,8 +100,8 @@ class FutureSpec {
100100

101101
val future2 = async {
102102
val a = await(future0.mapTo[Int])
103-
val b = await((future { (a * 2).toString }).mapTo[Int])
104-
val c = await(future { (7 * 2).toString })
103+
val b = await((Future { (a * 2).toString }).mapTo[Int])
104+
val c = await(Future { (7 * 2).toString })
105105
b + "-" + c
106106
}
107107

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

122122
val future1 = for {
@@ -217,7 +217,7 @@ class FutureSpec {
217217
@Test def `andThen like a boss`() {
218218
val q = new java.util.concurrent.LinkedBlockingQueue[Int]
219219
for (i <- 1 to 1000) {
220-
val chained = future {
220+
val chained = Future {
221221
q.add(1); 3
222222
} andThen {
223223
case _ => q.add(2)
@@ -244,7 +244,7 @@ class FutureSpec {
244244
}
245245

246246
@Test def `find`() {
247-
val futures = for (i <- 1 to 10) yield future {
247+
val futures = for (i <- 1 to 10) yield Future {
248248
i
249249
}
250250

@@ -279,27 +279,29 @@ class FutureSpec {
279279

280280
@Test def `fold`() {
281281
val timeout = 10000 millis
282-
def async(add: Int, wait: Int) = future {
282+
def async(add: Int, wait: Int) = Future {
283283
Thread.sleep(wait)
284284
add
285285
}
286286

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

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

300302
@Test def `fold by composing`() {
301303
val timeout = 10000 millis
302-
def async(add: Int, wait: Int) = future {
304+
def async(add: Int, wait: Int) = Future {
303305
Thread.sleep(wait)
304306
add
305307
}
@@ -314,14 +316,15 @@ class FutureSpec {
314316

315317
@Test def `fold with an exception`() {
316318
val timeout = 10000 millis
317-
def async(add: Int, wait: Int) = future {
319+
def async(add: Int, wait: Int) = Future {
318320
Thread.sleep(wait)
319321
if (add == 6) throw new IllegalArgumentException("shouldFoldResultsWithException: expected")
320322
add
321323
}
322324
def futures = (0 to 9) map {
323325
idx => async(idx, idx * 10)
324326
}
327+
// TODO: change to `foldLeft` after support for 2.11 is dropped
325328
val folded = Future.fold(futures)(0)(_ + _)
326329
intercept[IllegalArgumentException] {
327330
Await.result(folded, timeout)
@@ -332,6 +335,7 @@ class FutureSpec {
332335
import scala.collection.mutable.ArrayBuffer
333336
def test(testNumber: Int) {
334337
val fs = (0 to 1000) map (i => Future(i))
338+
// TODO: change to `foldLeft` after support for 2.11 is dropped
335339
val f = Future.fold(fs)(ArrayBuffer.empty[AnyRef]) {
336340
case (l, i) if i % 2 == 0 => l += i.asInstanceOf[AnyRef]
337341
case (l, _) => l
@@ -345,28 +349,31 @@ class FutureSpec {
345349
}
346350

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

352357
@Test def `shouldReduceResults`() {
353-
def async(idx: Int) = future {
358+
def async(idx: Int) = Future {
354359
Thread.sleep(idx * 20)
355360
idx
356361
}
357362
val timeout = 10000 millis
358363

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

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

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

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

400-
val oddFutures = List.fill(100)(future { counter.incAndGet() }).iterator
409+
val oddFutures = List.fill(100)(Future { counter.incAndGet() }).iterator
401410
val traversed = Future.sequence(oddFutures)
402411
Await.result(traversed, defaultTimeout).sum mustBe (10000)
403412

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

416-
val f = future {
425+
val f = Future {
417426
Await.ready(latch, 5 seconds)
418427
5
419428
}
420-
val f2 = future {
429+
val f2 = Future {
421430
val res = Await.result(f, Inf)
422431
res + 9
423432
}
@@ -430,7 +439,7 @@ class FutureSpec {
430439

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

433-
val f3 = future {
442+
val f3 = Future {
434443
Thread.sleep(100)
435444
5
436445
}
@@ -443,7 +452,7 @@ class FutureSpec {
443452
@Test def `run callbacks async`() {
444453
val latch = Vector.fill(10)(new TestLatch)
445454

446-
val f1 = future {
455+
val f1 = Future {
447456
latch(0).open()
448457
Await.ready(latch(1), TestLatch.DefaultTimeout)
449458
"Hello"
@@ -535,7 +544,7 @@ class FutureSpec {
535544

536545
@Test def `should not throw when Await.ready`() {
537546
val expected = try Success(5 / 0) catch { case a: ArithmeticException => Failure(a) }
538-
val f = async { await(future(5)) / 0 }
547+
val f = async { await(Future(5)) / 0 }
539548
Await.ready(f, defaultTimeout).value.get.toString mustBe expected.toString
540549
}
541550
}

src/test/scala/scala/async/run/ifelse0/IfElse0.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ package run
77
package ifelse0
88

99
import language.{reflectiveCalls, postfixOps}
10-
import scala.concurrent.{Future, ExecutionContext, future, Await}
10+
import scala.concurrent.{Future, ExecutionContext, Await}
1111
import scala.concurrent.duration._
1212
import scala.async.Async.{async, await}
1313
import org.junit.Test
@@ -18,7 +18,7 @@ class TestIfElseClass {
1818

1919
import ExecutionContext.Implicits.global
2020

21-
def m1(x: Int): Future[Int] = future {
21+
def m1(x: Int): Future[Int] = Future {
2222
x + 2
2323
}
2424

0 commit comments

Comments
 (0)