|
| 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 |
| 6 | + |
| 7 | +import com.google.common.reflect.* |
| 8 | +import kotlinx.coroutines.* |
| 9 | +import org.junit.Test |
| 10 | +import kotlin.test.* |
| 11 | + |
| 12 | +class ListAllCoroutineThrowableSubclassesTest { |
| 13 | + |
| 14 | + /* |
| 15 | + * These are all the known throwables in kotlinx.coroutines. |
| 16 | + * If you add 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 | + * |
| 21 | + * See #3328 for serialization rationale. |
| 22 | + */ |
| 23 | + private val knownThrowables = setOf( |
| 24 | + "kotlinx.coroutines.TimeoutCancellationException", |
| 25 | + "kotlinx.coroutines.JobCancellationException", |
| 26 | + "kotlinx.coroutines.internal.UndeliveredElementException", |
| 27 | + "kotlinx.coroutines.CompletionHandlerException", |
| 28 | + "kotlinx.coroutines.DiagnosticCoroutineContextException", |
| 29 | + "kotlinx.coroutines.CoroutinesInternalError", |
| 30 | + "kotlinx.coroutines.channels.ClosedSendChannelException", |
| 31 | + "kotlinx.coroutines.channels.ClosedReceiveChannelException", |
| 32 | + "kotlinx.coroutines.flow.internal.ChildCancelledException", |
| 33 | + "kotlinx.coroutines.flow.internal.AbortFlowException", |
| 34 | + ) |
| 35 | + |
| 36 | + @Test |
| 37 | + fun testThrowableSubclassesAreSerializable() { |
| 38 | + val classes = ClassPath.from(this.javaClass.classLoader) |
| 39 | + .getTopLevelClassesRecursive("kotlinx.coroutines"); |
| 40 | + val throwables = classes.filter { Throwable::class.java.isAssignableFrom(it.load()) }.map { it.toString() } |
| 41 | + assertEquals(knownThrowables.sorted(), throwables.sorted()) |
| 42 | + } |
| 43 | +} |
0 commit comments