blob: 2646dfac58452a34a5e372b5fcbe50adf62b6096 [file] [log] [blame]
Vsevolod Tolstopyatov1cbe8f02018-06-05 18:13:51 +03001package kotlinx.coroutines.experimental.guava
2
3import com.google.common.util.concurrent.*
4import kotlinx.coroutines.experimental.*
5import org.junit.Test
6import java.io.*
7import java.util.concurrent.*
8import kotlin.coroutines.experimental.*
9import kotlin.test.*
10
11class ListenableFutureExceptionsTest : TestBase() {
12
13 @Test
14 fun testAwait() {
15 testException(IOException(), { it is IOException })
16 }
17
18 @Test
19 fun testAwaitChained() {
20 testException(IOException(), { it is IOException }, { i -> i!! + 1 })
21 }
22
23 @Test
24 fun testAwaitCompletionException() {
25 testException(CompletionException("test", IOException()), { it is CompletionException })
26 }
27
28 @Test
29 fun testAwaitChainedCompletionException() {
30 testException(
31 CompletionException("test", IOException()),
32 { it is CompletionException },
33 { i -> i!! + 1 })
34 }
35
36 @Test
37 fun testAwaitTestException() {
38 testException(TestException(), { it is TestException })
39 }
40
41 @Test
42 fun testAwaitChainedTestException() {
43 testException(TestException(), { it is TestException }, { i -> i!! + 1 })
44 }
45
46 class TestException : CompletionException("test2")
47
48 private fun testException(
49 exception: Exception,
50 expected: ((Throwable) -> Boolean),
51 transformer: ((Int?) -> Int?)? = null
52 ) {
53
54 // Fast path
55 runTest {
56 val future = SettableFuture.create<Int>()
57 val chained = if (transformer == null) future else Futures.transform(future, transformer)
58 future.setException(exception)
59 try {
60 chained.await()
61 } catch (e: Exception) {
62 assertTrue(expected(e))
63 }
64 }
65
66 // Slow path
67 runTest {
68 val future = SettableFuture.create<Int>()
69 val chained = if (transformer == null) future else Futures.transform(future, transformer)
70 launch(coroutineContext) {
71 future.setException(exception)
72 }
73
74 try {
75 chained.await()
76 } catch (e: Exception) {
77 assertTrue(expected(e))
78 }
79 }
80 }
81}