Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure that all coroutines throwables in core are serializable
Fixes #3328
- Loading branch information
Showing
5 changed files
with
108 additions
and
4 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
integration/kotlinx-coroutines-guava/test/ListAllCoroutineThrowableSubclassesTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright 2016-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.coroutines.guava | ||
|
||
import com.google.common.reflect.* | ||
import kotlinx.coroutines.* | ||
import org.junit.Test | ||
import kotlin.test.* | ||
|
||
class ListAllCoroutineThrowableSubclassesTest : TestBase() { | ||
|
||
/* | ||
* These are all known throwables in kotlinx.coroutines. | ||
* If you have added 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. | ||
* Also, this test meant to be in kotlinx-coroutines-core, but properly scanning classpath | ||
* requires guava which is toxic dependency that we'd like to avoid even in tests. | ||
* | ||
* 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() { | ||
var throwables = 0 | ||
val classes = ClassPath.from(this.javaClass.classLoader) | ||
.getTopLevelClassesRecursive("kotlinx.coroutines"); | ||
classes.forEach { | ||
try { | ||
if (Throwable::class.java.isAssignableFrom(it.load())) { | ||
// Skip classes from test sources | ||
if (it.load().protectionDomain.codeSource.location.toString().contains("/test/")) { | ||
return@forEach | ||
} | ||
++throwables | ||
// println(""""$it",""") | ||
assertTrue(knownThrowables.contains(it.toString())) | ||
} | ||
} catch (e: Throwable) { | ||
// Ignore unloadable classes | ||
} | ||
} | ||
|
||
assertEquals(knownThrowables.size, throwables) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
kotlinx-coroutines-core/jvm/test/JobCancellationExceptionSerializerTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright 2016-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.coroutines | ||
|
||
import org.junit.* | ||
import java.io.* | ||
|
||
|
||
@Suppress("BlockingMethodInNonBlockingContext") | ||
class JobCancellationExceptionSerializerTest : TestBase() { | ||
|
||
@Test | ||
fun testSerialization() = runTest { | ||
try { | ||
coroutineScope { | ||
expect(1) | ||
|
||
launch { | ||
expect(2) | ||
try { | ||
hang {} | ||
} catch (e: CancellationException) { | ||
throw RuntimeException("RE2", e) | ||
} | ||
} | ||
|
||
launch { | ||
expect(3) | ||
throw RuntimeException("RE1") | ||
} | ||
} | ||
} catch (e: Throwable) { | ||
// Should not fail | ||
ObjectOutputStream(ByteArrayOutputStream()).use { | ||
it.writeObject(e) | ||
} | ||
finish(4) | ||
} | ||
} | ||
} |