blob: ad95ceacb539aef0b8774cb3266f61ea783b3faa (
plain) (
tree)
|
|
/**
* Copyright (C) 2012 Typesafe Inc. <http://www.typesafe.com>
*/
import language.{ reflectiveCalls, postfixOps }
import scala.concurrent.{ Future, ExecutionContext, future, Await }
import scala.concurrent.duration._
import scala.async.Async.{ async, await }
object Test extends App {
IfElse3Spec.check()
}
class TestIfElse3Class {
import ExecutionContext.Implicits.global
def base(x: Int): Future[Int] = future {
Thread.sleep(1000)
x + 2
}
def m(y: Int): Future[Int] = async {
val f = base(y)
var z = 0
if (y > 0) {
val x1 = await(f)
var w = x1 + 2
z = w + 2
} else {
val x2 = await(f)
var w = x2 + 2
z = w - 2
}
z
}
}
object IfElse3Spec extends MinimalScalaTest {
"An async method" should {
"allow variables of the same name in different blocks" in {
val o = new TestIfElse3Class
val fut = o.m(10)
val res = Await.result(fut, 2 seconds)
res mustBe(16)
}
}
}
|