|
| 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