aboutsummaryrefslogtreecommitdiff
path: root/src/test/scala/scala/async/run/await0/Await0Spec.scala
blob: 7cc4095f7002827239e0040466e7cb96ff9eb4c8 (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
72
73
/*
 * Copyright (C) 2012-2014 Lightbend Inc. <http://www.lightbend.com>
 */

package scala.async
package run
package await0

/**
 * Copyright (C) 2012-2014 Lightbend Inc. <http://www.lightbend.com>
 */

import language.{reflectiveCalls, postfixOps}

import scala.concurrent.{Future, ExecutionContext, future, Await}
import scala.concurrent.duration._
import scala.async.Async.{async, await}
import org.junit.Test

class Await0Class {

  import ExecutionContext.Implicits.global

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

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

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

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

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

  def m0(y: Int): Future[Double] = async {
    val f1 = m1(y.toDouble)
    val x1: Double = await(f1)

    val f2 = m2(y.toFloat)
    val x2: Float = await(f2)

    val f3 = m3(y.toChar)
    val x3: Char = await(f3)

    val f4 = m4(y.toShort)
    val x4: Short = await(f4)

    val f5 = m5(y.toByte)
    val x5: Byte = await(f5)

    x1 + x2 + 2.0
  }
}

class Await0Spec {

  @Test
  def `An async method support a simple await`() {
    val o = new Await0Class
    val fut = o.m0(10)
    val res = Await.result(fut, 10 seconds)
    res mustBe (26.0)
  }
}