|
| 1 | +package kotlinx.coroutines.exceptions |
| 2 | + |
| 3 | +import kotlinx.coroutines.* |
| 4 | +import org.junit.* |
| 5 | +import kotlin.coroutines.* |
| 6 | + |
| 7 | +class StackTraceRecoveryNestedScopesTest : TestBase() { |
| 8 | + |
| 9 | + private val TEST_MACROS = "TEST_NAME" |
| 10 | + |
| 11 | + private val expectedTrace = "kotlinx.coroutines.RecoverableTestException\n" + |
| 12 | + "\tat kotlinx.coroutines.exceptions.StackTraceRecoveryNestedScopesTest.failure(StackTraceRecoveryNestedScopesTest.kt:9)\n" + |
| 13 | + "\tat kotlinx.coroutines.exceptions.StackTraceRecoveryNestedScopesTest.access\$failure(StackTraceRecoveryNestedScopesTest.kt:7)\n" + |
| 14 | + "\tat kotlinx.coroutines.exceptions.StackTraceRecoveryNestedScopesTest\$createFailingAsync\$1.invokeSuspend(StackTraceRecoveryNestedScopesTest.kt:12)\n" + |
| 15 | + "\t(Coroutine boundary)\n" + |
| 16 | + "\tat kotlinx.coroutines.exceptions.StackTraceRecoveryNestedScopesTest\$callWithTimeout\$2.invokeSuspend(StackTraceRecoveryNestedScopesTest.kt:23)\n" + |
| 17 | + "\tat kotlinx.coroutines.exceptions.StackTraceRecoveryNestedScopesTest\$callCoroutineScope\$2.invokeSuspend(StackTraceRecoveryNestedScopesTest.kt:29)\n" + |
| 18 | + "\tat kotlinx.coroutines.exceptions.StackTraceRecoveryNestedScopesTest\$$TEST_MACROS\$1.invokeSuspend(StackTraceRecoveryNestedScopesTest.kt:36)\n" + |
| 19 | + "Caused by: kotlinx.coroutines.RecoverableTestException\n" + |
| 20 | + "\tat kotlinx.coroutines.exceptions.StackTraceRecoveryNestedScopesTest.failure(StackTraceRecoveryNestedScopesTest.kt:9)\n" + |
| 21 | + "\tat kotlinx.coroutines.exceptions.StackTraceRecoveryNestedScopesTest.access\$failure(StackTraceRecoveryNestedScopesTest.kt:7)\n" + |
| 22 | + "\tat kotlinx.coroutines.exceptions.StackTraceRecoveryNestedScopesTest\$createFailingAsync\$1.invokeSuspend(StackTraceRecoveryNestedScopesTest.kt:12)\n" + |
| 23 | + "\tat kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:32)" |
| 24 | + |
| 25 | + private fun failure(): String = throw RecoverableTestException() |
| 26 | + |
| 27 | + private fun CoroutineScope.createFailingAsync() = async { |
| 28 | + failure() |
| 29 | + } |
| 30 | + |
| 31 | + private suspend fun callWithContext(doYield: Boolean) = withContext(wrapperDispatcher(coroutineContext)) { |
| 32 | + if (doYield) yield() |
| 33 | + createFailingAsync().await() |
| 34 | + yield() |
| 35 | + } |
| 36 | + |
| 37 | + private suspend fun callWithTimeout(doYield: Boolean) = withTimeout(Long.MAX_VALUE) { |
| 38 | + if (doYield) yield() |
| 39 | + callWithContext(doYield) |
| 40 | + yield() |
| 41 | + } |
| 42 | + |
| 43 | + private suspend fun callCoroutineScope(doYield: Boolean) = coroutineScope { |
| 44 | + if (doYield) yield() |
| 45 | + callWithTimeout(doYield) |
| 46 | + yield() |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + fun testNestedScopes() = runTest { |
| 51 | + try { |
| 52 | + callCoroutineScope(false) |
| 53 | + } catch (e: Exception) { |
| 54 | + verifyStackTrace(e, expectedTrace.replace(TEST_MACROS, "testNestedScopes")) |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + fun testNestedScopesYield() = runTest { |
| 60 | + try { |
| 61 | + callCoroutineScope(true) |
| 62 | + } catch (e: Exception) { |
| 63 | + verifyStackTrace(e, expectedTrace.replace(TEST_MACROS, "testNestedScopesYield")) |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + fun testAwaitNestedScopes() = runTest { |
| 69 | + val deferred = async(NonCancellable) { |
| 70 | + callCoroutineScope(false) |
| 71 | + } |
| 72 | + |
| 73 | + verifyAwait(deferred) |
| 74 | + } |
| 75 | + |
| 76 | + private suspend fun verifyAwait(deferred: Deferred<Unit>) { |
| 77 | + try { |
| 78 | + deferred.await() |
| 79 | + } catch (e: Exception) { |
| 80 | + verifyStackTrace(e, |
| 81 | + "kotlinx.coroutines.RecoverableTestException\n" + |
| 82 | + "\tat kotlinx.coroutines.exceptions.StackTraceRecoveryNestedScopesTest.failure(StackTraceRecoveryNestedScopesTest.kt:23)\n" + |
| 83 | + "\tat kotlinx.coroutines.exceptions.StackTraceRecoveryNestedScopesTest.access\$failure(StackTraceRecoveryNestedScopesTest.kt:7)\n" + |
| 84 | + "\tat kotlinx.coroutines.exceptions.StackTraceRecoveryNestedScopesTest\$createFailingAsync\$1.invokeSuspend(StackTraceRecoveryNestedScopesTest.kt:26)\n" + |
| 85 | + "\t(Coroutine boundary)\n" + |
| 86 | + "\tat kotlinx.coroutines.exceptions.StackTraceRecoveryNestedScopesTest\$callWithTimeout\$2.invokeSuspend(StackTraceRecoveryNestedScopesTest.kt:37)\n" + |
| 87 | + "\tat kotlinx.coroutines.exceptions.StackTraceRecoveryNestedScopesTest\$callCoroutineScope\$2.invokeSuspend(StackTraceRecoveryNestedScopesTest.kt:43)\n" + |
| 88 | + "\tat kotlinx.coroutines.exceptions.StackTraceRecoveryNestedScopesTest\$testAwaitNestedScopes\$1\$deferred\$1.invokeSuspend(StackTraceRecoveryNestedScopesTest.kt:68)\n" + |
| 89 | + "\tat kotlinx.coroutines.DeferredCoroutine.await\$suspendImpl(Builders.common.kt:99)\n" + |
| 90 | + "\tat kotlinx.coroutines.exceptions.StackTraceRecoveryNestedScopesTest.verifyAwait(StackTraceRecoveryNestedScopesTest.kt:76)\n" + |
| 91 | + "\tat kotlinx.coroutines.exceptions.StackTraceRecoveryNestedScopesTest\$testAwaitNestedScopes\$1.invokeSuspend(StackTraceRecoveryNestedScopesTest.kt:71)\n" + |
| 92 | + "Caused by: kotlinx.coroutines.RecoverableTestException\n" + |
| 93 | + "\tat kotlinx.coroutines.exceptions.StackTraceRecoveryNestedScopesTest.failure(StackTraceRecoveryNestedScopesTest.kt:23)\n" + |
| 94 | + "\tat kotlinx.coroutines.exceptions.StackTraceRecoveryNestedScopesTest.access\$failure(StackTraceRecoveryNestedScopesTest.kt:7)\n" + |
| 95 | + "\tat kotlinx.coroutines.exceptions.StackTraceRecoveryNestedScopesTest\$createFailingAsync\$1.invokeSuspend(StackTraceRecoveryNestedScopesTest.kt:26)\n" + |
| 96 | + "\tat kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:32)") |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments