summaryrefslogtreecommitdiff
path: root/test/files/jvm/actmig-react-receive.scala
blob: bf70ce0c4638d691a35d2e6b939accd3aade92a9 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**
 * NOTE: Code snippets from this test are included in the Actor Migration Guide. In case you change
 * code in these tests prior to the 2.10.0 release please send the notification to @vjovanov.
 */
import scala.actors.Actor._
import scala.actors._
import scala.actors.migration._
import java.util.concurrent.{ TimeUnit, CountDownLatch }
import scala.collection.mutable.ArrayBuffer
import scala.concurrent.duration._
import scala.concurrent.{ Promise, Await }

object Test {
  val finishedRS, finishedRS1, finishedRSC, finishedRSC1 = Promise[Boolean]
  def testComposition() = {
    // Snippet showing composition of receives
    // React Snippet - before
    val myActor = actor {
      // do before
      println("do before")
      receive {
        case x: Int =>
          // do task
          println("do task")
      }
      println("do in between")
      receive {
        case x: String =>
          // do string now
          println("do string")
      }
      println("do after")
      finishedRSC1.success(true)
    }
    myActor.start()
    myActor ! 1
    myActor ! "1"
    Await.ready(finishedRSC1.future, 5 seconds)

    // React Snippet - migrated
    val myAkkaActor = ActorDSL.actor(new StashingActor {
      override def preStart() = {
        println("do before")
      }

      def receive = ({
        case x: Int =>
          // do task
          println("do task")
      }: Receive) andThen { v =>
        context.become {
          case x: String =>
            //do string
            println("do string")
            context.stop(self)
        }
        println("do in between")
      }

      override def postStop() = {
        println("do after")
        finishedRSC.success(true)
      }

    })
    myAkkaActor ! 1
    myAkkaActor ! "1"
    Await.ready(finishedRSC.future, 5 seconds)
  }

  def main(args: Array[String]) = {
    // React Snippet - before
    val myActor = actor {
      // do before
      println("do before")
      receive {
        case x: Int =>
          // do task
          println("do task")
      }
      println("do after")
      finishedRS1.success(true)
    }
    myActor.start()
    myActor ! 1

    Await.ready(finishedRS1.future, 5 seconds)

    // React Snippet - migrated
    val myAkkaActor = ActorDSL.actor(new StashingActor {
      override def preStart() = {
        println("do before")
      }

      def receive = {
        case x: Int =>
          // do task
          println("do task")
          context.stop(self)
      }

      override def postStop() = {
        println("do after")
        finishedRS.success(true)
      }

    })
    myAkkaActor ! 1

    Await.ready(finishedRS.future, 5 seconds)
    // Starting composition test
    testComposition()

  }
}