summaryrefslogtreecommitdiff
path: root/test/files/jvm/actmig-instantiation.scala
blob: d54dff955850b8b4b7de6b59fbd3a1b2daac214b (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
/**
 * 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.migration.MigrationSystem._
import scala.actors.migration._
import scala.actors.Actor._
import scala.actors._
import java.util.concurrent.{ TimeUnit, CountDownLatch }
import scala.collection.mutable.ArrayBuffer

class TestStashingActor extends StashingActor {

  def receive = { case v: Int => Test.append(v); Test.latch.countDown() }

}

object Test {
  val NUMBER_OF_TESTS = 5

  // used for sorting non-deterministic output
  val buff = ArrayBuffer[Int](0)
  val latch = new CountDownLatch(NUMBER_OF_TESTS)
  val toStop = ArrayBuffer[ActorRef]()

  def append(v: Int) = synchronized {
    buff += v
  }

  def main(args: Array[String]) = {
    // plain scala actor
    val a1 = actor {
      react { case v: Int => Test.append(v); Test.latch.countDown() }
    }
    a1 ! 100

    // simple instantiation
    val a2 = MigrationSystem.actorOf(Props(() => new TestStashingActor, "akka.actor.default-stash-dispatcher"))
    a2 ! 200
    toStop += a2

    // actor of with scala actor
    val a3 = MigrationSystem.actorOf(Props(() => actor {
      react { case v: Int => Test.append(v); Test.latch.countDown() }
    }, "akka.actor.default-stash-dispatcher"))
    a3 ! 300

    // using the manifest
    val a4 = MigrationSystem.actorOf(Props(() => new TestStashingActor, "akka.actor.default-stash-dispatcher"))
    a4 ! 400
    toStop += a4

    // deterministic part of a test
    // creation without actorOf
    try {
      val a3 = new TestStashingActor
      a3 ! -1
    } catch {
      case e => println("OK error: " + e)
    }

    // actorOf double creation
    try {
      val a3 = MigrationSystem.actorOf(Props(() => {
        new TestStashingActor
        new TestStashingActor
      }, "akka.actor.default-stash-dispatcher"))
      a3 ! -1
    } catch {
      case e => println("OK error: " + e)
    }

    // actorOf nesting
    try {
      val a5 = MigrationSystem.actorOf(Props(() => {
        val a6 = MigrationSystem.actorOf(Props(() => new TestStashingActor, "akka.actor.default-stash-dispatcher"))
        toStop += a6
        new TestStashingActor
      }, "akka.actor.default-stash-dispatcher"))

      a5 ! 500
      toStop += a5
    } catch {
      case e => println("Should not throw an exception: " + e)
    }

    // output
    latch.await(5, TimeUnit.SECONDS)
    if (latch.getCount() > 0) {
      println("Error: Tasks have not finished!!!")
    }

    buff.sorted.foreach(println)
    toStop.foreach(_ ! PoisonPill)
  }
}