/
ListAllCoroutineThrowableSubclassesTest.kt
53 lines (48 loc) · 2.11 KB
/
ListAllCoroutineThrowableSubclassesTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
* Copyright 2016-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.coroutines
import com.google.common.reflect.*
import kotlinx.coroutines.*
import org.junit.Test
import java.io.Serializable
import java.lang.reflect.Modifier
import kotlin.test.*
class ListAllCoroutineThrowableSubclassesTest {
/*
* These are all the known throwables in kotlinx.coroutines.
* If you add one, this test will fail to make
* you ensure your exception type is java.io.Serializable.
*
* We do not have means to check it automatically, so checks are delegated to humans.
*
* See #3328 for serialization rationale.
*/
private val knownThrowables = setOf(
"kotlinx.coroutines.TimeoutCancellationException",
"kotlinx.coroutines.JobCancellationException",
"kotlinx.coroutines.internal.UndeliveredElementException",
"kotlinx.coroutines.CompletionHandlerException",
"kotlinx.coroutines.DiagnosticCoroutineContextException",
"kotlinx.coroutines.CoroutinesInternalError",
"kotlinx.coroutines.channels.ClosedSendChannelException",
"kotlinx.coroutines.channels.ClosedReceiveChannelException",
"kotlinx.coroutines.flow.internal.ChildCancelledException",
"kotlinx.coroutines.flow.internal.AbortFlowException",
)
@Test
fun testThrowableSubclassesAreSerializable() {
val classes = ClassPath.from(this.javaClass.classLoader)
.getTopLevelClassesRecursive("kotlinx.coroutines");
val throwables = classes.filter { Throwable::class.java.isAssignableFrom(it.load()) }.map { it.toString() }
for (throwable in throwables) {
for (field in throwable.javaClass.declaredFields) {
if (Modifier.isStatic(field.modifiers)) continue
val type = field.type
assertTrue(type.isPrimitive || Serializable::class.java.isAssignableFrom(type),
"Throwable $throwable has non-serializable field $field")
}
}
assertEquals(knownThrowables.sorted(), throwables.sorted())
}
}