Skip to content

Commit de91d72

Browse files
vadimsemenovqwwdfsad
authored andcommitted
Fix toString representation of JobListenableFuture
Also add tests for toString.
1 parent e00d7d8 commit de91d72

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ private class JobListenableFuture<T>(private val jobToCancel: Job): ListenableFu
455455
try {
456456
when (val result = Uninterruptibles.getUninterruptibly(auxFuture)) {
457457
is Cancelled -> append("CANCELLED, cause=[${result.exception}]")
458-
else -> append("SUCCESS, result=[$result")
458+
else -> append("SUCCESS, result=[$result]")
459459
}
460460
} catch (e: CancellationException) {
461461
// `this` future was cancelled by `Future.cancel`. In this case there's no cause or message.
@@ -469,6 +469,7 @@ private class JobListenableFuture<T>(private val jobToCancel: Job): ListenableFu
469469
} else {
470470
append("PENDING, delegate=[$auxFuture]")
471471
}
472+
append(']')
472473
}
473474
}
474475

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
package kotlinx.coroutines.guava
6+
7+
import kotlinx.coroutines.*
8+
import org.junit.Test
9+
import kotlin.test.*
10+
11+
class ListenableFutureToStringTest : TestBase() {
12+
@Test
13+
fun testSuccessfulFuture() = runTest {
14+
val deferred = CompletableDeferred("OK")
15+
val succeededFuture = deferred.asListenableFuture()
16+
val toString = succeededFuture.toString()
17+
assertTrue(message = "Unexpected format: $toString") {
18+
toString.matches(Regex("""kotlinx\.coroutines\.guava\.JobListenableFuture@[^\[]*\[status=SUCCESS, result=\[OK]]"""))
19+
}
20+
}
21+
22+
@Test
23+
fun testFailedFuture() = runTest {
24+
val exception = TestRuntimeException("test")
25+
val deferred = CompletableDeferred<String>().apply {
26+
completeExceptionally(exception)
27+
}
28+
val failedFuture = deferred.asListenableFuture()
29+
val toString = failedFuture.toString()
30+
assertTrue(message = "Unexpected format: $toString") {
31+
toString.matches(Regex("""kotlinx\.coroutines\.guava\.JobListenableFuture@[^\[]*\[status=FAILURE, cause=\[$exception]]"""))
32+
}
33+
}
34+
35+
@Test
36+
fun testPendingFuture() = runTest {
37+
val deferred = CompletableDeferred<String>()
38+
val pendingFuture = deferred.asListenableFuture()
39+
val toString = pendingFuture.toString()
40+
assertTrue(message = "Unexpected format: $toString") {
41+
toString.matches(Regex("""kotlinx\.coroutines\.guava\.JobListenableFuture@[^\[]*\[status=PENDING, delegate=\[.*]]"""))
42+
}
43+
}
44+
45+
@Test
46+
fun testCancelledCoroutineAsListenableFuture() = runTest {
47+
val exception = CancellationException("test")
48+
val deferred = CompletableDeferred<String>().apply {
49+
cancel(exception)
50+
}
51+
val cancelledFuture = deferred.asListenableFuture()
52+
val toString = cancelledFuture.toString()
53+
assertTrue(message = "Unexpected format: $toString") {
54+
toString.matches(Regex("""kotlinx\.coroutines\.guava\.JobListenableFuture@[^\[]*\[status=CANCELLED, cause=\[$exception]]"""))
55+
}
56+
}
57+
58+
@Test
59+
fun testCancelledFuture() = runTest {
60+
val deferred = CompletableDeferred<String>()
61+
val cancelledFuture = deferred.asListenableFuture().apply {
62+
cancel(false)
63+
}
64+
val toString = cancelledFuture.toString()
65+
assertTrue(message = "Unexpected format: $toString") {
66+
toString.matches(Regex("""kotlinx\.coroutines\.guava\.JobListenableFuture@[^\[]*\[status=CANCELLED]"""))
67+
}
68+
}
69+
}

0 commit comments

Comments
 (0)