-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce SupervisorJob & supervisorScope
This change also fixes propagation of cancellation for Job() constructor. When both Job() and SupervisorJob() are cancelled with exception (fail), they cancel their parent, too. So we have similar behavior between: * Job() and coroutineScope { ... } * SupervisorJob() and supervisorScope { ... } Fixes #576
- Loading branch information
Showing
9 changed files
with
195 additions
and
11 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.coroutines.experimental | ||
|
||
import kotlin.coroutines.experimental.* | ||
|
||
/** | ||
* Creates a new _supervisor_ job object in an active state. | ||
* Children of a supervisor job can fail independently of each other. | ||
* | ||
* A failure or cancellation of a child does not cause the supervisor job to fail and does not affect its other children, | ||
* so a supervisor can implement a custom policy for handling failures of its children: | ||
* | ||
* * A failure of a child job that was created using [launch][CoroutineScope.launch] can be handled via [CoroutineExceptionHandler] in the context. | ||
* * A failure of a child job that was created using [async][CoroutineScope.async] can be handled via [Deferred.await] on the resulting deferred value. | ||
* | ||
* If [parent] job is specified, then this supervisor job becomes a child job of its parent and is cancelled when its | ||
* parent fails or is cancelled. All this supervisor's children are cancelled in this case, too. The invocation of | ||
* of [cancel][Job.cancel] with exception (other than [CancellationException]) on this supervisor job also cancels parent. | ||
* | ||
* @param parent an optional parent job. | ||
*/ | ||
@Suppress("FunctionName") | ||
public fun SupervisorJob(parent: Job? = null) : Job = SupervisorJobImpl(parent) | ||
|
||
/** | ||
* Creates new [CoroutineScope] with [SupervisorJob] and calls the specified suspend block with this scope. | ||
* The provided scope inherits its [coroutineContext][CoroutineScope.coroutineContext] from the outer scope, but overrides | ||
* context's [Job] with [SupervisorJob]. | ||
* | ||
* A failure of a child does not cause this scope to fail and does not affect its other children, | ||
* so a custom policy for handling failures of its children can be implemented. See [SupervisorJob] for details. | ||
*/ | ||
public suspend fun <R> supervisorScope(block: suspend CoroutineScope.() -> R): R { | ||
// todo: optimize implementation to a single allocated object | ||
// todo: fix copy-and-paste with coroutineScope | ||
val owner = SupervisorCoroutine<R>(coroutineContext) | ||
owner.start(CoroutineStart.UNDISPATCHED, owner, block) | ||
owner.join() | ||
if (owner.isCancelled) { | ||
throw owner.getCancellationException().let { it.cause ?: it } | ||
} | ||
val state = owner.state | ||
if (state is CompletedExceptionally) { | ||
throw state.cause | ||
} | ||
@Suppress("UNCHECKED_CAST") | ||
return state as R | ||
|
||
} | ||
|
||
private class SupervisorJobImpl(parent: Job?) : JobSupport(true) { | ||
init { initParentJobInternal(parent) } | ||
override val cancelsParent: Boolean get() = true | ||
override val onCancelComplete get() = true | ||
override val handlesException: Boolean get() = false | ||
override fun childCancelled(cause: Throwable): Boolean = false | ||
} | ||
|
||
private class SupervisorCoroutine<R>( | ||
parentContext: CoroutineContext | ||
) : AbstractCoroutine<R>(parentContext, true) { | ||
override val cancelsParent: Boolean get() = true | ||
override fun childCancelled(cause: Throwable): Boolean = false | ||
} |
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
76 changes: 76 additions & 0 deletions
76
common/kotlinx-coroutines-core-common/test/SupervisorTest.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,76 @@ | ||
/* | ||
* Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
@file:Suppress("NAMED_ARGUMENTS_NOT_ALLOWED") // KT-21913 | ||
|
||
package kotlinx.coroutines.experimental | ||
|
||
import kotlin.test.* | ||
|
||
class SupervisorTest : TestBase() { | ||
@Test | ||
fun testSupervisorJob() = runTest( | ||
unhandled = listOf( | ||
{ it -> it is TestException2 }, | ||
{ it -> it is TestException1 } | ||
) | ||
) { | ||
expect(1) | ||
val supervisor = SupervisorJob() | ||
val job1 = launch(supervisor + CoroutineName("job1")) { | ||
expect(2) | ||
yield() // to second child | ||
expect(4) | ||
throw TestException1() | ||
} | ||
val job2 = launch(supervisor + CoroutineName("job2")) { | ||
expect(3) | ||
throw TestException2() | ||
} | ||
joinAll(job1, job2) | ||
finish(5) | ||
assertTrue(job1.isCancelled) | ||
assertTrue(job2.isCancelled) | ||
} | ||
|
||
@Test | ||
fun testSupervisorScope() = runTest( | ||
unhandled = listOf( | ||
{ it -> it is TestException1 }, | ||
{ it -> it is TestException2 } | ||
) | ||
) { | ||
val result = supervisorScope { | ||
launch { | ||
throw TestException1() | ||
} | ||
launch { | ||
throw TestException2() | ||
} | ||
"OK" | ||
} | ||
assertEquals("OK", result) | ||
} | ||
|
||
@Test | ||
fun testSupervisorWithParentCancelNormally() { | ||
val parent = Job() | ||
val supervisor = SupervisorJob(parent) | ||
supervisor.cancel() | ||
assertTrue(supervisor.isCancelled) | ||
assertFalse(parent.isCancelled) | ||
} | ||
|
||
@Test | ||
fun testSupervisorWithParentCancelException() { | ||
val parent = Job() | ||
val supervisor = SupervisorJob(parent) | ||
supervisor.cancel(TestException1()) | ||
assertTrue(supervisor.isCancelled) | ||
assertTrue(parent.isCancelled) | ||
} | ||
|
||
private class TestException1 : Exception() | ||
private class TestException2 : Exception() | ||
} |
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