-
Notifications
You must be signed in to change notification settings - Fork 334
/
FutureOutcome.scala
639 lines (612 loc) · 31 KB
/
FutureOutcome.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
/*
* Copyright 2001-2016 Artima, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.scalatest
import scala.concurrent.Future
import scala.concurrent.ExecutionContext
import org.scalactic.{Or, Good, Bad}
import scala.util.{Try, Success, Failure}
import exceptions.TestCanceledException
import exceptions.TestPendingException
import Suite.anExceptionThatShouldCauseAnAbort
import scala.concurrent.ExecutionException
/*
Note, the reason Outcome or Throwable is used here instead of Try[Outcome] is
to avoid confusion over the Try that comes back from the Future[Outcome]. Only
run-aborting exceptions will be contained in scala.util.Failures in this case.
Other exceptions will show up as Success(org.scalatest.Failed) or Success(org.scalatest.Canceled).
And this confusion of Success(Failed) is what the Or is intended to alleviate.
*/
/**
* Wrapper class for <code>Future[Outcome]</code> that presents a more convenient API for
* manipulation in <code>withFixture</code> methods in async styles.
*
* <p>
* This type serves as the result type of both test functions and <code>withFixture</code> methods
* in ScalaTest's async styles. A <code>Future[Outcome]</code> is not used as this result type
* for two reasons. First, <code>Outcome</code> treats exceptions specially, and as a result
* methods on <code>Future</code> would usually not yield the desired <code>Future[Outcome]</code> result.
* Only run-aborting exceptions should result in a failed <code>Future[Outcome]</code>. Any other thrown exception
* other than <code>TestCanceledException</code> or <code>TestPendingException</code>
* should result in a successful<code>Future</code> containing a <code>org.scalatest.Failed</code>.
* A thrown <code>TestCanceledException</code> should result in a successful <code>Future</code>
* containing an <code>org.scalatest.Canceled</code>; A thrown <code>TestPendingException</code> should result in
* a successful <code>Future</code> containing a <code>org.scalatest.Pending</code>.
* If manipulating a <code>Future[Outcome]</code> directly, by contrast, any thrown exception would result in
* a failed <code>Future</code>.
* </p>
*
* <p>
* Additionally, to be consistent with corresponding transformations in traditional testing styles,
* methods registering callbacks should return a new future outcome that doesn't complete until
* both the original future outcome has completed and the subsequent callback has completed execution.
* Additionally, if the callback itself throws an exception, that exception should determine the result
* of the future outcome returned by the callback registration method. This behavior is rather inconvenient
* to obtain on the current <code>Future</code> API, so <code>FutureOutcome</code> provides well-named
* methods that have this behavior.
* </p>
*
* <p>
* Lastly, the <code>FutureOutcome</code> is intended to help prevent confusion by eliminating the need
* to work with types like <code>scala.util.Success(org.scalatest.Failed)</code>. For this purpose a
* <code>org.scalactic.Or</code> is used instead of a <code>scala.util.Try</code> to describe results
* of <code>FutureOutcome</code>.
* </p>
*
* <p>
* A <code>FutureOutcome</code> represents a computation that can result in an <code>Outcome</code> or an "abort." An abort means
* that a run-aborting exception occurred during the computation. Any other, non-run-aborting exception will be represented
* as an non-<code>Succeeded</code> <code>Outcome</code>: one of <code>Failed</code>, <code>Canceled</code>, or <code>Pending</code>.
* </p>
*
* <p>
* The methods of <code>FutureOutcome</code> include the following callback registration methods:
* </p>
*
* <ul>
* <li><code>onSucceededThen</code> - registers a callback to be executed if the future outcome is <code>Succeeded</code>.</li>
* <li><code>onFailedThen</code> - registers a callback to be executed if the future outcome is <code>Failed</code>.</li>
* <li><code>onCanceledThen</code> - registers a callback to be executed if the future outcome is <code>Canceled</code>.</li>
* <li><code>onPendingThen</code> - registers a callback to be executed if the future outcome is <code>Pending</code>.</li>
* <li><code>onOutcomeThen</code> - registers a callback to be executed if the future outcome is actually an <code>Outcome</code>
* and not an abort.</li>
* <li><code>onAbortedThen</code> - registers a callback to be executed if the future outcome aborts.</li>
* <li><code>onCompletedThen</code> - registers a callback to be executed upon completion no matter how the future outcome completes.</li>
* </ul>
*
* <p>
* The callback methods listed previously can be used to perform a side effect once a <code>FutureOutcome</code> completes. To change an
* <code>Outcome</code> into a different <code>Outcome</code> asynchronously, use the <code>change</code> registration method, which takes a function
* from <code>Outcome</code> to <code>Outcome</code>. The other methods on <code>FutureOutcome</code>, <code>isCompleted</code> and
* <code>value</code>, allow you to poll a <code>FutureOutcome</code>. None of the methods on <code>FutureOutcome</code> block.
* Lastly, because an implicit <a href="enablers/Futuristic.html"><code>Futuristic</code></a> instance is provided for
* <code>FutureOutcome</code>, you can use <a href="CompleteLastly.html"><code>complete</code>-<code>lastly</code> syntax</a>
* with <code>FutureOutcome</code>.
* </p>
*/
// SKIP-DOTTY-START
class FutureOutcome(private[scalatest] val underlying: Future[Outcome]) {
// SKIP-DOTTY-END
//DOTTY-ONLY open class FutureOutcome(private[scalatest] val underlying: Future[Outcome]) {
// TODO: add tests for pretty toString
/**
* Registers a callback function to be executed after this future completes, returning
* a new future that completes only after the callback has finished execution.
*
* <p>
* The resulting <code>FutureOutcome</code> will have the same result as this <code>FutureOutcome</code>, unless
* the callback completes abruptly with an exception. In that case, the resulting <code>FutureOutcome</code>
* will be determined by the type of the thrown exception:
* </p>
*
* <ul>
* <li><code>TestPendingException</code></li> - <code>Good(Pending)</code>
* <li><code>TestCanceledException</code></li> - <code>Good(Canceled(<the exception>))</code>
* <li>Any non-run-aborting <code>Throwable</code></li> - <code>Good(Failed(<the exception>))</code>
* <li>A run-aborting <code>Throwable</code></li> - <code>Bad(<the run-aborting exception>)</code>
* </ul>
*
* <p>
* For more information on <em>run-aborting</em> exceptions, see the <a href="Suite.html#errorHandling">Run-aborting exceptions</a> section
* in the main Scaladoc for trait <code>Suite</code>.
* </p>
*
* @param callback a side-effecting function to execute when this <code>FutureOutcome</code> completes
* @param executionContext an execution context that provides a strategy for executing the callback function
* @return a new <code>FutureOutcome</code> that will complete only after this <code>FutureOutcome</code>
* and, subsequently, the passed callback function have completed execution.
*/
def onCompletedThen(callback: (Outcome Or Throwable) => Unit)(implicit executionContext: ExecutionContext): FutureOutcome = {
FutureOutcome {
underlying recoverWith {
case ex =>
try {
callback(Bad(ex))
Future.failed(ex)
}
catch {
case _: TestPendingException => Future.successful(Pending)
case ex: TestCanceledException => Future.successful(Canceled(ex))
case ex: Throwable if !anExceptionThatShouldCauseAnAbort(ex) => Future.successful(Failed(ex))
case ex: Throwable => Future.failed(new ExecutionException(ex))
}
} flatMap { outcome =>
try {
callback(Good(outcome))
Future.successful(outcome)
}
catch {
case _: TestPendingException => Future.successful(Pending)
case ex: TestCanceledException => Future.successful(Canceled(ex))
case ex: Throwable if !anExceptionThatShouldCauseAnAbort(ex) => Future.successful(Failed(ex))
case ex: Throwable => Future.failed(new ExecutionException(ex))
}
}
}
}
/**
* Registers a callback function to be executed if this future completes with
* <code>Succeeded</code>, returning a new future that completes only after the
* callback has finished execution.
*
* <p>
* The resulting <code>FutureOutcome</code> will have the same result as this <code>FutureOutcome</code>, unless
* the callback completes abruptly with an exception. In that case, the resulting <code>FutureOutcome</code>
* will be determined by the type of the thrown exception:
* </p>
*
* <ul>
* <li><code>TestPendingException</code></li> - <code>Good(Pending)</code>
* <li><code>TestCanceledException</code></li> - <code>Good(Canceled(<the exception>))</code>
* <li>Any non-run-aborting <code>Throwable</code></li> - <code>Good(Failed(<the exception>))</code>
* <li>A run-aborting <code>Throwable</code></li> - <code>Bad(<the run-aborting exception>)</code>
* </ul>
*
* <p>
* For more information on <em>run-aborting</em> exceptions, see the <a href="Suite.html#errorHandling">Run-aborting exceptions</a> section
* in the main Scaladoc for trait <code>Suite</code>.
* </p>
*
* @param callback a side-effecting function to execute if and when this <code>FutureOutcome</code> completes with <code>Succeeded</code>
* @param executionContext an execution context that provides a strategy for executing the callback function
* @return a new <code>FutureOutcome</code> that will complete only after this <code>FutureOutcome</code>
* has completed and, if this <code>FutureOutcome</code> completes with <code>Succeeded</code>, the
* passed callback function has completed execution.
*/
def onSucceededThen(callback: => Unit)(implicit executionContext: ExecutionContext): FutureOutcome = {
FutureOutcome {
underlying flatMap { outcome =>
if (outcome.isSucceeded) {
try {
callback
Future.successful(outcome)
}
catch {
case _: TestPendingException => Future.successful(Pending)
case ex: TestCanceledException => Future.successful(Canceled(ex))
case ex: Throwable if !anExceptionThatShouldCauseAnAbort(ex) => Future.successful(Failed(ex))
case ex: Throwable => Future.failed(new ExecutionException(ex))
}
} else Future.successful(outcome)
}
}
}
/**
* Registers a callback function to be executed if this future completes with
* <code>Failed</code>, returning a new future that completes only after the
* callback has finished execution.
*
* <p>
* The resulting <code>FutureOutcome</code> will have the same result as this <code>FutureOutcome</code>, unless
* the callback completes abruptly with an exception. In that case, the resulting <code>FutureOutcome</code>
* will be determined by the type of the thrown exception:
* </p>
*
* <ul>
* <li><code>TestPendingException</code></li> - <code>Good(Pending)</code>
* <li><code>TestCanceledException</code></li> - <code>Good(Canceled(<the exception>))</code>
* <li>Any non-run-aborting <code>Throwable</code></li> - <code>Good(Failed(<the exception>))</code>
* <li>A run-aborting <code>Throwable</code></li> - <code>Bad(<the run-aborting exception>)</code>
* </ul>
*
* <p>
* For more information on <em>run-aborting</em> exceptions, see the <a href="Suite.html#errorHandling">Run-aborting exceptions</a> section
* in the main Scaladoc for trait <code>Suite</code>.
* </p>
*
* @param callback a side-effecting function to execute if and when this <code>FutureOutcome</code> completes with <code>Failed</code>
* @param executionContext an execution context that provides a strategy for executing the callback function
* @return a new <code>FutureOutcome</code> that will complete only after this <code>FutureOutcome</code>
* has completed and, if this <code>FutureOutcome</code> completes with <code>Failed</code>, the
* passed callback function has completed execution.
*/
def onFailedThen(callback: Throwable => Unit)(implicit executionContext: ExecutionContext): FutureOutcome = {
FutureOutcome {
underlying flatMap { outcome =>
outcome match {
case Failed(originalEx) =>
try {
callback(originalEx)
Future.successful(outcome)
}
catch {
case _: TestPendingException => Future.successful(Pending)
case ex: TestCanceledException => Future.successful(Canceled(ex))
case ex: Throwable if !anExceptionThatShouldCauseAnAbort(ex) => Future.successful(Failed(ex))
case ex: Throwable => Future.failed(new ExecutionException(ex))
}
case _ =>
Future.successful(outcome)
}
}
}
}
/**
* Registers a callback function to be executed if this future completes with
* <code>Canceled</code>, returning a new future that completes only after the
* callback has finished execution.
*
* <p>
* The resulting <code>FutureOutcome</code> will have the same result as this <code>FutureOutcome</code>, unless
* the callback completes abruptly with an exception. In that case, the resulting <code>FutureOutcome</code>
* will be determined by the type of the thrown exception:
* </p>
*
* <ul>
* <li><code>TestPendingException</code></li> - <code>Good(Pending)</code>
* <li><code>TestCanceledException</code></li> - <code>Good(Canceled(<the exception>))</code>
* <li>Any non-run-aborting <code>Throwable</code></li> - <code>Good(Failed(<the exception>))</code>
* <li>A run-aborting <code>Throwable</code></li> - <code>Bad(<the run-aborting exception>)</code>
* </ul>
*
* <p>
* For more information on <em>run-aborting</em> exceptions, see the <a href="Suite.html#errorHandling">Run-aborting exceptions</a> section
* in the main Scaladoc for trait <code>Suite</code>.
* </p>
*
* @param callback a side-effecting function to execute if and when this <code>FutureOutcome</code> completes with <code>Canceled</code>
* @param executionContext an execution context that provides a strategy for executing the callback function
* @return a new <code>FutureOutcome</code> that will complete only after this <code>FutureOutcome</code>
* has completed and, if this <code>FutureOutcome</code> completes with <code>Canceled</code>, the
* passed callback function has completed execution.
*/
def onCanceledThen(callback: TestCanceledException => Unit)(implicit executionContext: ExecutionContext): FutureOutcome = {
FutureOutcome {
underlying flatMap { outcome =>
outcome match {
case Canceled(originalEx) =>
try {
callback(originalEx)
Future.successful(outcome)
}
catch {
case _: TestPendingException => Future.successful(Pending)
case ex: TestCanceledException => Future.successful(Canceled(ex))
case ex: Throwable if !anExceptionThatShouldCauseAnAbort(ex) => Future.successful(Failed(ex))
case ex: Throwable => Future.failed(new ExecutionException(ex))
}
case _ =>
Future.successful(outcome)
}
}
}
}
/**
* Registers a callback function to be executed if this future completes with
* <code>Pending</code>, returning a new future that completes only after the
* callback has finished execution.
*
* <p>
* The resulting <code>FutureOutcome</code> will have the same result as this <code>FutureOutcome</code>, unless
* the callback completes abruptly with an exception. In that case, the resulting <code>FutureOutcome</code>
* will be determined by the type of the thrown exception:
* </p>
*
* <ul>
* <li><code>TestPendingException</code></li> - <code>Good(Pending)</code>
* <li><code>TestCanceledException</code></li> - <code>Good(Canceled(<the exception>))</code>
* <li>Any non-run-aborting <code>Throwable</code></li> - <code>Good(Failed(<the exception>))</code>
* <li>A run-aborting <code>Throwable</code></li> - <code>Bad(<the run-aborting exception>)</code>
* </ul>
*
* <p>
* For more information on <em>run-aborting</em> exceptions, see the <a href="Suite.html#errorHandling">Run-aborting exceptions</a> section
* in the main Scaladoc for trait <code>Suite</code>.
* </p>
*
* @param callback a side-effecting function to execute if and when this <code>FutureOutcome</code> completes with <code>Pending</code>
* @param executionContext an execution context that provides a strategy for executing the callback function
* @return a new <code>FutureOutcome</code> that will complete only after this <code>FutureOutcome</code>
* has completed and, if this <code>FutureOutcome</code> completes with <code>Pending</code>, the
* passed callback function has completed execution.
*/
def onPendingThen(callback: => Unit)(implicit executionContext: ExecutionContext): FutureOutcome = {
FutureOutcome {
underlying flatMap { outcome =>
if (outcome.isPending) {
try {
callback
Future.successful(outcome)
}
catch {
case _: TestPendingException => Future.successful(Pending)
case ex: TestCanceledException => Future.successful(Canceled(ex))
case ex: Throwable if !anExceptionThatShouldCauseAnAbort(ex) => Future.successful(Failed(ex))
case ex: Throwable => Future.failed(new ExecutionException(ex))
}
} else Future.successful(outcome)
}
}
}
/**
* Registers a transformation function to be executed if this future completes with any
* <code>Outcome</code> (<em>i.e.</em>, no run-aborting exception is thrown), returning
* a new <code>FutureOutcome</code> representing the result of passing
* this <code>FutureOutcome</code>'s <code>Outcome</code> result to the given transformation function.
*
* <p>
* If the passed function completes abruptly with an exception, the resulting <code>FutureOutcome</code>
* will be determined by the type of the thrown exception:
* </p>
*
* <ul>
* <li><code>TestPendingException</code></li> - <code>Good(Pending)</code>
* <li><code>TestCanceledException</code></li> - <code>Good(Canceled(<the exception>))</code>
* <li>Any non-run-aborting <code>Throwable</code></li> - <code>Good(Failed(<the exception>))</code>
* <li>A run-aborting <code>Throwable</code></li> - <code>Bad(<the run-aborting exception>)</code>
* </ul>
*
* <p>
* For more information on <em>run-aborting</em> exceptions, see the <a href="Suite.html#errorHandling">Run-aborting exceptions</a> section
* in the main Scaladoc for trait <code>Suite</code>.
* </p>
*
* @param f a transformation function to execute if and when this <code>FutureOutcome</code> completes with an <code>Outcome</code>
* @param executionContext an execution context that provides a strategy for executing the transformation function
* @return a new <code>FutureOutcome</code> that will complete only after this <code>FutureOutcome</code>
* has completed and, if this <code>FutureOutcome</code> completes with a valid
* <code>Outcome</code>, the passed callback function has completed execution.
*/
def change(f: Outcome => Outcome)(implicit executionContext: ExecutionContext): FutureOutcome = {
FutureOutcome {
underlying flatMap { outcome =>
try Future.successful(f(outcome))
catch {
case _: TestPendingException => Future.successful(Pending)
case ex: TestCanceledException => Future.successful(Canceled(ex))
case ex: Throwable if !anExceptionThatShouldCauseAnAbort(ex) => Future.successful(Failed(ex))
case ex: Throwable => Future.failed(new ExecutionException(ex))
}
}
}
}
/**
* Registers a callback function to be executed if this future completes because
* a run-aborting exception was thrown, returning a new future that completes only after the
* callback has finished execution.
*
* <p>
* The resulting <code>FutureOutcome</code> will have the same result as this <code>FutureOutcome</code>, unless
* the callback completes abruptly with an exception. In that case, the resulting <code>FutureOutcome</code>
* will be determined by the type of the thrown exception:
* </p>
*
* <ul>
* <li><code>TestPendingException</code></li> - <code>Good(Pending)</code>
* <li><code>TestCanceledException</code></li> - <code>Good(Canceled(<the exception>))</code>
* <li>Any non-run-aborting <code>Throwable</code></li> - <code>Good(Failed(<the exception>))</code>
* <li>A run-aborting <code>Throwable</code></li> - <code>Bad(<the run-aborting exception>)</code>
* </ul>
*
* <p>
* For more information on <em>run-aborting</em> exceptions, see the <a href="Suite.html#errorHandling">Run-aborting exceptions</a> section
* in the main Scaladoc for trait <code>Suite</code>.
* </p>
*
* @param callback a side-effecting function to execute if and when this <code>FutureOutcome</code> completes with an abort.
* @param executionContext an execution context that provides a strategy for executing the callback function
* @return a new <code>FutureOutcome</code> that will complete only after this <code>FutureOutcome</code>
* has completed and, if this <code>FutureOutcome</code> completes abnormally with
* a run-aborting exception, the passed callback function has completed execution.
*/
def onAbortedThen(callback: Throwable => Unit)(implicit executionContext: ExecutionContext): FutureOutcome = {
FutureOutcome {
underlying recoverWith {
case originalEx =>
try {
callback(originalEx)
Future.failed(originalEx)
}
catch {
case _: TestPendingException => Future.successful(Pending)
case ex: TestCanceledException => Future.successful(Canceled(ex))
case ex: Throwable if !anExceptionThatShouldCauseAnAbort(ex) => Future.successful(Failed(ex))
case ex: Throwable => Future.failed(new ExecutionException(ex))
}
}
}
}
/**
* Registers a callback function to be executed if this future completes with any
* <code>Outcome</code> (<em>i.e.</em>, no run-aborting exception is thrown), returning
* a new future that completes only after the callback has finished execution.
*
* <p>
* The resulting <code>FutureOutcome</code> will have the same result as this <code>FutureOutcome</code>, unless
* the callback completes abruptly with an exception. In that case, the resulting <code>FutureOutcome</code>
* will be determined by the type of the thrown exception:
* </p>
*
* <ul>
* <li><code>TestPendingException</code></li> - <code>Good(Pending)</code>
* <li><code>TestCanceledException</code></li> - <code>Good(Canceled(<the exception>))</code>
* <li>Any non-run-aborting <code>Throwable</code></li> - <code>Good(Failed(<the exception>))</code>
* <li>A run-aborting <code>Throwable</code></li> - <code>Bad(<the run-aborting exception>)</code>
* </ul>
*
* <p>
* For more information on <em>run-aborting</em> exceptions, see the <a href="Suite.html#errorHandling">Run-aborting exceptions</a> section
* in the main Scaladoc for trait <code>Suite</code>.
* </p>
*
* @param callback a side-effecting function to execute if and when this <code>FutureOutcome</code> completes with an <code>Outcome</code>
* (<em>i.e.</em>, not an abort)
* @param executionContext an execution context that provides a strategy for executing the callback function
* @return a new <code>FutureOutcome</code> that will complete only after this <code>FutureOutcome</code>
* has completed and, if this <code>FutureOutcome</code> completes with a valid
* <code>Outcome</code>, the passed callback function has completed execution.
*/
def onOutcomeThen(callback: Outcome => Unit)(implicit executionContext: ExecutionContext): FutureOutcome = {
FutureOutcome {
underlying flatMap { outcome =>
try {
callback(outcome)
Future.successful(outcome)
}
catch {
case _: TestPendingException => Future.successful(Pending)
case ex: TestCanceledException => Future.successful(Canceled(ex))
case ex: Throwable if !anExceptionThatShouldCauseAnAbort(ex) => Future.successful(Failed(ex))
case ex: Throwable => Future.failed(new ExecutionException(ex))
}
}
}
}
/**
* Indicates whether this <code>FutureOutcome</code> has completed.
*
* <p>
* This method does not block.
* </p>
*
* @return <code>true</code> if this <code>FutureOutcome</code> has completed; <code>false</code> otherwise.
*/
def isCompleted: Boolean = underlying.isCompleted
/**
* Returns a value that indicates whether this <code>FutureOutcome</code> has completed,
* and if so, indicates its result.
*
* <p>
* If this <code>FutureOutcome</code> has not yet completed, this method will return
* <code>None</code>. Otherwise, this method will return a <code>Some</code> that contains
* either a <code>Good[Outcome]</code>, if this <code>FutureOutcome</code> completed with
* a valid <code>Outcome</code> result, or if it completed with a thrown run-aborting
* exception, a <code>Bad[Throwable]</code>.
* </p>
*
* <p>
* For more information on <em>run-aborting</em> exceptions, see the <a href="Suite.html#errorHandling">Run-aborting exceptions</a> section
* in the main Scaladoc for trait <code>Suite</code>.
* </p>
*
* @return a <code>Some</code> containing an <code>Or</code> value that indicates the result of this
* <code>FutureOutcome</code> if it has completed; <code>None</code> otherwise.
*/
def value: Option[Outcome Or Throwable] =
underlying.value match {
case None => None
case Some(Success(outcome)) => Some(Good(outcome))
case Some(Failure(ex)) => Some(Bad(ex))
}
/**
* Converts this <code>FutureOutcome</code> to a <code>Future[Outcome]</code>.
*
* @return the underlying <code>Future[Outcome]</code>
*/
def toFuture: Future[Outcome] = underlying
}
/**
* Companion object to <code>FutureOutcomes</code> that contains factory methods for creating already-completed
* <code>FutureOutcomes</code>.
*/
object FutureOutcome {
// Make this private so only ScalaTest can make one, so we can "promise" that
// you'll never need to look for things like a TestCanceledException being passed
// to onAbortedThen.
private[scalatest] def apply(underlying: Future[Outcome]): FutureOutcome = new FutureOutcome(underlying)
/**
* Factory method that creates an already completed <code>FutureOutcome</code> with a <code>Canceled</code> result.
*/
def canceled(): FutureOutcome =
FutureOutcome { Future.successful(Canceled()) }
/**
* Factory method that creates an already completed <code>FutureOutcome</code> with a <code>Canceled</code> result
* whose <code>TestCanceledException</code> contains the specified message.
*
* @message the message string to include in the <code>Canceled</code>'s <code>TestCanceledException</code>.
*/
def canceled(message: String): FutureOutcome =
FutureOutcome { Future.successful(Canceled(message)) }
/**
* Factory method that creates an already completed <code>FutureOutcome</code> with a <code>Canceled</code> result
* whose <code>TestCanceledException</code> contains the specified cause.
*
* @cause exception to include as the <code>Canceled</code>'s <code>TestCanceledException</code> cause.
*/
def canceled(cause: Throwable): FutureOutcome =
FutureOutcome { Future.successful(Canceled(cause)) }
/**
* Factory method that creates an already completed <code>FutureOutcome</code> with a <code>Canceled</code> result
* whose <code>TestCanceledException</code> contains the specified message and cause.
*
* @message the message string to include in the <code>Canceled</code>'s <code>TestCanceledException</code>.
* @cause exception to include as the <code>Canceled</code>'s <code>TestCanceledException</code> cause.
*/
def canceled(message: String, cause: Throwable) =
FutureOutcome { Future.successful(Canceled(message, cause)) }
/**
* Factory method that creates an already completed <code>FutureOutcome</code> with a <code>Succeeded</code> result.
*/
def succeeded: FutureOutcome =
FutureOutcome { Future.successful(Succeeded) }
/**
* Factory method that creates an already completed <code>FutureOutcome</code> with a <code>Failed</code> result.
*/
def failed(): FutureOutcome =
FutureOutcome { Future.successful(Failed()) }
/**
* Factory method that creates an already completed <code>FutureOutcome</code> with a <code>Failed</code> result
* containing a <code>TestFailedException</code> with the specified message.
*
* @message the message string to include in the <code>Failed</code>'s <code>TestFailedException</code>.
*/
def failed(message: String): FutureOutcome =
FutureOutcome { Future.successful(Failed(message)) }
/**
* Factory method that creates an already completed <code>FutureOutcome</code> with a <code>Failed</code> result
* containing a <code>TestFailedException</code> with the specified message and cause.
*
* @message the message string to include in the <code>Failed</code>'s <code>TestFailedException</code>.
* @cause exception to include as the <code>Failed</code>'s <code>TestFailedException</code> cause.
*/
def failed(message: String, cause: Throwable) =
FutureOutcome { Future.successful(Failed(message, cause)) }
/**
* Factory method that creates an already completed <code>FutureOutcome</code> with a <code>Failed</code> result
* containing a <code>TestFailedException</code> with the specified cause.
*
* @cause exception to include as the <code>Failed</code>'s <code>TestFailedException</code> cause.
*/
def failed(cause: Throwable): FutureOutcome =
FutureOutcome { Future.successful(Failed(cause)) }
/**
* Factory method that creates an already completed <code>FutureOutcome</code> with a <code>Pending</code> result.
*/
def pending: FutureOutcome =
FutureOutcome { Future.successful(Pending) }
}
/*
FutureOutcome.fromOutcome(Canceled("..."))
*/