Skip to content

Improve documentation of CompletableJob.completeExceptionally, add te… #1536

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions kotlinx-coroutines-core/common/src/CompletableJob.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public interface CompletableJob : Job {
* This function transitions this job into _cancelled_ state if it was not completed or cancelled yet.
* However, that if this job has children, then it transitions into _cancelling_ state and becomes _cancelled_
* once all its children are [complete][isCompleted]. See [Job] for details.
*
* Its responsibility of the caller to properly handle and report the given [exception], all job's children will receive
* a [CancellationException] with the [exception] as a cause for the sake of diagnostic.
*/
public fun completeExceptionally(exception: Throwable): Boolean
}
57 changes: 55 additions & 2 deletions kotlinx-coroutines-core/common/test/CompletableJobTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package kotlinx.coroutines

import kotlin.test.*

class CompletableJobTest {
class CompletableJobTest : TestBase() {
@Test
fun testComplete() {
val job = Job()
Expand Down Expand Up @@ -46,4 +46,57 @@ class CompletableJobTest {
assertFalse(child.isActive)
assertFalse(parent.isActive)
}
}

@Test
fun testExceptionIsNotReportedToChildren() = parametrized { job ->
expect(1)
val child = launch(job) {
expect(2)
try {
// KT-33840
hang {}
} catch (e: Throwable) {
assertTrue(e is CancellationException)
assertTrue((if (RECOVER_STACK_TRACES) e.cause?.cause else e.cause) is TestException)
expect(4)
throw e
}
}
yield()
expect(3)
job.completeExceptionally(TestException())
child.join()
finish(5)
}

@Test
fun testCompleteExceptionallyDoesntAffectDeferred() = parametrized { job ->
expect(1)
val child = async(job) {
expect(2)
try {
// KT-33840
hang {}
} catch (e: Throwable) {
assertTrue(e is CancellationException)
assertTrue((if (RECOVER_STACK_TRACES) e.cause?.cause else e.cause) is TestException)
expect(4)
throw e
}
}
yield()
expect(3)
job.completeExceptionally(TestException())
child.join()
assertTrue { child.getCompletionExceptionOrNull() is CancellationException }
finish(5)
}

private fun parametrized(block: suspend CoroutineScope.(CompletableJob) -> Unit) {
runTest {
block(Job())
reset()
block(SupervisorJob())
}
}
}