Skip to content

Commit a334c4e

Browse files
committed
Catch Throwable instead of Exception in as*Future
Fixes #469
1 parent c82a54d commit a334c4e

File tree

4 files changed

+34
-14
lines changed

4 files changed

+34
-14
lines changed

integration/kotlinx-coroutines-guava/src/ListenableFuture.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ private class DeferredListenableFuture<T>(
9595
deferred.invokeOnCompletion {
9696
try {
9797
set(deferred.getCompleted())
98-
} catch (exception: Exception) {
99-
setException(exception)
98+
} catch (t: Throwable) {
99+
setException(t)
100100
}
101101
}
102102
}

integration/kotlinx-coroutines-guava/test/ListenableFutureTest.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,21 @@ class ListenableFutureTest : TestBase() {
153153
finish(4)
154154
}
155155

156+
@Test
157+
fun testAsListenableFutureThrowable() {
158+
val deferred = async {
159+
throw OutOfMemoryError()
160+
}
161+
162+
val future = deferred.asListenableFuture()
163+
try {
164+
future.get()
165+
} catch (e: ExecutionException) {
166+
assertTrue(future.isDone)
167+
assertTrue(e.cause is OutOfMemoryError)
168+
}
169+
}
170+
156171
@Test
157172
fun testCancellableAwait() = runBlocking {
158173
expect(1)

integration/kotlinx-coroutines-jdk8/src/future/Future.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ public fun <T> Deferred<T>.asCompletableFuture(): CompletableFuture<T> {
9393
invokeOnCompletion {
9494
try {
9595
future.complete(getCompleted())
96-
} catch (exception: Exception) {
97-
future.completeExceptionally(exception)
96+
} catch (t: Throwable) {
97+
future.completeExceptionally(t)
9898
}
9999
}
100100
return future

integration/kotlinx-coroutines-jdk8/test/FutureTest.kt

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,6 @@ class FutureTest : TestBase() {
2121
ignoreLostThreads("ForkJoinPool.commonPool-worker-")
2222
}
2323

24-
@Test
25-
fun testSimpleAwait() {
26-
val future = future {
27-
CompletableFuture.supplyAsync {
28-
"O"
29-
}.await() + "K"
30-
}
31-
assertThat(future.get(), IsEqual("OK"))
32-
}
33-
3424
@Test
3525
fun testCompletedFuture() {
3626
val toAwait = CompletableFuture<String>()
@@ -186,6 +176,21 @@ class FutureTest : TestBase() {
186176
finish(4)
187177
}
188178

179+
@Test
180+
fun testAsCompletableFutureThrowable() {
181+
val deferred = async {
182+
throw OutOfMemoryError()
183+
}
184+
185+
val future = deferred.asCompletableFuture()
186+
try {
187+
future.get()
188+
} catch (e: ExecutionException) {
189+
assertTrue(future.isCompletedExceptionally)
190+
assertTrue(e.cause is OutOfMemoryError)
191+
}
192+
}
193+
189194
@Test
190195
fun testCancellableAwaitFuture() = runBlocking {
191196
expect(1)

0 commit comments

Comments
 (0)