blob: 2e3ffc3c300d06879d61e061e137d7ca21142e9e (
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
|
/**
* 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._
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 = ActorDSL.actor(new TestStashingActor)
a2 ! 200
toStop += a2
// actor of with scala actor
val a3 = ActorDSL.actor(actor {
react { case v: Int => Test.append(v); Test.latch.countDown() }
})
a3 ! 300
// using the manifest
val a4 = ActorDSL.actor(new TestStashingActor)
a4 ! 400
toStop += a4
// deterministic part of a test
// creation without actor
try {
val a3 = new TestStashingActor
a3 ! -1
} catch {
case e: Throwable => println("OK error: " + e)
}
// actor double creation
try {
val a3 = ActorDSL.actor({
new TestStashingActor
new TestStashingActor
})
a3 ! -1
} catch {
case e: Throwable => println("OK error: " + e)
}
// actor nesting
try {
val a5 = ActorDSL.actor({
val a6 = ActorDSL.actor(new TestStashingActor)
toStop += a6
new TestStashingActor
})
a5 ! 500
toStop += a5
} catch {
case e: Throwable => 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)
}
}
|