blob: 7db81cac2d73e37a8e2ccca3b7f62ed91680c919 [file] [log] [blame]
Roman Elizarovf106ff32017-05-17 12:22:14 +03001/*
2 * Copyright 2016-2017 JetBrains s.r.o.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package kotlinx.coroutines.experimental.guava
18
Roman Elizarov9fe5f462018-02-21 19:05:52 +030019import com.google.common.util.concurrent.*
Roman Elizarovf106ff32017-05-17 12:22:14 +030020import kotlinx.coroutines.experimental.*
Roman Elizarov9fe5f462018-02-21 19:05:52 +030021import kotlinx.coroutines.experimental.CancellationException
22import org.hamcrest.core.*
23import org.junit.*
Roman Elizarovf106ff32017-05-17 12:22:14 +030024import org.junit.Assert.*
Roman Elizarov9fe5f462018-02-21 19:05:52 +030025import java.util.concurrent.*
26import kotlin.coroutines.experimental.*
Roman Elizarovf106ff32017-05-17 12:22:14 +030027
28class ListenableFutureTest : TestBase() {
Roman Elizarov45181042017-07-20 20:37:51 +030029 @Before
30 fun setup() {
31 ignoreLostThreads("ForkJoinPool.commonPool-worker-")
32 }
33
Roman Elizarovf106ff32017-05-17 12:22:14 +030034 @Test
35 fun testSimpleAwait() {
36 val service = MoreExecutors.listeningDecorator(ForkJoinPool.commonPool())
37 val future = future {
38 service.submit(Callable<String> {
39 "O"
40 }).await() + "K"
41 }
42 assertThat(future.get(), IsEqual("OK"))
43 }
44
45 @Test
46 fun testCompletedFuture() {
47 val toAwait = SettableFuture.create<String>()
48 toAwait.set("O")
49 val future = future {
50 toAwait.await() + "K"
51 }
52 assertThat(future.get(), IsEqual("OK"))
53 }
54
55 @Test
56 fun testWaitForFuture() {
57 val toAwait = SettableFuture.create<String>()
58 val future = future {
59 toAwait.await() + "K"
60 }
61 assertFalse(future.isDone)
62 toAwait.set("O")
63 assertThat(future.get(), IsEqual("OK"))
64 }
65
66 @Test
67 fun testCompletedFutureExceptionally() {
68 val toAwait = SettableFuture.create<String>()
69 toAwait.setException(IllegalArgumentException("O"))
70 val future = future<String> {
71 try {
72 toAwait.await()
73 } catch (e: RuntimeException) {
74 assertThat(e, IsInstanceOf(IllegalArgumentException::class.java))
75 e.message!!
76 } + "K"
77 }
78 assertThat(future.get(), IsEqual("OK"))
79 }
80
81 @Test
82 fun testWaitForFutureWithException() {
83 val toAwait = SettableFuture.create<String>()
84 val future = future<String> {
85 try {
86 toAwait.await()
87 } catch (e: RuntimeException) {
88 assertThat(e, IsInstanceOf(IllegalArgumentException::class.java))
89 e.message!!
90 } + "K"
91 }
92 assertFalse(future.isDone)
93 toAwait.setException(IllegalArgumentException("O"))
94 assertThat(future.get(), IsEqual("OK"))
95 }
96
97 @Test
98 fun testExceptionInsideCoroutine() {
99 val service = MoreExecutors.listeningDecorator(ForkJoinPool.commonPool())
100 val future = future {
101 if (service.submit(Callable<Boolean> { true }).await()) {
102 throw IllegalStateException("OK")
103 }
104 "fail"
105 }
106 try {
107 future.get()
108 fail("'get' should've throw an exception")
109 } catch (e: ExecutionException) {
110 assertThat(e.cause, IsInstanceOf(IllegalStateException::class.java))
111 assertThat(e.cause!!.message, IsEqual("OK"))
112 }
113 }
114
115 @Test
116 fun testCompletedDeferredAsListenableFuture() = runBlocking {
117 expect(1)
Roman Elizarov43e3af72017-07-21 16:01:31 +0300118 val deferred = async(coroutineContext, CoroutineStart.UNDISPATCHED) {
Roman Elizarovf106ff32017-05-17 12:22:14 +0300119 expect(2) // completed right away
120 "OK"
121 }
122 expect(3)
123 val future = deferred.asListenableFuture()
124 assertThat(future.await(), IsEqual("OK"))
125 finish(4)
126 }
127
128 @Test
129 fun testWaitForDeferredAsListenableFuture() = runBlocking {
130 expect(1)
Roman Elizarov43e3af72017-07-21 16:01:31 +0300131 val deferred = async(coroutineContext) {
Roman Elizarovf106ff32017-05-17 12:22:14 +0300132 expect(3) // will complete later
133 "OK"
134 }
135 expect(2)
136 val future = deferred.asListenableFuture()
137 assertThat(future.await(), IsEqual("OK")) // await yields main thread to deferred coroutine
138 finish(4)
139 }
140
141 @Test
142 fun testCancellableAwait() = runBlocking {
143 expect(1)
144 val toAwait = SettableFuture.create<String>()
Roman Elizarov43e3af72017-07-21 16:01:31 +0300145 val job = launch(coroutineContext, CoroutineStart.UNDISPATCHED) {
Roman Elizarovf106ff32017-05-17 12:22:14 +0300146 expect(2)
147 try {
148 toAwait.await() // suspends
149 } catch (e: CancellationException) {
150 expect(5) // should throw cancellation exception
151 throw e
152 }
153 }
154 expect(3)
155 job.cancel() // cancel the job
156 toAwait.set("fail") // too late, the waiting job was already cancelled
157 expect(4) // job processing of cancellation was scheduled, not executed yet
158 yield() // yield main thread to job
159 finish(6)
160 }
161}