From c4b1fbd7a13d41f13eac4416dd09170c16fccb19 Mon Sep 17 00:00:00 2001 From: Vadim Semenov Date: Tue, 26 Jan 2021 22:08:27 +0000 Subject: [PATCH] Fix toString representation of JobListenableFuture Also add tests for toString. --- .../src/ListenableFuture.kt | 3 +- .../test/ListenableFutureToStringTest.kt | 69 +++++++++++++++++++ 2 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 integration/kotlinx-coroutines-guava/test/ListenableFutureToStringTest.kt diff --git a/integration/kotlinx-coroutines-guava/src/ListenableFuture.kt b/integration/kotlinx-coroutines-guava/src/ListenableFuture.kt index 6d1fab3d69..612b528b1f 100644 --- a/integration/kotlinx-coroutines-guava/src/ListenableFuture.kt +++ b/integration/kotlinx-coroutines-guava/src/ListenableFuture.kt @@ -455,7 +455,7 @@ private class JobListenableFuture(private val jobToCancel: Job): ListenableFu try { when (val result = Uninterruptibles.getUninterruptibly(auxFuture)) { is Cancelled -> append("CANCELLED, cause=[${result.exception}]") - else -> append("SUCCESS, result=[$result") + else -> append("SUCCESS, result=[$result]") } } catch (e: CancellationException) { // `this` future was cancelled by `Future.cancel`. In this case there's no cause or message. @@ -469,6 +469,7 @@ private class JobListenableFuture(private val jobToCancel: Job): ListenableFu } else { append("PENDING, delegate=[$auxFuture]") } + append(']') } } diff --git a/integration/kotlinx-coroutines-guava/test/ListenableFutureToStringTest.kt b/integration/kotlinx-coroutines-guava/test/ListenableFutureToStringTest.kt new file mode 100644 index 0000000000..3fdcfdba10 --- /dev/null +++ b/integration/kotlinx-coroutines-guava/test/ListenableFutureToStringTest.kt @@ -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 success() = 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 failure() = runTest { + val exception = TestRuntimeException("test") + val deferred = CompletableDeferred().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 pending() = runTest { + val deferred = CompletableDeferred() + 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 cancelledCoroutine() = runTest { + val exception = CancellationException("test") + val deferred = CompletableDeferred().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 cancelledFuture() = runTest { + val deferred = CompletableDeferred() + 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]""")) + } + } +} \ No newline at end of file