Roman Elizarov | f16fd27 | 2017-02-07 11:26:00 +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 | |
Roman Elizarov | 7cf452e | 2017-01-29 21:58:33 +0300 | [diff] [blame] | 17 | package kotlinx.coroutines.experimental.future |
Denis Zharkov | 8e4e0e4 | 2016-06-22 18:27:19 +0300 | [diff] [blame] | 18 | |
Roman Elizarov | 489cac2 | 2017-05-17 12:18:28 +0300 | [diff] [blame] | 19 | import kotlinx.coroutines.experimental.* |
Roman Elizarov | 19c1f2e | 2018-03-01 22:15:58 +0300 | [diff] [blame] | 20 | import kotlinx.coroutines.experimental.CancellationException |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 21 | import org.hamcrest.core.* |
| 22 | import org.junit.* |
Roman Elizarov | 489cac2 | 2017-05-17 12:18:28 +0300 | [diff] [blame] | 23 | import org.junit.Assert.* |
Roman Elizarov | 19c1f2e | 2018-03-01 22:15:58 +0300 | [diff] [blame] | 24 | import java.util.concurrent.* |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 25 | import java.util.concurrent.atomic.* |
| 26 | import java.util.concurrent.locks.* |
| 27 | import kotlin.concurrent.* |
| 28 | import kotlin.coroutines.experimental.* |
Denis Zharkov | 8e4e0e4 | 2016-06-22 18:27:19 +0300 | [diff] [blame] | 29 | |
Roman Elizarov | 489cac2 | 2017-05-17 12:18:28 +0300 | [diff] [blame] | 30 | class FutureTest : TestBase() { |
Roman Elizarov | 4518104 | 2017-07-20 20:37:51 +0300 | [diff] [blame] | 31 | @Before |
| 32 | fun setup() { |
| 33 | ignoreLostThreads("ForkJoinPool.commonPool-worker-") |
| 34 | } |
| 35 | |
Denis Zharkov | 8e4e0e4 | 2016-06-22 18:27:19 +0300 | [diff] [blame] | 36 | @Test |
Roman Elizarov | 489cac2 | 2017-05-17 12:18:28 +0300 | [diff] [blame] | 37 | fun testSimpleAwait() { |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 38 | val future = future { |
Denis Zharkov | b483312 | 2016-12-15 11:49:03 +0300 | [diff] [blame] | 39 | CompletableFuture.supplyAsync { |
Denis Zharkov | 8e4e0e4 | 2016-06-22 18:27:19 +0300 | [diff] [blame] | 40 | "O" |
Denis Zharkov | b483312 | 2016-12-15 11:49:03 +0300 | [diff] [blame] | 41 | }.await() + "K" |
Denis Zharkov | 8e4e0e4 | 2016-06-22 18:27:19 +0300 | [diff] [blame] | 42 | } |
Roman Elizarov | 489cac2 | 2017-05-17 12:18:28 +0300 | [diff] [blame] | 43 | assertThat(future.get(), IsEqual("OK")) |
| 44 | } |
| 45 | |
| 46 | @Test |
| 47 | fun testCompletedFuture() { |
| 48 | val toAwait = CompletableFuture<String>() |
| 49 | toAwait.complete("O") |
| 50 | val future = future { |
| 51 | toAwait.await() + "K" |
| 52 | } |
| 53 | assertThat(future.get(), IsEqual("OK")) |
| 54 | } |
| 55 | |
| 56 | @Test |
| 57 | fun testCompletedCompletionStage() { |
| 58 | val completable = CompletableFuture<String>() |
| 59 | completable.complete("O") |
| 60 | val toAwait: CompletionStage<String> = completable |
| 61 | val future = future { |
| 62 | toAwait.await() + "K" |
| 63 | } |
| 64 | assertThat(future.get(), IsEqual("OK")) |
Denis Zharkov | 8e4e0e4 | 2016-06-22 18:27:19 +0300 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | @Test |
Roman Elizarov | fc6461f | 2017-05-16 18:48:17 +0300 | [diff] [blame] | 68 | fun testWaitForFuture() { |
Denis Zharkov | 8e4e0e4 | 2016-06-22 18:27:19 +0300 | [diff] [blame] | 69 | val toAwait = CompletableFuture<String>() |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 70 | val future = future { |
Denis Zharkov | b483312 | 2016-12-15 11:49:03 +0300 | [diff] [blame] | 71 | toAwait.await() + "K" |
Denis Zharkov | 8e4e0e4 | 2016-06-22 18:27:19 +0300 | [diff] [blame] | 72 | } |
Denis Zharkov | 8e4e0e4 | 2016-06-22 18:27:19 +0300 | [diff] [blame] | 73 | assertFalse(future.isDone) |
| 74 | toAwait.complete("O") |
Roman Elizarov | 489cac2 | 2017-05-17 12:18:28 +0300 | [diff] [blame] | 75 | assertThat(future.get(), IsEqual("OK")) |
Denis Zharkov | 8e4e0e4 | 2016-06-22 18:27:19 +0300 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | @Test |
Roman Elizarov | fc6461f | 2017-05-16 18:48:17 +0300 | [diff] [blame] | 79 | fun testWaitForCompletionStage() { |
| 80 | val completable = CompletableFuture<String>() |
| 81 | val toAwait: CompletionStage<String> = completable |
| 82 | val future = future { |
| 83 | toAwait.await() + "K" |
| 84 | } |
| 85 | assertFalse(future.isDone) |
| 86 | completable.complete("O") |
Roman Elizarov | 489cac2 | 2017-05-17 12:18:28 +0300 | [diff] [blame] | 87 | assertThat(future.get(), IsEqual("OK")) |
Roman Elizarov | fc6461f | 2017-05-16 18:48:17 +0300 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | @Test |
Roman Elizarov | 489cac2 | 2017-05-17 12:18:28 +0300 | [diff] [blame] | 91 | fun testCompletedFutureExceptionally() { |
Roman Elizarov | ee89344 | 2017-01-19 14:56:21 +0300 | [diff] [blame] | 92 | val toAwait = CompletableFuture<String>() |
Roman Elizarov | 6121663 | 2018-05-18 11:22:51 +0300 | [diff] [blame] | 93 | toAwait.completeExceptionally(TestException("O")) |
| 94 | val future = future { |
Roman Elizarov | ee89344 | 2017-01-19 14:56:21 +0300 | [diff] [blame] | 95 | try { |
| 96 | toAwait.await() |
Roman Elizarov | 6121663 | 2018-05-18 11:22:51 +0300 | [diff] [blame] | 97 | } catch (e: TestException) { |
Roman Elizarov | ee89344 | 2017-01-19 14:56:21 +0300 | [diff] [blame] | 98 | e.message!! |
| 99 | } + "K" |
| 100 | } |
Roman Elizarov | 489cac2 | 2017-05-17 12:18:28 +0300 | [diff] [blame] | 101 | assertThat(future.get(), IsEqual("OK")) |
Roman Elizarov | ee89344 | 2017-01-19 14:56:21 +0300 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | @Test |
Roman Elizarov | 6121663 | 2018-05-18 11:22:51 +0300 | [diff] [blame] | 105 | // Test fast-path of CompletionStage.await() extension |
Roman Elizarov | 489cac2 | 2017-05-17 12:18:28 +0300 | [diff] [blame] | 106 | fun testCompletedCompletionStageExceptionally() { |
Roman Elizarov | fc6461f | 2017-05-16 18:48:17 +0300 | [diff] [blame] | 107 | val completable = CompletableFuture<String>() |
| 108 | val toAwait: CompletionStage<String> = completable |
Roman Elizarov | 6121663 | 2018-05-18 11:22:51 +0300 | [diff] [blame] | 109 | completable.completeExceptionally(TestException("O")) |
| 110 | val future = future { |
Roman Elizarov | fc6461f | 2017-05-16 18:48:17 +0300 | [diff] [blame] | 111 | try { |
| 112 | toAwait.await() |
Roman Elizarov | 6121663 | 2018-05-18 11:22:51 +0300 | [diff] [blame] | 113 | } catch (e: TestException) { |
Roman Elizarov | fc6461f | 2017-05-16 18:48:17 +0300 | [diff] [blame] | 114 | e.message!! |
| 115 | } + "K" |
| 116 | } |
Roman Elizarov | 489cac2 | 2017-05-17 12:18:28 +0300 | [diff] [blame] | 117 | assertThat(future.get(), IsEqual("OK")) |
Roman Elizarov | fc6461f | 2017-05-16 18:48:17 +0300 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | @Test |
Roman Elizarov | 6121663 | 2018-05-18 11:22:51 +0300 | [diff] [blame] | 121 | // Test slow-path of CompletionStage.await() extension |
| 122 | fun testWaitForFutureWithException() = runTest { |
| 123 | expect(1) |
Denis Zharkov | 8e4e0e4 | 2016-06-22 18:27:19 +0300 | [diff] [blame] | 124 | val toAwait = CompletableFuture<String>() |
Roman Elizarov | 6121663 | 2018-05-18 11:22:51 +0300 | [diff] [blame] | 125 | val future = future(coroutineContext, start = CoroutineStart.UNDISPATCHED) { |
Denis Zharkov | 8e4e0e4 | 2016-06-22 18:27:19 +0300 | [diff] [blame] | 126 | try { |
Roman Elizarov | 6121663 | 2018-05-18 11:22:51 +0300 | [diff] [blame] | 127 | expect(2) |
| 128 | toAwait.await() // will suspend (slow path) |
| 129 | } catch (e: TestException) { |
| 130 | expect(4) |
Denis Zharkov | 8e4e0e4 | 2016-06-22 18:27:19 +0300 | [diff] [blame] | 131 | e.message!! |
| 132 | } + "K" |
| 133 | } |
Roman Elizarov | 6121663 | 2018-05-18 11:22:51 +0300 | [diff] [blame] | 134 | expect(3) |
Denis Zharkov | 8e4e0e4 | 2016-06-22 18:27:19 +0300 | [diff] [blame] | 135 | assertFalse(future.isDone) |
Roman Elizarov | 6121663 | 2018-05-18 11:22:51 +0300 | [diff] [blame] | 136 | toAwait.completeExceptionally(TestException("O")) |
| 137 | yield() // to future coroutine |
Roman Elizarov | 489cac2 | 2017-05-17 12:18:28 +0300 | [diff] [blame] | 138 | assertThat(future.get(), IsEqual("OK")) |
Roman Elizarov | 6121663 | 2018-05-18 11:22:51 +0300 | [diff] [blame] | 139 | finish(5) |
Roman Elizarov | fc6461f | 2017-05-16 18:48:17 +0300 | [diff] [blame] | 140 | } |
Denis Zharkov | 8e4e0e4 | 2016-06-22 18:27:19 +0300 | [diff] [blame] | 141 | |
Roman Elizarov | fc6461f | 2017-05-16 18:48:17 +0300 | [diff] [blame] | 142 | @Test |
Roman Elizarov | 489cac2 | 2017-05-17 12:18:28 +0300 | [diff] [blame] | 143 | fun testWaitForCompletionStageWithException() { |
Roman Elizarov | fc6461f | 2017-05-16 18:48:17 +0300 | [diff] [blame] | 144 | val completable = CompletableFuture<String>() |
| 145 | val toAwait: CompletionStage<String> = completable |
Roman Elizarov | 6121663 | 2018-05-18 11:22:51 +0300 | [diff] [blame] | 146 | val future = future { |
Roman Elizarov | fc6461f | 2017-05-16 18:48:17 +0300 | [diff] [blame] | 147 | try { |
| 148 | toAwait.await() |
Roman Elizarov | 6121663 | 2018-05-18 11:22:51 +0300 | [diff] [blame] | 149 | } catch (e: TestException) { |
Roman Elizarov | fc6461f | 2017-05-16 18:48:17 +0300 | [diff] [blame] | 150 | e.message!! |
| 151 | } + "K" |
| 152 | } |
| 153 | assertFalse(future.isDone) |
Roman Elizarov | 6121663 | 2018-05-18 11:22:51 +0300 | [diff] [blame] | 154 | completable.completeExceptionally(TestException("O")) |
Roman Elizarov | 489cac2 | 2017-05-17 12:18:28 +0300 | [diff] [blame] | 155 | assertThat(future.get(), IsEqual("OK")) |
Denis Zharkov | 8e4e0e4 | 2016-06-22 18:27:19 +0300 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | @Test |
| 159 | fun testExceptionInsideCoroutine() { |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 160 | val future = future { |
Denis Zharkov | b483312 | 2016-12-15 11:49:03 +0300 | [diff] [blame] | 161 | if (CompletableFuture.supplyAsync { true }.await()) { |
Denis Zharkov | 8e4e0e4 | 2016-06-22 18:27:19 +0300 | [diff] [blame] | 162 | throw IllegalStateException("OK") |
| 163 | } |
Roman Elizarov | 489cac2 | 2017-05-17 12:18:28 +0300 | [diff] [blame] | 164 | "fail" |
Denis Zharkov | 8e4e0e4 | 2016-06-22 18:27:19 +0300 | [diff] [blame] | 165 | } |
Denis Zharkov | 8e4e0e4 | 2016-06-22 18:27:19 +0300 | [diff] [blame] | 166 | try { |
| 167 | future.get() |
| 168 | fail("'get' should've throw an exception") |
| 169 | } catch (e: ExecutionException) { |
| 170 | assertTrue(e.cause is IllegalStateException) |
Roman Elizarov | 489cac2 | 2017-05-17 12:18:28 +0300 | [diff] [blame] | 171 | assertThat(e.cause!!.message, IsEqual("OK")) |
Denis Zharkov | 8e4e0e4 | 2016-06-22 18:27:19 +0300 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
| 175 | @Test |
Roman Elizarov | 489cac2 | 2017-05-17 12:18:28 +0300 | [diff] [blame] | 176 | fun testCompletedDeferredAsCompletableFuture() = runBlocking { |
| 177 | expect(1) |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 178 | val deferred = async(coroutineContext, CoroutineStart.UNDISPATCHED) { |
Roman Elizarov | 489cac2 | 2017-05-17 12:18:28 +0300 | [diff] [blame] | 179 | expect(2) // completed right away |
| 180 | "OK" |
| 181 | } |
| 182 | expect(3) |
| 183 | val future = deferred.asCompletableFuture() |
| 184 | assertThat(future.await(), IsEqual("OK")) |
| 185 | finish(4) |
| 186 | } |
| 187 | |
| 188 | @Test |
| 189 | fun testWaitForDeferredAsCompletableFuture() = runBlocking { |
| 190 | expect(1) |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 191 | val deferred = async(coroutineContext) { |
Roman Elizarov | 489cac2 | 2017-05-17 12:18:28 +0300 | [diff] [blame] | 192 | expect(3) // will complete later |
| 193 | "OK" |
| 194 | } |
| 195 | expect(2) |
| 196 | val future = deferred.asCompletableFuture() |
| 197 | assertThat(future.await(), IsEqual("OK")) // await yields main thread to deferred coroutine |
| 198 | finish(4) |
| 199 | } |
| 200 | |
| 201 | @Test |
| 202 | fun testCancellableAwaitFuture() = runBlocking { |
| 203 | expect(1) |
| 204 | val toAwait = CompletableFuture<String>() |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 205 | val job = launch(coroutineContext, CoroutineStart.UNDISPATCHED) { |
Roman Elizarov | 489cac2 | 2017-05-17 12:18:28 +0300 | [diff] [blame] | 206 | expect(2) |
| 207 | try { |
| 208 | toAwait.await() // suspends |
| 209 | } catch (e: CancellationException) { |
| 210 | expect(5) // should throw cancellation exception |
| 211 | throw e |
| 212 | } |
| 213 | } |
| 214 | expect(3) |
| 215 | job.cancel() // cancel the job |
| 216 | toAwait.complete("fail") // too late, the waiting job was already cancelled |
| 217 | expect(4) // job processing of cancellation was scheduled, not executed yet |
| 218 | yield() // yield main thread to job |
| 219 | finish(6) |
| 220 | } |
| 221 | |
| 222 | @Test |
Denis Zharkov | 8e4e0e4 | 2016-06-22 18:27:19 +0300 | [diff] [blame] | 223 | fun testContinuationWrapped() { |
| 224 | val depth = AtomicInteger() |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 225 | val future = future(wrapContinuation { |
Denis Zharkov | 8e4e0e4 | 2016-06-22 18:27:19 +0300 | [diff] [blame] | 226 | depth.andIncrement |
| 227 | it() |
| 228 | depth.andDecrement |
| 229 | }) { |
Roman Elizarov | 12f961d | 2017-02-06 15:44:03 +0300 | [diff] [blame] | 230 | assertEquals("Part before first suspension must be wrapped", 1, depth.get()) |
Denis Zharkov | 8e4e0e4 | 2016-06-22 18:27:19 +0300 | [diff] [blame] | 231 | val result = |
Denis Zharkov | b483312 | 2016-12-15 11:49:03 +0300 | [diff] [blame] | 232 | CompletableFuture.supplyAsync { |
Jonathan Cornaz | 7bd2c50 | 2018-02-28 15:59:21 +0100 | [diff] [blame] | 233 | while (depth.get() > 0); |
Roman Elizarov | 12f961d | 2017-02-06 15:44:03 +0300 | [diff] [blame] | 234 | assertEquals("Part inside suspension point should not be wrapped", 0, depth.get()) |
Denis Zharkov | 8e4e0e4 | 2016-06-22 18:27:19 +0300 | [diff] [blame] | 235 | "OK" |
Denis Zharkov | b483312 | 2016-12-15 11:49:03 +0300 | [diff] [blame] | 236 | }.await() |
Roman Elizarov | 12f961d | 2017-02-06 15:44:03 +0300 | [diff] [blame] | 237 | assertEquals("Part after first suspension should be wrapped", 1, depth.get()) |
Denis Zharkov | b483312 | 2016-12-15 11:49:03 +0300 | [diff] [blame] | 238 | CompletableFuture.supplyAsync { |
Jonathan Cornaz | 7bd2c50 | 2018-02-28 15:59:21 +0100 | [diff] [blame] | 239 | while (depth.get() > 0); |
Roman Elizarov | 12f961d | 2017-02-06 15:44:03 +0300 | [diff] [blame] | 240 | assertEquals("Part inside suspension point should not be wrapped", 0, depth.get()) |
Denis Zharkov | 8e4e0e4 | 2016-06-22 18:27:19 +0300 | [diff] [blame] | 241 | "ignored" |
Denis Zharkov | b483312 | 2016-12-15 11:49:03 +0300 | [diff] [blame] | 242 | }.await() |
Denis Zharkov | 8e4e0e4 | 2016-06-22 18:27:19 +0300 | [diff] [blame] | 243 | result |
| 244 | } |
Roman Elizarov | 489cac2 | 2017-05-17 12:18:28 +0300 | [diff] [blame] | 245 | assertThat(future.get(), IsEqual("OK")) |
Denis Zharkov | 8e4e0e4 | 2016-06-22 18:27:19 +0300 | [diff] [blame] | 246 | } |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 247 | |
Jonathan Cornaz | 7bd2c50 | 2018-02-28 15:59:21 +0100 | [diff] [blame] | 248 | @Test |
| 249 | fun testCompletableFutureStageAsDeferred() = runBlocking { |
| 250 | val lock = ReentrantLock().apply { lock() } |
| 251 | |
| 252 | val deferred: Deferred<Int> = CompletableFuture.supplyAsync { |
| 253 | lock.withLock { 42 } |
| 254 | }.asDeferred() |
| 255 | |
| 256 | assertFalse(deferred.isCompleted) |
| 257 | lock.unlock() |
| 258 | |
| 259 | assertEquals(42, deferred.await()) |
| 260 | assertTrue(deferred.isCompleted) |
| 261 | } |
| 262 | |
| 263 | @Test |
| 264 | fun testCompletedFutureAsDeferred() = runBlocking { |
| 265 | val deferred: Deferred<Int> = CompletableFuture.completedFuture(42).asDeferred() |
| 266 | assertEquals(42, deferred.await()) |
| 267 | } |
| 268 | |
| 269 | @Test |
| 270 | fun testFailedFutureAsDeferred() = runBlocking { |
Roman Elizarov | 19c1f2e | 2018-03-01 22:15:58 +0300 | [diff] [blame] | 271 | val future = CompletableFuture<Int>().apply { |
| 272 | completeExceptionally(TestException("something went wrong")) |
| 273 | } |
Jonathan Cornaz | 7bd2c50 | 2018-02-28 15:59:21 +0100 | [diff] [blame] | 274 | val deferred = future.asDeferred() |
| 275 | |
| 276 | assertTrue(deferred.isCompletedExceptionally) |
Roman Elizarov | 19c1f2e | 2018-03-01 22:15:58 +0300 | [diff] [blame] | 277 | val completionException = deferred.getCompletionExceptionOrNull()!! |
| 278 | assertTrue(completionException is TestException) |
| 279 | assertEquals("something went wrong", completionException.message) |
Jonathan Cornaz | 7bd2c50 | 2018-02-28 15:59:21 +0100 | [diff] [blame] | 280 | |
| 281 | try { |
| 282 | deferred.await() |
| 283 | fail("deferred.await() should throw an exception") |
| 284 | } catch (e: Exception) { |
Roman Elizarov | 19c1f2e | 2018-03-01 22:15:58 +0300 | [diff] [blame] | 285 | assertTrue(e is TestException) |
| 286 | assertEquals("something went wrong", e.message) |
Jonathan Cornaz | 7bd2c50 | 2018-02-28 15:59:21 +0100 | [diff] [blame] | 287 | } |
| 288 | } |
| 289 | |
| 290 | @Test |
| 291 | fun testCompletableFutureWithExceptionAsDeferred() = runBlocking { |
| 292 | val lock = ReentrantLock().apply { lock() } |
| 293 | |
| 294 | val deferred: Deferred<Int> = CompletableFuture.supplyAsync { |
Roman Elizarov | 19c1f2e | 2018-03-01 22:15:58 +0300 | [diff] [blame] | 295 | lock.withLock { throw TestException("something went wrong") } |
Jonathan Cornaz | 7bd2c50 | 2018-02-28 15:59:21 +0100 | [diff] [blame] | 296 | }.asDeferred() |
| 297 | |
| 298 | assertFalse(deferred.isCompleted) |
| 299 | lock.unlock() |
| 300 | |
| 301 | try { |
| 302 | deferred.await() |
| 303 | fail("deferred.await() should throw an exception") |
| 304 | } catch (e: Exception) { |
| 305 | assertTrue(deferred.isCompletedExceptionally) |
Roman Elizarov | 19c1f2e | 2018-03-01 22:15:58 +0300 | [diff] [blame] | 306 | assertTrue(e is CompletionException) // that's how supplyAsync wraps it |
| 307 | val cause = e.cause!! |
| 308 | assertTrue(cause is TestException) |
| 309 | assertEquals("something went wrong", cause.message) |
| 310 | assertSame(e, deferred.getCompletionExceptionOrNull()) // same exception is returns as thrown |
Jonathan Cornaz | 7bd2c50 | 2018-02-28 15:59:21 +0100 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | |
Roman Elizarov | 19c1f2e | 2018-03-01 22:15:58 +0300 | [diff] [blame] | 314 | class TestException(message: String) : Exception(message) |
| 315 | |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 316 | private fun wrapContinuation(wrapper: (() -> Unit) -> Unit): CoroutineDispatcher = object : CoroutineDispatcher() { |
Roman Elizarov | 67891d8 | 2017-01-23 16:47:20 +0300 | [diff] [blame] | 317 | override fun dispatch(context: CoroutineContext, block: Runnable) { |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 318 | wrapper { |
| 319 | block.run() |
| 320 | } |
| 321 | } |
| 322 | } |
Denis Zharkov | 8e4e0e4 | 2016-06-22 18:27:19 +0300 | [diff] [blame] | 323 | } |