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
116
117
118
119
120
121
122
|
import scala.concurrent._
object Test extends App {
def once(body: (() => Unit) => Unit) {
val sv = new SyncVar[Boolean]
body(() => sv put true)
sv.take()
}
def output(num: Int, msg: String) {
println("test" + num + ": " + msg)
}
def testOnSuccess(): Unit = once {
done =>
val f = future {
output(1, "hai world")
}
f onSuccess { case _ =>
output(1, "kthxbye")
done()
}
}
def testOnSuccessWhenCompleted(): Unit = once {
done =>
val f = future {
output(2, "hai world")
}
f onSuccess { case _ =>
output(2, "awsum thx")
f onSuccess { case _ =>
output(2, "kthxbye")
done()
}
}
}
def testOnSuccessWhenFailed(): Unit = once {
done =>
val f = future[Unit] {
output(3, "hai world")
done()
throw new Exception
}
f onSuccess { case _ =>
output(3, "onoes")
}
}
def testOnFailure(): Unit = once {
done =>
val f = future[Unit] {
output(4, "hai world")
throw new Exception
}
f onSuccess { case _ =>
output(4, "onoes")
done()
}
f onFailure { case _ =>
output(4, "kthxbye")
done()
}
}
def testOnFailureWhenSpecialThrowable(num: Int, cause: Throwable): Unit = once {
done =>
val f = future[Unit] {
output(num, "hai world")
throw cause
}
f onSuccess { case _ =>
output(num, "onoes")
done()
}
f onFailure {
case e: ExecutionException if (e.getCause == cause) =>
output(num, "kthxbye")
done()
case _ =>
output(num, "onoes")
done()
}
}
// def testOnFailureWhenFutureTimeoutException(): Unit = once {
// done =>
// val f = future[Unit] {
// output(8, "hai world")
// throw new FutureTimeoutException(null)
// }
// f onSuccess { case _ =>
// output(8, "onoes")
// done()
// }
// f onFailure {
// case e: FutureTimeoutException =>
// output(8, "im in yr loop")
// done()
// case other =>
// output(8, "onoes: " + other)
// done()
// }
// }
testOnSuccess()
testOnSuccessWhenCompleted()
testOnSuccessWhenFailed()
testOnFailure()
testOnFailureWhenSpecialThrowable(5, new Error)
testOnFailureWhenSpecialThrowable(6, new scala.util.control.ControlThrowable { })
testOnFailureWhenSpecialThrowable(7, new InterruptedException)
// testOnFailureWhenFutureTimeoutException()
}
|