aboutsummaryrefslogtreecommitdiff
path: root/pending/run/fallback0/fallback0-manual.scala
blob: 9bc570d8f238db3c1ebf04ed80954eb2dfc29196 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
 * Copyright (C) 2012 Typesafe Inc. <http://www.typesafe.com>
 */

import language.{ reflectiveCalls, postfixOps }
import scala.concurrent.{ Future, ExecutionContext, future, Await, Promise }
import scala.concurrent.duration._
import scala.async.EndTaskException
import scala.async.Async.{ async, await, awaitCps }
import scala.util.continuations.reset

object TestManual extends App {

  Fallback0ManualSpec.check()

}

class TestFallback0ManualClass {
  import ExecutionContext.Implicits.global
  
  def m1(x: Int): Future[Int] = future {
    x + 2
  }
  
  def m2(y: Int): Future[Int] = {
    val p = Promise[Int]()
    future { reset {
      val f = m1(y)
      var z = 0
      val res = awaitCps(f, p) + 5
      if (res > 0) {
        z = 2
      } else {
        z = 4
      }
      z
    } }
    p.future
  }
  
  /* that isn't even supported by current CPS plugin
  def m3(y: Int): Future[Int] = {
    val p = Promise[Int]()
    future { reset {
      val f = m1(y)
      var z = 0
      val res: Option[Int] = Some(5)
      res match {
        case None    => z = 4
        case Some(a) => z = awaitCps(f, p) - 10
      }
      z
    } }
    p.future
  }
  */
}


object Fallback0ManualSpec extends MinimalScalaTest {

  "An async method" should {
    "support await in a simple if-else expression" in {
      val o = new TestFallback0ManualClass
      val fut = o.m2(10)
      val res = Await.result(fut, 2 seconds)
      res mustBe(2)
    }
  }

}