forked from scala/scala
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPromiseTests.scala
244 lines (190 loc) · 7.33 KB
/
PromiseTests.scala
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import scala.concurrent._
import scala.concurrent.util.duration._
import scala.concurrent.util.Duration.Inf
import scala.collection._
import scala.runtime.NonLocalReturnControl
object PromiseTests extends MinimalScalaTest {
val defaultTimeout = Inf
/* promise specification */
"An empty Promise" should {
"not be completed" in {
val p = Promise()
p.future.isCompleted mustBe (false)
}
"have no value" in {
val p = Promise()
p.future.value mustBe (None)
}
"return supplied value on timeout" in {
val failure = Promise.failed[String](new RuntimeException("br0ken")).future
val otherFailure = Promise.failed[String](new RuntimeException("last")).future
val empty = Promise[String]().future
val timedOut = Promise.successful[String]("Timedout").future
Await.result(failure fallbackTo timedOut, defaultTimeout) mustBe ("Timedout")
Await.result(timedOut fallbackTo empty, defaultTimeout) mustBe ("Timedout")
Await.result(failure fallbackTo failure fallbackTo timedOut, defaultTimeout) mustBe ("Timedout")
intercept[RuntimeException] {
Await.result(failure fallbackTo otherFailure, defaultTimeout)
}.getMessage mustBe ("last")
}
}
"A successful Promise" should {
val result = "test value"
val future = Promise[String]().complete(Right(result)).future
futureWithResult(_(future, result))
}
"A failed Promise" should {
val message = "Expected Exception"
val future = Promise[String]().complete(Left(new RuntimeException(message))).future
futureWithException[RuntimeException](_(future, message))
}
"An interrupted Promise" should {
val message = "Boxed InterruptedException"
val future = Promise[String]().complete(Left(new InterruptedException(message))).future
futureWithException[ExecutionException](_(future, message))
}
"A NonLocalReturnControl failed Promise" should {
val result = "test value"
val future = Promise[String]().complete(Left(new NonLocalReturnControl[String]("test", result))).future
futureWithResult(_(future, result))
}
def futureWithResult(f: ((Future[Any], Any) => Unit) => Unit) {
"be completed" in { f((future, _) => future.isCompleted mustBe (true)) }
"contain a value" in { f((future, result) => future.value mustBe (Some(Right(result)))) }
"return result with 'blocking'" in { f((future, result) => blocking(future, defaultTimeout) mustBe (result)) }
"return result with 'Await.result'" in { f((future, result) => Await.result(future, defaultTimeout) mustBe (result)) }
"not timeout" in { f((future, _) => Await.ready(future, 0 millis)) }
"filter result" in {
f {
(future, result) =>
Await.result((future filter (_ => true)), defaultTimeout) mustBe (result)
intercept[NoSuchElementException] {
Await.result((future filter (_ => false)), defaultTimeout)
}
}
}
"transform result with map" in { f((future, result) => Await.result((future map (_.toString.length)), defaultTimeout) mustBe (result.toString.length)) }
"compose result with flatMap" in {
f { (future, result) =>
val r = for (r <- future; p <- Promise.successful("foo").future) yield r.toString + p
Await.result(r, defaultTimeout) mustBe (result.toString + "foo")
}
}
"perform action with foreach" in {
f {
(future, result) =>
val p = Promise[Any]()
future foreach p.success
Await.result(p.future, defaultTimeout) mustBe (result)
}
}
"zip properly" in {
f {
(future, result) =>
Await.result(future zip Promise.successful("foo").future, defaultTimeout) mustBe ((result, "foo"))
intercept[RuntimeException] {
Await.result(future zip Promise.failed(new RuntimeException("ohnoes")).future, defaultTimeout)
}.getMessage mustBe ("ohnoes")
}
}
"not recover from exception" in { f((future, result) => Await.result(future.recover({ case _ => "pigdog" }), defaultTimeout) mustBe (result)) }
"perform action on result" in {
f {
(future, result) =>
val p = Promise[Any]()
future.onSuccess { case x => p.success(x) }
Await.result(p.future, defaultTimeout) mustBe (result)
}
}
"not project a failure" in {
f {
(future, result) =>
intercept[NoSuchElementException] {
Await.result(future.failed, defaultTimeout)
}.getMessage mustBe ("Future.failed not completed with a throwable.")
}
}
"cast using mapTo" in {
f {
(future, result) =>
Await.result(future.mapTo[Boolean].recover({ case _: ClassCastException ⇒ false }), defaultTimeout) mustBe (false)
}
}
}
def futureWithException[E <: Throwable: Manifest](f: ((Future[Any], String) => Unit) => Unit) {
"be completed" in {
f((future, _) => future.isCompleted mustBe (true))
}
"contain a value" in {
f((future, message) => {
future.value.get.left.get.getMessage mustBe (message)
})
}
"throw exception with 'blocking'" in {
f {
(future, message) =>
intercept[E] {
blocking(future, defaultTimeout)
}.getMessage mustBe (message)
}
}
"throw exception with 'Await.result'" in {
f {
(future, message) =>
intercept[E] {
Await.result(future, defaultTimeout)
}.getMessage mustBe (message)
}
}
"retain exception with filter" in {
f {
(future, message) =>
intercept[E] { Await.result(future filter (_ => true), defaultTimeout) }.getMessage mustBe (message)
intercept[E] { Await.result(future filter (_ => false), defaultTimeout) }.getMessage mustBe (message)
}
}
"retain exception with map" in {
f {
(future, message) =>
intercept[E] { Await.result(future map (_.toString.length), defaultTimeout) }.getMessage mustBe (message)
}
}
"retain exception with flatMap" in {
f {
(future, message) =>
intercept[E] { Await.result(future flatMap (_ => Promise.successful("foo").future), defaultTimeout) }.getMessage mustBe (message)
}
}
"zip properly" in {
f {
(future, message) =>
intercept[E] {
Await.result(future zip Promise.successful("foo").future, defaultTimeout)
}.getMessage mustBe (message)
}
}
"recover from exception" in {
f {
(future, message) =>
Await.result(future.recover({ case e if e.getMessage == message ⇒ "pigdog" }), defaultTimeout) mustBe ("pigdog")
}
}
"project a failure" in {
f((future, message) => Await.result(future.failed, defaultTimeout).getMessage mustBe (message))
}
"perform action on exception" in {
f {
(future, message) =>
val p = Promise[Any]()
future.onFailure { case _ => p.success(message) }
Await.result(p.future, defaultTimeout) mustBe (message)
}
}
"always cast successfully using mapTo" in {
f {
(future, message) =>
intercept[E] { Await.result(future.mapTo[java.lang.Thread], defaultTimeout) }.getMessage mustBe (message)
}
}
}
}