Roman Elizarov | f106ff3 | 2017-05-17 12:22:14 +0300 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package kotlinx.coroutines.experimental.guava |
| 18 | |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame^] | 19 | import com.google.common.util.concurrent.* |
Roman Elizarov | f106ff3 | 2017-05-17 12:22:14 +0300 | [diff] [blame] | 20 | import kotlinx.coroutines.experimental.* |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame^] | 21 | import kotlinx.coroutines.experimental.CancellationException |
| 22 | import org.hamcrest.core.* |
| 23 | import org.junit.* |
Roman Elizarov | f106ff3 | 2017-05-17 12:22:14 +0300 | [diff] [blame] | 24 | import org.junit.Assert.* |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame^] | 25 | import java.util.concurrent.* |
| 26 | import kotlin.coroutines.experimental.* |
Roman Elizarov | f106ff3 | 2017-05-17 12:22:14 +0300 | [diff] [blame] | 27 | |
| 28 | class ListenableFutureTest : TestBase() { |
Roman Elizarov | 4518104 | 2017-07-20 20:37:51 +0300 | [diff] [blame] | 29 | @Before |
| 30 | fun setup() { |
| 31 | ignoreLostThreads("ForkJoinPool.commonPool-worker-") |
| 32 | } |
| 33 | |
Roman Elizarov | f106ff3 | 2017-05-17 12:22:14 +0300 | [diff] [blame] | 34 | @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 Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 118 | val deferred = async(coroutineContext, CoroutineStart.UNDISPATCHED) { |
Roman Elizarov | f106ff3 | 2017-05-17 12:22:14 +0300 | [diff] [blame] | 119 | 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 Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 131 | val deferred = async(coroutineContext) { |
Roman Elizarov | f106ff3 | 2017-05-17 12:22:14 +0300 | [diff] [blame] | 132 | 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 Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 145 | val job = launch(coroutineContext, CoroutineStart.UNDISPATCHED) { |
Roman Elizarov | f106ff3 | 2017-05-17 12:22:14 +0300 | [diff] [blame] | 146 | 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 | } |