|
| 1 | +/* |
| 2 | + * Copyright 2016-2022 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 com.google.common.reflect.* |
| 8 | +import kotlinx.coroutines.* |
| 9 | +import org.junit.Test |
| 10 | +import kotlin.test.* |
| 11 | + |
| 12 | +class ListAllCoroutineThrowableSubclassesTest : TestBase() { |
| 13 | + |
| 14 | + /* |
| 15 | + * These are all known throwables in kotlinx.coroutines. |
| 16 | + * If you have added one, this test will fail to make |
| 17 | + * you ensure your exception type is java.io.Serializable. |
| 18 | + * |
| 19 | + * We do not have means to check it automatically, so checks are delegated to humans. |
| 20 | + * Also, this test meant to be in kotlinx-coroutines-core, but properly scanning classpath |
| 21 | + * requires guava which is toxic dependency that we'd like to avoid even in tests. |
| 22 | + * |
| 23 | + * See #3328 for serialization rationale. |
| 24 | + */ |
| 25 | + private val knownThrowables = setOf( |
| 26 | + "kotlinx.coroutines.TimeoutCancellationException", |
| 27 | + "kotlinx.coroutines.JobCancellationException", |
| 28 | + "kotlinx.coroutines.internal.UndeliveredElementException", |
| 29 | + "kotlinx.coroutines.CompletionHandlerException", |
| 30 | + "kotlinx.coroutines.DiagnosticCoroutineContextException", |
| 31 | + "kotlinx.coroutines.CoroutinesInternalError", |
| 32 | + "kotlinx.coroutines.channels.ClosedSendChannelException", |
| 33 | + "kotlinx.coroutines.channels.ClosedReceiveChannelException", |
| 34 | + "kotlinx.coroutines.flow.internal.ChildCancelledException", |
| 35 | + "kotlinx.coroutines.flow.internal.AbortFlowException", |
| 36 | + |
| 37 | + ) |
| 38 | + |
| 39 | + @Test |
| 40 | + fun testThrowableSubclassesAreSerializable() { |
| 41 | + var throwables = 0 |
| 42 | + val classes = ClassPath.from(this.javaClass.classLoader) |
| 43 | + .getTopLevelClassesRecursive("kotlinx.coroutines"); |
| 44 | + classes.forEach { |
| 45 | + try { |
| 46 | + if (Throwable::class.java.isAssignableFrom(it.load())) { |
| 47 | + // Skip classes from test sources |
| 48 | + if (it.load().protectionDomain.codeSource.location.toString().contains("/test/")) { |
| 49 | + return@forEach |
| 50 | + } |
| 51 | + ++throwables |
| 52 | + // println(""""$it",""") |
| 53 | + assertTrue(knownThrowables.contains(it.toString())) |
| 54 | + } |
| 55 | + } catch (e: Throwable) { |
| 56 | + // Ignore unloadable classes |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + assertEquals(knownThrowables.size, throwables) |
| 61 | + } |
| 62 | +} |
0 commit comments