-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Do not throw from JobListenableFuture.isCancelled (#2498)
* Fix toString representation of JobListenableFuture * Do not throw from JobListenableFuture.isCancelled. This properly handles ExecutionException that can be thrown from getUninterruptibly. Fixes #2421 Co-authored-by: Vsevolod Tolstopyatov <qwwdfsad@gmail.com>
- Loading branch information
1 parent
76b12f9
commit d37b834
Showing
3 changed files
with
141 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
integration/kotlinx-coroutines-guava/test/ListenableFutureToStringTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.coroutines.guava | ||
|
||
import kotlinx.coroutines.* | ||
import org.junit.Test | ||
import kotlin.test.* | ||
|
||
class ListenableFutureToStringTest : TestBase() { | ||
@Test | ||
fun testSuccessfulFuture() = runTest { | ||
val deferred = CompletableDeferred("OK") | ||
val succeededFuture = deferred.asListenableFuture() | ||
val toString = succeededFuture.toString() | ||
assertTrue(message = "Unexpected format: $toString") { | ||
toString.matches(Regex("""kotlinx\.coroutines\.guava\.JobListenableFuture@[^\[]*\[status=SUCCESS, result=\[OK]]""")) | ||
} | ||
} | ||
|
||
@Test | ||
fun testFailedFuture() = runTest { | ||
val exception = TestRuntimeException("test") | ||
val deferred = CompletableDeferred<String>().apply { | ||
completeExceptionally(exception) | ||
} | ||
val failedFuture = deferred.asListenableFuture() | ||
val toString = failedFuture.toString() | ||
assertTrue(message = "Unexpected format: $toString") { | ||
toString.matches(Regex("""kotlinx\.coroutines\.guava\.JobListenableFuture@[^\[]*\[status=FAILURE, cause=\[$exception]]""")) | ||
} | ||
} | ||
|
||
@Test | ||
fun testPendingFuture() = runTest { | ||
val deferred = CompletableDeferred<String>() | ||
val pendingFuture = deferred.asListenableFuture() | ||
val toString = pendingFuture.toString() | ||
assertTrue(message = "Unexpected format: $toString") { | ||
toString.matches(Regex("""kotlinx\.coroutines\.guava\.JobListenableFuture@[^\[]*\[status=PENDING, delegate=\[.*]]""")) | ||
} | ||
} | ||
|
||
@Test | ||
fun testCancelledCoroutineAsListenableFuture() = runTest { | ||
val exception = CancellationException("test") | ||
val deferred = CompletableDeferred<String>().apply { | ||
cancel(exception) | ||
} | ||
val cancelledFuture = deferred.asListenableFuture() | ||
val toString = cancelledFuture.toString() | ||
assertTrue(message = "Unexpected format: $toString") { | ||
toString.matches(Regex("""kotlinx\.coroutines\.guava\.JobListenableFuture@[^\[]*\[status=CANCELLED, cause=\[$exception]]""")) | ||
} | ||
} | ||
|
||
@Test | ||
fun testCancelledFuture() = runTest { | ||
val deferred = CompletableDeferred<String>() | ||
val cancelledFuture = deferred.asListenableFuture().apply { | ||
cancel(false) | ||
} | ||
val toString = cancelledFuture.toString() | ||
assertTrue(message = "Unexpected format: $toString") { | ||
toString.matches(Regex("""kotlinx\.coroutines\.guava\.JobListenableFuture@[^\[]*\[status=CANCELLED]""")) | ||
} | ||
} | ||
} |