-
Notifications
You must be signed in to change notification settings - Fork 900
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
ScopeProvider.coroutineScope
: Deliver lifecycle exceptions to CoroutineExceptionHandler
instead of throwing at call-site.
#632
Open
psteiger
wants to merge
1
commit into
uber:main
Choose a base branch
from
psteiger:change-coroutine-scope-behavior
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
39 changes: 39 additions & 0 deletions
39
...d/libraries/rib-coroutines/src/main/kotlin/com/uber/rib/core/internal/DirectDispatcher.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,39 @@ | ||
/* | ||
* Copyright (C) 2024. Uber Technologies | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.uber.rib.core.internal | ||
|
||
import kotlin.coroutines.CoroutineContext | ||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.Runnable | ||
|
||
/** | ||
* A coroutine dispatcher that is not confined to any specific thread. It executes the initial | ||
* continuation of a coroutine in the current call-frame and lets the coroutine resume in whatever | ||
* thread that is used by the corresponding suspending function, without mandating any specific | ||
* threading policy. | ||
* | ||
* This dispatcher is similar to [Unconfined][com.uber.rib.core.RibDispatchers.Unconfined], with the | ||
* difference that it does not form an event-loop on nested coroutines, which implies that it has | ||
* predictable ordering of events with the tradeoff of a risk StackOverflowError. | ||
* | ||
* **This is internal API, not supposed to be used by library consumers.** | ||
*/ | ||
@CoroutinesFriendModuleApi | ||
public object DirectDispatcher : CoroutineDispatcher() { | ||
override fun dispatch(context: CoroutineContext, block: Runnable) { | ||
block.run() | ||
} | ||
} |
124 changes: 124 additions & 0 deletions
124
android/libraries/rib-coroutines/src/test/kotlin/com/uber/rib/core/RibCoroutineScopesTest.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,124 @@ | ||
/* | ||
* Copyright (C) 2024. Uber Technologies | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.uber.rib.core | ||
|
||
import com.google.common.truth.Truth.assertThat | ||
import com.uber.autodispose.lifecycle.LifecycleEndedException | ||
import com.uber.autodispose.lifecycle.LifecycleNotStartedException | ||
import kotlin.contracts.ExperimentalContracts | ||
import kotlin.contracts.InvocationKind | ||
import kotlin.contracts.contract | ||
import kotlinx.coroutines.CoroutineExceptionHandler | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.Job | ||
import kotlinx.coroutines.awaitCancellation | ||
import kotlinx.coroutines.isActive | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.test.runCurrent | ||
import kotlinx.coroutines.test.runTest | ||
import org.hamcrest.CoreMatchers.instanceOf | ||
import org.junit.Before | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.rules.ExpectedException | ||
import org.junit.runner.RunWith | ||
import org.junit.runners.Parameterized | ||
import org.mockito.kotlin.mock | ||
|
||
@OptIn(ExperimentalCoroutinesApi::class) | ||
@RunWith(Parameterized::class) | ||
class RibCoroutineScopesTest( | ||
private val throwWhenBeforeActive: Boolean, | ||
private val throwWhenAfterInactive: Boolean, | ||
) { | ||
@get:Rule val ribCoroutinesRule = RibCoroutinesRule() | ||
|
||
@get:Rule val exceptionRule: ExpectedException = ExpectedException.none() | ||
|
||
private val interactor = TestInteractor() | ||
|
||
@Before | ||
fun setUp() { | ||
RibCoroutinesConfig.exceptionHandler = CoroutineExceptionHandler { _, throwable -> | ||
when (throwable) { | ||
is CoroutineScopeLifecycleException -> if (shouldThrow(throwable)) throw throwable | ||
else -> throw throwable | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
fun coroutineScope_whenCalledBeforeActive_throwsCoroutineScopeLifecycleException() = runTest { | ||
if (throwWhenBeforeActive) { | ||
exceptionRule.expect(CoroutineScopeLifecycleException::class.java) | ||
exceptionRule.expectCause(instanceOf(LifecycleNotStartedException::class.java)) | ||
} | ||
assertThat(interactor.coroutineScope.isActive).isFalse() | ||
} | ||
|
||
@Test | ||
fun coroutineScope_whenCalledAfterInactive_throwsCoroutineScopeLifecycleException() = runTest { | ||
if (throwWhenAfterInactive) { | ||
exceptionRule.expect(CoroutineScopeLifecycleException::class.java) | ||
exceptionRule.expectCause(instanceOf(LifecycleEndedException::class.java)) | ||
} | ||
interactor.attachAndDetach {} | ||
assertThat(interactor.coroutineScope.isActive).isFalse() | ||
} | ||
|
||
@Test | ||
fun coroutineScope_whenCalledWhileActive_cancelsWhenInactive() = runTest { | ||
var launched = false | ||
val job: Job | ||
interactor.attachAndDetach { | ||
job = | ||
coroutineScope.launch { | ||
launched = true | ||
awaitCancellation() | ||
} | ||
runCurrent() | ||
assertThat(launched).isTrue() | ||
assertThat(job.isActive).isTrue() | ||
} | ||
assertThat(job.isCancelled).isTrue() | ||
} | ||
|
||
private fun shouldThrow(e: CoroutineScopeLifecycleException): Boolean = | ||
(throwWhenBeforeActive && e.cause is LifecycleNotStartedException) || | ||
(throwWhenAfterInactive && e.cause is LifecycleEndedException) | ||
|
||
companion object { | ||
@JvmStatic | ||
@Parameterized.Parameters(name = "throwWhenBeforeActive = {0}, throwWhenAfterInactive = {1}") | ||
fun data() = | ||
listOf( | ||
arrayOf(true, true), | ||
arrayOf(true, false), | ||
arrayOf(false, true), | ||
arrayOf(false, false), | ||
) | ||
} | ||
} | ||
|
||
private class TestInteractor : Interactor<Unit, Router<*>>() | ||
|
||
@OptIn(ExperimentalContracts::class) | ||
private inline fun TestInteractor.attachAndDetach(block: TestInteractor.() -> Unit) { | ||
contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } | ||
InteractorHelper.attach(this, Unit, mock(), null) | ||
block() | ||
InteractorHelper.detach(this) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is now 1.9.0-RC