Skip to content

Remove requirement that job of the pre-created JobCancelNode have to … #2427

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 3 commits into from
Dec 7, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
15 changes: 9 additions & 6 deletions kotlinx-coroutines-core/common/src/JobSupport.kt
Original file line number Diff line number Diff line change
Expand Up @@ -508,14 +508,17 @@ public open class JobSupport constructor(active: Boolean) : Job, ChildJob, Paren
}
}

private fun makeNode(handler: CompletionHandler, onCancelling: Boolean): JobNode<*> {
return if (onCancelling)
(handler as? JobCancellingNode<*>)?.also { assert { it.job === this } }
private fun makeNode(handler: CompletionHandler, onCancelling: Boolean): JobNode<*> =
if (onCancelling) {
(handler as? JobCancellingNode<*>)
?.takeIf { it.job === this }
?: InvokeOnCancelling(this, handler)
else
(handler as? JobNode<*>)?.also { assert { it.job === this && it !is JobCancellingNode } }
} else {
(handler as? JobNode<*>)
?.also { assert { it !is JobCancellingNode } }
?.takeIf { it.job === this }
?: InvokeOnCompletion(this, handler)
}
}

private fun addLastAtomic(expect: Any, list: NodeList, node: JobNode<*>) =
list.addLastIf(node) { this.state === expect }
Expand Down
28 changes: 28 additions & 0 deletions kotlinx-coroutines-core/common/test/AwaitTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -351,4 +351,32 @@ class AwaitTest : TestBase() {
async(NonCancellable) { throw TestException() }
joinAll(job, job, job)
}

@Test
fun testAwaitAllDelegates() = runTest {
expect(1)
val deferred = CompletableDeferred<String>()
val delegate = object : Deferred<String> by deferred {}
launch {
expect(3)
deferred.complete("OK")
}
expect(2)
awaitAll(delegate)
finish(4)
}

@Test
fun testCancelAwaitAllDelegate() = runTest {
expect(1)
val deferred = CompletableDeferred<String>()
val delegate = object : Deferred<String> by deferred {}
launch {
expect(3)
deferred.cancel()
}
expect(2)
assertFailsWith<CancellationException> { awaitAll(delegate) }
finish(4)
}
}