forked from Kotlin/kotlinx.coroutines
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 (Kotli…
…n#3337) * Ensure that all coroutines throwables in core are serializable Fixes Kotlin#3328
- Loading branch information
1 parent
db5f172
commit 064785d
Showing
6 changed files
with
107 additions
and
5 deletions.
There are no files selected for viewing
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
43 changes: 43 additions & 0 deletions
43
integration-testing/src/withGuavaTest/kotlin/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,43 @@ | ||
/* | ||
* 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 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() } | ||
assertEquals(knownThrowables.sorted(), throwables.sorted()) | ||
} | ||
} |
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) | ||
} | ||
} | ||
} |